Click here to Skip to main content
15,886,110 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have a datagredview with 50 rows , make search on grid and want to set that particular row on the top.

SQL
For Each dr As DataGridViewRow In DGAccount.Rows
               If dr.Cells(3).Value = txtAcID.Text.Trim Then
                   dr.Selected = True
                   DGAccount.FirstDisplayedScrollingRowIndex = dr.Index
                   DGAccount.CurrentCell = dr.Cells(3)
                   DGAccount.Update()
                   Exit For
               End If
           Next



Can any body help me how can i rearrange datagrid .

Thanks
Posted
Comments
TnTinMn 21-Jan-14 0:35am    
It is not completely clear what your intent is. Based on the code you have shown, it appears that you want to scroll to to the selected account Id. But then you state "rearrange datagrid"; this implies that you may want to make the found row the first row (not just first scrolled to row).

If it is just scrolling, change "dr.Cells(3).Value" to "dr.Cells(3).Value.ToString()" so that you are comparing string values. Your code appears fine for a DataGridView configured with MultiSelect=False.

If you want to rearrange the displayed row order, I can provide you an example of how to do this using "LINQ to DataSets".

1 solution

Instead of searching the grid, please search the datatable that is bound to the grid. You will be able to filter the record faster. Transfer the filtered records to another datatable and bind the new datatable to the grid. This way it will be easy for you rather than searching the grid...

I assume you are working in a web application

VB
Dim dr() as DataRow
Dim dt1 as DataTable
dt1=dt.clone()
dr=dt.Select(" AccountID='" & txtAcId.Text.Trim &"'")
for each dr1 as DataRow in dr
  dt1.ImportRow(dr1)
next

DGAccount.DataSource = dt1
DGAccount.DataBind()



Hope it helps!!

Dinesh Kumar V
 
Share this answer
 
v2

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