Sorting issue !

  • Thread starter Thread starter Gary
  • Start date Start date
G

Gary

Hi,

We are facing a typical problem with sorting a datagrid in Windows
application ( c# )

The scenario

We have a datagrid which is binded to a dataview which conatins two columns
say Name and Age.

We tried with allowsorting property, to make the sorting enable and it is
working fine.

But the problem is

As soon as a user enters some value in new row, and leaves the row, the row

display position will change, based on the sort order. This is very much

Odd looking. Actually what I expect is, the new row should stay in the last

poistion, until the user does some action (like clicking the column header)

to sort it again.

For acheiving this, we tried with setting the allowsorting property back

to false as soon as the sorting is finished once then the display will also
get unsorted, which is not expected.

It will be of great help if someone can sort out this problem. Thanks in
advance.

- Gary -
 
Hi Gary,

I feel your pain. This indeed might look odd, but I'm afraid there is no way
to change this behaviour - well, at least unless you provide your own
implementation of the IBindingList interface that would skip newly added
rows on sorting.
 
Here's one potential solution, but it's still a bit odd! You can remove the
sort when the user enters data into the new row. This reverts the display to
the view stored in the DataTable, but it does leave the newly created row at
the bottom of the grid.

private BindingManagerBase bindingManager;

private void Form1_Load(object sender, System.EventArgs e)
{
daOrders.Fill(dsOrders1, "Orders");
dataGrid1.DataSource = dsOrders1.Tables["Orders"];

bindingManager = this.BindingContext[dataGrid1.DataSource];
bindingManager.PositionChanged += new System.EventHandler(PositionChanged );
}


private void PositionChanged (object sender, System.EventArgs e)
{
Console.WriteLine("PositionChanged " + bindingManager.Position.ToString() );

if(bindingManager.Count > ((DataTable)dataGrid1.DataSource).Rows.Count)
((DataTable)dataGrid1.DataSource).DefaultView.Sort = "";
}

HTH,
Eric Cadwell
http://www.origincontrols.com
 
Back
Top