public void SetDataSource(Grid grid)
{
DataTable table = new DataTable();
table.Columns.Add(new DataColumn("Column1"));
table.Columns.Add(new DataColumn("Column2"));
table.Rows.Add(new object[] { "Row0, Col0", "Row0, Col1" });
table.Rows.Add(new object[] { "Row1, Col0", "Row1, Col1" });
table.Rows.Add(new object[] { "Row2, Col0", "Row2, Col1" });
grid.Headers.AutoGenerate = true;
grid.DataSource = table;
Row row1 = grid.Rows[0];
Console.WriteLine("Row1, Column1: value = {0}", row1["Column1"].Value);
Console.WriteLine("Row1: index in Grid: {0}, index in Datasource = {1}", row1.VisibleIndex, row1.DataSourceIndex);
grid.Headers[0]["Column1"].SortDirection = SortDirection.Descending;
Console.WriteLine("Row1: index in Grid: {0}, index in Datasource = {1}", row1.VisibleIndex, row1.DataSourceIndex);
row1.Filtered = true;
Console.WriteLine("Row1: index in Grid: {0}, index in Datasource = {1}", row1.VisibleIndex, row1.DataSourceIndex);
Row row2 = grid.Rows[0];
Console.WriteLine("Row2: index in Grid: {0}, index in Datasource = {1}", row2.VisibleIndex, row2.DataSourceIndex);
Console.WriteLine("Row2, Column1: value = {0}", row2["Column1"].Value);
}
Output:
Row1, Column1: value = Row0, Col0
Row1: index in Grid: 0, index in Datasource = 0
Row1: index in Grid: 2, index in Datasource = 0
Row1: index in Grid: -1, index in Datasource = 0
Row2: index in Grid: 0, index in Datasource = 2
Row2, Column1: value = Row2, Col0 |