Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Windows Forms / WinForm Controls / March 2005

Tip: Looking for answers? Try searching our database.

DataGrid Selection Highlighting After Row Sort.

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
el_sid - 29 Mar 2005 09:53 GMT
I have a DataGrid that is bound to a DataView and everything seems to work
fine. When the user clicks on a cell the entire row gets highlighted, however
when the user decides to sort the datagrid  things go wrong.  I have added
code to ensure that the row of data the user had selected gets re-selected
after the sort, but when I try to highlight the selected row (by using
DataGrid.Select()) the row fails to be highlighted.  Now what seems to be
happening is that my code gets called before the grid is physically sorted on
the screen (despite it being sorted behind the scenes long before this) and
the select gets overriden with this on screen sort. Is there any way to force
the selection after this on screen datagrid sort so the row gets
rehighlighted?
"Jeffrey Tan[MSFT]" - 30 Mar 2005 06:18 GMT
Hi el_sid,

Thanks for your posting!!

Can you show us where is the DataGrid.Select() called after column sorting?
Normally, if we explicitly call this method in DataGrid_MouseUp or
CurrencyManager.CurrentChanged event, both will have no effect. The cause
is what you suspect: the actual dataview sorting occurs after these 2
events.

For this issue, we should inherit from DataGrid class, then in the child
class override its OnMouseUp method. At last, we should place the
DataGrid.Select() method calling after the base.OnMouseUp (e) call. Sample
code lists below:
public class MyDataGrid : System.Windows.Forms.DataGrid
{
    private System.ComponentModel.Container components = null;
    public MyDataGrid()
    {
    }

    protected override void Dispose( bool disposing )
    {
        if( disposing )
        {
            if(components != null)
            {
                components.Dispose();
            }
        }
        base.Dispose( disposing );
    }

    protected override void OnMouseUp(MouseEventArgs e)
    {
        base.OnMouseUp (e);

        System.Drawing.Point pt = new Point(e.X, e.Y);  
        DataGrid.HitTestInfo hti = this.HitTest(pt);
        if(hti.Type == DataGrid.HitTestType.Cell)  
        {
            this.CurrentCell = new DataGridCell(hti.Row, hti.Column);
            this.Select(hti.Row);
        }

        if(hti.Type == DataGrid.HitTestType.ColumnHeader && hti.Column == 1)
        {               
            this.Select(this.CurrentRowIndex);
        }
    }

           private void InitializeComponent()
    {
        this.Name = "MyDataGrid";
    }
}
This works well on my side.

Addtionally, I am not sure if you want to select the original selected row,
or the new sorted row in CurrentRowIndex. Normally, DataGrid will sort the
underlying dataview, and lose track of the original selected row. So if you
want to keep track of the original selected row index after sorting. I
think you may follow the below program logic:
1) To override the grid's OnMouseDown protected method to trap clicks on
column headers (the HitTest method can be used to determine where exactly
the click occured).
2) Store the primary key of the current row in a private variable
3) In the OnMouseUp event, after calling base.OnMouseUp(), look up the
stored primary key value and select the appropriate row in the grid.

Hope this helps you.
=========================================
Thank you for your patience and cooperation. If you have any questions or
concerns, please feel free to post it in the group. I am standing by to be
of assistance.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Signature

Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

el_sid - 30 Mar 2005 13:47 GMT
With a couple of modifications that worked.

Thanks

> Hi el_sid,
>
[quoted text clipped - 77 lines]
> Get Secure! - www.microsoft.com/security
> This posting is provided "as is" with no warranties and confers no rights.
"Jeffrey Tan[MSFT]" - 31 Mar 2005 04:33 GMT
Hi el_sid,

Oh, I am glad my reply makes sense to you. If you need further help, please
feel free to tell me, I will work with you. Thanks

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Signature

Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.