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

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.
 
D

David

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());
}
 
R

Rajesh.V

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());
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top