Click here to Skip to main content
15,879,613 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear all,

My Datagridview has records from sql server 2000, in which one datagridview row is associated with the current data displayed on form. I am highlighting the associated datagridview row. But I want to show that row on the first position. So how can I perform this? Is this possible or not? If yes, any help would be highly appreciable.

Thanks and regards,
Rizwan Gazi.
Posted
Comments
Sergey Alexandrovich Kryukov 6-May-15 16:50pm    
Not clear. Do you want to re-sort DataGridView to move some chosen row on top? But 1) why, 2) what prevents you from doing so?
What have you tried so far?
—SA
Rizwan Gazi 7-May-15 2:56am    
In my Datagridview, there are many rows. So, instead of scrolling down to the selected row associated to the current record, I want to show it at first position.

Have a look at datagridview.firstdisplayedscrollingrowindex[^]
E.g.
C#
private void dataGridView1_SelectionChanged(object sender, EventArgs e)
{
    if (dataGridView1.SelectedRows.Count == 0) return;
    dataGridView1.FirstDisplayedScrollingRowIndex = dataGridView1.SelectedRows[0].Index;
}
The first line is included because the SelectionChanged event appears to be fired when populating the dataGridView

[Edit] - The solution in VB.NET - mea culpa
SQL
Private Sub DataGridView1_SelectionChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DataGridView1.SelectionChanged
    If DataGridView1.SelectedRows.Count = 0 Then Return
    DataGridView1.FirstDisplayedScrollingRowIndex = DataGridView1.SelectedRows(0).Index
End Sub
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 6-May-15 17:27pm    
5ed.
—SA
CHill60 6-May-15 17:58pm    
Thank you.
Maciej Los 7-May-15 2:13am    
5ed!
Rizwan Gazi 7-May-15 2:32am    
Dear @CHill60, your solution is not working. Anything remaining?
CHill60 7-May-15 4:17am    
I've updated my solution with the VB.NET solution (my apologies). It's working fine for me in VS2008 Express and VS2013 CE. If you still can't get it working then try Solution 2
Here's another solution, which requires a bit more effort but will actually move the selected row to the top of all rows, instead of moving the "view" so that the row appears to be at the top but not actually changes its position among the other rows. (Which has the benefit that it will show at the top even if there aren't enough records present to move the "view" so it could appear at the top.) It's up to you to decide which solution you want :)

1) After filling the records into a DataTable, add another column to the DataTable with a name like "selected" and type of boolean.

2) If you currently assign the DataTable directly to the .DataSource of your DataGridView, instead assign the .DefaultView of the DataTable to the .DataSource of your DataGridView.

3) Set the .Sort-Property of the .DefaultView of the DataTable to "selected desc".

4) Set the .Visible-Property of the "selected"-column of the DataGridView to false.

5) Where you currently have your code to highlight the "form-associated" row, do this instead:
5.1) Set the .Value-Property of the "selected"-cell of the first row to false.
5.2) Set the .Value-Property of the "selected"-cell of the "form-associated" row to true.
5.3) Call ClearSelection() on the DataGridView.
5.4) Set the .Selected-Property of the first row to true.
 
Share this answer
 
v3
Comments
Sergey Alexandrovich Kryukov 7-May-15 9:17am    
5ed.
—SA
Sascha Lefèvre 7-May-15 11:31am    
Thank you, Sergey.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900