Datagrid Sorting Problem

P

PJHORNSA

Hi,

I am stuck on a problem with the datagrid control in Visual Studio
2003, using C#.

I have a main form with a datagrid, then a search screen that filters
the dataset from the grid. Well on the search screen I get the current
selected record's ID with this code:


BindingManagerBase b = this.BindingContext[Datatable1];
DataRowView rvCurrent = (DataRowView) b.Current;
DataRow row = rvCurrent.Row;

CurrID = int.Parse(row["ID"].ToString());
this.DialogResult = DialogResult.OK;
this.Close();

then back on the main form I use the ID to navigate to the correct
record with this code:

if (frmFind.DialogResult == DialogResult.OK)
{
for(int x=0;x<dataSetEmployee1.Employee.Rows.Count;x++)
{
if(dataSetEmployee1.Employee.Rows[x]["ID"].ToString() ==
frmFind.CurrentID.ToString())
{
dataGrid1.BindingContext[dataSetEmployee1,"Employee"].Position =
x;
break;
}
}
}

now this works fine but if I click on the datagrids header to change
the sorting. I get the wrong record.
If anyone can help please.

Thanx in advance
Paul
 
A

Alex Koltun

Hi,

Store the filter in some variable, then catch the event of the sorting
of the grid and then invoke your code that selects the correct row once
again.

The next thing you will need to deal with is how to catch the sorting
event of the grid,
there are some protected event in DataGrid called "RowHeaderClick", it
may help you.

Hope it helped you.
 
C

Cor Ligthert [MVP]

PJHornsa,

Nice code, however you are not the first with this problem.

Try to use the table.defaultview (of a dataview) as your datasource and your
binding source.

I hope this helps,

Cor
 
P

PJHORNSA

I am not entirely sure what you mean, can you please explain in more
detail. Thanx for the quick reply...
 
C

Cor Ligthert [MVP]

Using your code, something as

BindingManagerBase b = this.BindingContext[Datatable1.DefaultView];

And that than of course consequent

DataGrid1.DataSource = Datatable1.DefaultView

(The defaultview is the inbuild dataview in the datatable).

In that is the sort property included which tells what is the latest sort
when that is done by a columnchange.

I hope this helps,

Cor
 

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