Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Get data from DataGrid after sorting 1

Status
Not open for further replies.

spcheng7

Programmer
Feb 27, 2002
7
HK
Hi all,
I have created a datagrid in a window form. The datagrid
can display the data from the database perfectly. But if
I have clicked the top rows, which means row containing the
coulmns name, the rows would be sorted by that column asc
or desc.
But when I get the value of a cell, the value still is
the value before sorting. For example,
row 1 : 1 a b c 3
row 2 : 2 g h e f

after I sort the table by clicking the column name.
it becomes :
row 1 : 2 g h e f
row 2 : 1 a b c 3

But when I clicked the cell (row 2 and column 1 ), the
returned value is 2 , not 1

I want to ask how I can get the correct value after sorting.
Thx

SP
 
Make sure you rebind your grid after executing the sort procedure.
 
When you sort a grid, you are not sorting the underlying datasource, so row 2 on the grid is not necessarily row 2 in the underlying table.

When a grid is sorted, a DataView is automatically created to do the sort, and this is what the datagrid shows - however, the underlying datatable remains unsorted.

To get the correct row value, use something like this:
Code:
Dim drv As DataRowView

drv = CType(DataGrid1.DataSource, DataView).Item(DataGrid1.CurrentRowIndex)

MessageBox.Show(drv.Item(colIndex).ToString)
 
Oops, sorry about the VB code (wrong forum....)
Code:
DataRowView drv;

drv = ((DataView)DataGrid1.DataSource)[DataGrid1.CurrentRowIndex];

MessageBox.Show(drv[colIndex]);
 
Hi all

I have encountered the same problem getting the correct values after sorting in a colunm.

I have a button which runs the following code given in the previous reply:

DataRowView drv;

drv = ((DataView)DataGrid1.DataSource)[DataGrid1.CurrentRowIndex];

MessageBox.Show(drv[colIndex]);

But I get this Exception when running it:
Specified cast is not valid.

Any ideas why??
Is there anything special I need to do before running the code?
 
Is your grid sorted ? If not, the datasource will not be a DataView and therefore the cast will fail.
 
AllowSorting = true for the DataGrid.
I have clicked the top rows, which means row containing the
coulmns name, the rows would be sorted by that column asc
or desc.

Doesn't this means that the DataGrid is sorted.

Since I've been dealing with this problem for 2 days now, I'm really desperate for a solution.

Thanks.
 
Try this:
Code:
BindingManagerBase bm = DataGrid1.BindingContext[DataGrid1.DataSource, DataGrid1.DataMember]; 
 
DataRow dr = ((DataRowView)bm.Current).Row;

MessageBox.Show(dr[colIndex].ToString());
 
That works!

Is there a way of getting the index/rownumber of the selected row, it has to be the correct index even if the column is sorted by clicking the header.

I want't to use the row index to move a row from DataGrid1 to DataGrid2, and delete that row from DataGrid1.

Thanks.
 
Use
Code:
DataTable.ImportRow(dr)
to copy the row from one datasource to the other, then remove the row from the first datasource.
 
The importRow() works fine, but
o use: DataTable.Rows[rowIndex].Delete()

I need to get the row index, but the problem is getting the correct index after sorting.

Any ideas?
 
I actually found an easy way of deleting the row:

After this:

BindingManagerBase bm = DataGrid1.BindingContext[DataGrid1.DataSource, DataGrid1.DataMember];

DataRow dr = ((DataRowView)bm.Current).Row;

since dr is the row I want to delete I just use the
Delete() method:

dr.Delete()

removes the row from the first datasource.

Thanks to SHelton for providing quick and great help.
[bigsmile]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top