How to Implement Automatic Sorting of Datagridview

DataGridView auto re-sort

Finally got it work!
There is what I did:

DataGridViewColumn column = dataGridView1.SortedColumn;
ListSortDirection order;

if (dataGridView1.SortOrder.Equals(SortOrder.Ascending))
{
order = ListSortDirection.Ascending;
}
else
{
order = ListSortDirection.Descending;
}

dataGridView1.Sort(column, order);

So, I got the sorted column and I check in a if what is the order that the datagridview is sorted, then I sort again the column that used sorted for the same order...
Notice that this code is fresh so it isn't "bulletproof" I'm not checking if the values of column and the order are null or not!
If you need this code and use it, take that in mind!
It's almost like the answer of Jayesh.
Thank you all for the help!

DataGridView automatic sorting doesn't work when datasource bound

I 've found solution.

It's seems that DataGridView can't sort either List <T> or BindingList<T>

So I've added class SortedBindingList<T> based on code from:
and now my DataGridView can sort columns.

Thanks for help guys.

datagridview sorting is not done correctly

The problem is that the RowCount includes the new records row, where users can enter new data. The last data row with "after-" is added to this row, but this row appears always as the last row.

Therefore specify the row count in the gridview to be 1 higher than the number of data rows. You can then hide this extra row by specifying dataGridView1.AllowUserToAddRows = false;.

Start by reading the XML file into the data table, then set

dataGridView1.RowCount = dt.Rows.Count + 1;

But you can simplify your code dramatically, if you just assign the data table to the data source of the gridview. This creates the columns and the rows automatically. The sort mode is automatic by default.

private void Form1_Load(object sender, EventArgs e)
{
string myXMLfile = @"C:\Users\PC\Desktop\XMLFile1.xml";
DataSet ds = new DataSet();
ds.ReadXml(myXMLfile);
DataTable dt = ds.Tables["XMLFile1"];
dataGridView1.DataSource = dt;
}

In response to your EDIT 3, I suggest a totally different solution. I have tried hard with DataSets, but run into a lot of problems. Therefore I suggest reading the XML into a list of custom objects.

Let's create a class for this:

class XMLFile1
{
public int? Radif { get; set; }
public string Root { get; set; }
}

Now, in the form we declare two fields and rewrite Form1_Load like this:

private XDocument _xDoc;
private BindingList<XMLFile1> _bindingList;

private void Form1_Load(object sender, EventArgs e)
{
string myXMLfile = @"C:\Users\PC\Desktop\XMLFile1.xml";

_xDoc = XDocument.Load(myXMLfile);
var records = _xDoc.Root.Elements()
.Select(el => new XMLFile1 {
Radif = Int32.Parse(el.Element("Radif").Value),
Root = el.Element("Root").Value
})
.ToList();

_bindingList = new BindingList<XMLFile1>(records);
dataGridView1.DataSource = _bindingList;
}

Now you can do edits in dataGridView1, including adding new records through the bottom line. You don't need text boxes to enter new data. If you still want to enter new data through text boxes you can add a new entry like this:

private void buttonAdd_Click(object sender, EventArgs e)
{
_bindingList.Add(
new XMLFile1 { Radif = Int32.Parse(tbRaAdd.Text), Root = tbRoAdd.Text });
}

Note that this automatically updates dataGridView1. THis also works the other way round. All edits made in the grid are automatically persisted to _bindingList. Therefore, we can now save all edits with:

private void btnSave_Click(object sender, EventArgs e)
{
string myXMLfile = @"C:\Users\PC\Desktop\XMLFile1.xml";

dataGridView1.EndEdit();

var root = _xDoc.Root;
root.RemoveNodes();
root.Add(_bindingList.Select(x => new XElement("XMLFile1",
new XElement("Radif", x.Radif),
new XElement("Root", x.Root))));

_xDoc.Save(myXMLfile);
}

How to sort DatagridView that is bound to DbSet Collection in order of clicked column header value

If you want Automatic sort mode work for you, you need to use an implementation of IBindingList which supports sorting. Using Entity Framework, ToBindingList method of the Local property of DbSet<T> returns a sortable BindingList<T>:

//using System.Data.Entity;
using (var db = new MyDbContext())
{
db.SomeTable.Load();
dataGridView1.DataSource = db.SomeTable.Local.ToBindingList();
}

Note:
You should first Load data into DbSet by calling Load or ToList() to enumerate the IQueryable and convert it to actual data. Then the data is in the Local property.

Load is an extension method on IQueryable and is equivalent to calling ToList and then throwing away the list without the overhead of actually creating the list.

winform datagridview column sort

Both answers above are correct. I've also found another article that points to code that converts a list to a "SortableBindingList". Of course you can change the class name if you like. Look at the marked answer in

Sortable List



Related Topics



Leave a reply



Submit