tonn

Hi,
Asked before but quite surprisingly not answered. Allow me to try again.

I have a DataGridView bound to a DataTable. I want my users to be able to sort a column by clicking the header. I don't want the DataGridViewRows to rearrange themselves while a user is editing cells, as this is perceived as annoying.
However, once a sort has been invoked the DataGridView keeps on sorting.

How do I tell the DataGridView to stop, cancel or end sorting
Why does the DataGridView behave like this Is this a bug

I tried the DataGridViewColumn.SortMode property but this doesn't cancel sorting. It merely prevents user from sorting themselves.
I tried a few work arounds (e.g. cloned DataSets) mentioned in this forum but they are cumbersome and keep raising issues.

I would appreciate your help on this matter.
Regards, Tonn



Re: Windows Forms Data Controls and Databinding DataGridView: how to stop / cancel sorting

Bob zhu - MSFT

please set sortmode to nonsortable and add event at head click:)
using
System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace WindowsApplication2
{
public partial class Form10 : Form
{
public Form10()
{
InitializeComponent();
}
public BindingSource BS = new BindingSource();
private void Form10_Load(object sender, EventArgs e)
{
Table_AB TBC = new Table_AB();
DataTable TB = TBC.GetTable();
BS.DataSource = TB;
this.dataGridView1.DataSource = BS;
this.dataGridView1.Columns[0].SortMode = DataGridViewColumnSortMode.NotSortable;
}
private void dataGridView1_ColumnHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
{
int a=this.dataGridView1.CurrentCell.ColumnIndex;
//MessageBox.Show(a.ToString());
this.dataGridView1.Sort(dataGridView1.ColumnsAngel,ListSortDirection.Ascending);
}
}
}





Re: Windows Forms Data Controls and Databinding DataGridView: how to stop / cancel sorting

tonn

Hi Bob, thanks for responding.

I'm sorry to say it doesn't work. Once the Sort(..) method has been called the DataGridView remains in 'sort-mode'.

So if a user starts editing cells in the sorted column, the rows start rearranging themselves.
The sorting works fine, but after that the rows must remain where they are.





Re: Windows Forms Data Controls and Databinding DataGridView: how to stop / cancel sorting

Bob zhu - MSFT

hoho, I tried it again and sort it myself,

sorry for the wrong post, it seems ok now,

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Text;

using System.Windows.Forms;

using System.Collections;

namespace WindowsApplication2

{

public partial class Form11 : Form

{

public Form11()

{

InitializeComponent();

}

DataTable TB;

private void Form11_Load(object sender, EventArgs e)

{

Table_AB TBC = new Table_AB();

TB=TBC.GetTable();

this.dataGridView1.DataSource=TB;

this.dataGridView1.Columns[0].SortMode = DataGridViewColumnSortMode.NotSortable;

}

private void dataGridView1_ColumnHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)

{

for (int i = 0; i < TB.Rows.Count; i++)

{

for (int j = i + 1; j < TB.Rows.Count; j++)

{

if (Convert.ToInt32(TB.RowsIdea[0].ToString()) > Convert.ToInt32(TB.Rows[j][0].ToString()))

{

DataRow row = TB.NewRow();

row[0]=TB.Rows[j][0];

row[1]=TB.Rows[j][1];

TB.Rows.InsertAt(row, i);

TB.Rows.RemoveAt(j+1);

}

}

}

}

}

}






Re: Windows Forms Data Controls and Databinding DataGridView: how to stop / cancel sorting

tonn

Bubble sorting the DataTable
I think this is easier done by cloning using the DataView:

DataView dataView = MyDataTable.DefaultView;
dataView.Sort = MyColumn.Name +
" DESC";
MyDataTable = dataView.ToTable();
MyDataGridView.DataSource = MyDataTable;

Both methods indeed work, however now I've lost all my changes. I must be able to mark these changes as well as revert them by calling MyDataTable.RejectChanges().
Is there a way to do this and retain the change information





Re: Windows Forms Data Controls and Databinding DataGridView: how to stop / cancel sorting

adelu

I have the same problem. I need to suspend the auto-sort from occurring. I was wondering if you were ever able to find a fix



Re: Windows Forms Data Controls and Databinding DataGridView: how to stop / cancel sorting

tonn

Hi Adelu,

I've never been able to find a proper fix. Here are my findings:

  1. DataBinding doesn't offer any help.
    Since DataBinding mediates between DataSources and Controls I have looked for hooks which I could use. I've found none.
  2. It's possible to write your own DataView.
    According to documentation a custom DataView should be able to provide sorting (or suspended sorting).
    I haven't come around to try this.
  3. A reasonable work around is setting up a custom objectlist.
    The objectlist is read from- and written to a DataSet or DataTable.
    You can easily add your own sorting code to the objectlist.
    If you use a BindingList or a BindingSource you can still DataBind Controls to your data.


My suggestions:

  • I you have the time you could try the DataView option. I would be interested in the result myself.
  • Try option #3.
    It takes a little elbow-greese but it is generally quickly done and once set up you have an ideal situation for change or customization.

HTH, regards Tonn