DataGrid Cant sort if i need to know which row is selected.

  • Thread starter Thread starter Rajesh.V
  • Start date Start date
R

Rajesh.V

This is the scenario.

1. I am binding the customer table to a datagrid

2. Dont want the customerid to be shown in the datagrid, so am setting
ColumnMapping = MappingType.Hidden

3. Now when the user selects a row, its index and the tables row index is
the same, so i can access, the record

without the primarykey - customerid.

The problem now is i cant allow sorting. Does somebody know how to enable
sorting and also enable me to

identify the selected customer record. Also i dont want to set the
Gridcolumnstyle width to 0 if i dont set the columnmapping to hidden.
 
This is the scenario.

1. I am binding the customer table to a datagrid

2. Dont want the customerid to be shown in the datagrid, so am setting
ColumnMapping = MappingType.Hidden

3. Now when the user selects a row, its index and the tables row index is
the same, so i can access, the record

without the primarykey - customerid.

The problem now is i cant allow sorting. Does somebody know how to
enable sorting and also enable me to identify the selected customer
record. Also i dont want to set the Gridcolumnstyle width to 0 if i
dont set the columnmapping to hidden.

The answer here is to realize that it's the CurrencyManager rather than
the Grid itself that keeps track of which row is where. So when you
need to know the current row, ask the CurrencyManager for the current
record rather than asking the DataGrid for the current row index,
like...

MyDataGrid.DataSource = _myDataSet;
MyDataGrid.DataMember = "Customers";

......... Later....

private void ShowCurrentRow
{
CurrencyManager cm =
(CurrencyManager) this.BindingContext[_myDataSet, "Customers"];
DataRow row = ((DataRowView) cm.Current).Row;

MessageBox.Show(row["CustomerID"].ToString());
}
 
David,

That was very useful, tx. Now sorting and editing of event child tables
possible.


David said:
This is the scenario.

1. I am binding the customer table to a datagrid

2. Dont want the customerid to be shown in the datagrid, so am setting
ColumnMapping = MappingType.Hidden

3. Now when the user selects a row, its index and the tables row index is
the same, so i can access, the record

without the primarykey - customerid.

The problem now is i cant allow sorting. Does somebody know how to
enable sorting and also enable me to identify the selected customer
record. Also i dont want to set the Gridcolumnstyle width to 0 if i
dont set the columnmapping to hidden.

The answer here is to realize that it's the CurrencyManager rather than
the Grid itself that keeps track of which row is where. So when you
need to know the current row, ask the CurrencyManager for the current
record rather than asking the DataGrid for the current row index,
like...

MyDataGrid.DataSource = _myDataSet;
MyDataGrid.DataMember = "Customers";

........ Later....

private void ShowCurrentRow
{
CurrencyManager cm =
(CurrencyManager) this.BindingContext[_myDataSet, "Customers"];
DataRow row = ((DataRowView) cm.Current).Row;

MessageBox.Show(row["CustomerID"].ToString());
}
 
Back
Top