1
votes

I have created a datagridview and set the sort mode property of each column to Automatic. I then bind the datagridview with a list, and attempt to sort ascending or descending, but neither is working for my datagridview columns.

My sample code is given below.

    using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace PBAttendance
{
    public partial class frmFormTest : Form
    {
        public frmFormTest()
        {
            InitializeComponent();
        }

        private void frmFormTest_Load(object sender, EventArgs e)
        {
            tempDataList tmplist = new tempDataList();
            for(int i=0; i<10; i++)
            {
                tempData tmp = new  tempData();
                tmp.Name=i.ToString();
                tmplist.Add(tmp);
            }


            dataGridView1.Columns[0].DataPropertyName = "Name";
            BindingSource bs = new BindingSource();
            bs.DataSource = tmplist;
            dataGridView1.DataSource = bs;
        }
    }
    public class tempData
    {
        string name = null;

        public string Name
        {
            get { return name; }
            set { name = value; }
        }

    }
    public class tempDataList : List<tempData>
    {
    }
}
1
Usually "automatic" means you can't change it. - banging
@banging the up and down arrow in column header doesn't appear while i use the generic list to bind the datagridview. if i use normal list or normally added the content to the cell it is visible and also the information is sorted - Siva
In this case, Automatic sort mean nothing.You have to choose asc or desc, or nothing. Don't add a sort on your dataGrid, and you will be ale to sort it by a click on a column. - provençal le breton

1 Answers

0
votes

By default, List has no support for sorting when bound to a DataGridView (DGV). See How do I implement automatic sorting of DataGridView? for solutions that will work. Mine is to use the BindingListView library to make the code as simple as bs.DataSource = new BindingListView<tempData>(tmplist);.

Just an additional note on style and syntax, since it looks like you're used to Java and there are some differences:

  • Class names are PascalCase (e.g. TempDataList, although in the snippet you provided there is no disadvantage to just using var tmplist = new List<tempData>())
  • Local variables can be whatever but are usually camelCase (e.g. tmpList, although with Visual Studio's intellisense there is no reason to include the type in the name of the variable).
  • Sub-classes usually append the name of their base class, e.g. TestForm instead of frmFormTest, although there are still some advantages to Hungarian notation (frmTest) on filenames and classes extending Base Class Libraries. (These advantages don't extend to variable names in most cases, though: it's better to have bool isFormValidated than bool bValid.)
  • Regarding properties, especially auto-properties with no need for a backing field, just do public string Name {get; set;} (the default value for strings is null). If you want a default value, use a readonly property (public string Name {get; private set;}) and set its value in the constructor or else you can use a backing field.

I don't point these out to be a jerk (and I'm sorry if I've offended); these would have been useful pointers to me a few years ago and maybe others who aren't yet familiar with C# will find them useful. Also, note that style can be automatically checked (and fixed, e.g. with ReSharper or CodeRush).

Finally, the best book on .net, especially for intermediate programmers, is Framework Design Guidelines by Cwalina and Abrams (whether or not you are designing a framework).