How to get the row index of a datagrid in window forms

  • Thread starter Thread starter Henry
  • Start date Start date
H

Henry

I can get the row indexes using the following codes. but problem is
that when I clicked the grid column header , it still printed out the
same indexes. So the dataview here reflects the row indexes in the
data source instead of the datagrid. What I want are the row indexes
in the datagrid. How can I do it? Thanks.

private void PrintRowIndexes(DataGrid dg)
{
CurrencyManager cm =
(CurrencyManager)this.BindingContext[dg.DataSource, dg.DataMember];
DataView dv = (DataView)cm.List;
Console.Write ("Indexes: " );
for(int i = 0; i < dv.Count; ++i)
{
if(Convert.ToString (dv["yes_no"]) == "no") {

Console.Write ("{0}\t", i);
}
}
Console.WriteLine ();
}

These always printed the same set of indexes no matter which column
headers I clicked (I called it from datagrid_MouseDown event).
 
Henry,

Your code looks quite correct. Reordering in the grid is actually
facilitated by the underlying DataView the CurrencyManager holds reference
to. You can check that by inspecting the dataview's Sort property value.

What could be a problem that the grid still hasn't had a chance to carry out
the reorder when you call the PrintRowIndexes method. Try handling the
MouseUp event instead and see what happens.
 
I have meet the same problem as you. You should retrive data from the
datagrid not DataView.



for(int i = 0; i < dv.Count; ++i)
{

// assure the datagrid1 is the object of your DataGrid
if(Convert.ToString ( datagrid1[I,¡±yes_no¡±]) == "no") {

Console.Write ("{0}\t", i);
}
}
 
Using dg[rowidx, colindx] or dataview[rowindx][colname] do the same.
Now the problem is that the printout indexes are the ones before
clicking even I used mouseUp event.

What did I go wrong?

Thanks
 
Helle I'm french!!!!
I have a problem to reordes my rows in my datagrid.Indeed I want to drowp this lines
1 ---> 6
6 ---> 2
4 ---> 3
3 --->1
2 ---> 5
5 ---> 4

I USe datragrid Windows Form
Thanks


PS : Excuse me for my english.
 
Back
Top