Hitesh
Hitesh

Reputation: 1228

DatagridView Column Sorting

I have bind datagrid with following values. Id as Int,Price as int, IsActive as bit. Now I want to sort data based on IsActive when i click on column header of IsActive.

I did the same thing for Id and it working properly but for IsActive it is not working.

Below is my code for IsActive field :

 private void dataGridView1_SortCompare(object sender, DataGridViewSortCompareEventArgs e)
    {
        // Try to sort based on the cells in the current column.
        e.SortResult = System.String.Compare(e.CellValue1.ToString(), e.CellValue2.ToString());

        // If the cells are equal, sort based on the ID column. 
        if (e.SortResult == 0 && e.Column.Name != "IsActive ")
        {
            e.SortResult = System.String.Compare(
                dataGridView1.Rows[e.RowIndex1].Cells["IsActive "].Value.ToString(),
                dataGridView1.Rows[e.RowIndex2].Cells["IsActive "].Value.ToString());
        }
        e.Handled = true;
    }

I want to know How can i sort Boolean data in datagridView.

Upvotes: 0

Views: 10857

Answers (1)

Avi Turner
Avi Turner

Reputation: 10456

According to MSDN -DataGridView.SortCompare Event, The SortCompare will be triggered only for columns that have their "SortMode" property set to "Automatic":

DataGridViewColumn col = this.dataGridView1.Columns["IsActive"];
col.SortMode = DataGridViewColumnSortMode.Automatic;

If I may, I would suggest letting .Net doing the dirty job. Lets assume you have 3 rows in your grid view:

Id  Price  IsActive
1   1        1
2   2        1
3   11       1

In the way you have implemented the sorting, if you sort by price, row 3 will precede row 2 (the string "11" comes before "2"...). Preferably you would have your data in a data table, bind the datatable to the gridview and let .Net do the rest:

 /// <summary>
    /// Binds the Grid view.
    /// </summary>
    private void BindGridView()
    {
        //Creating the columns. 
        //The gridview will know how to sort Items by the type specified in the second argment
        DataTable dt = new DataTable();
        dt.Columns.Add("Id",typeof(int));
        dt.Columns.Add("Price",typeof(int));
        dt.Columns.Add("IsActive",typeof(bool));

        //Creating some random data
        //Replace this with your actual data...
        Random rnd = new Random(1);
        for (int i = 0; i < 100; i++)
        {
            int Id = i+1;
            int Price = Id%2 == 0? 500-Id*2:350+Id*3;
            bool isActive = (Id%5) !=0;

            DataRow row =  dt.NewRow();
            row["Id"] =Id ;
            row["Price"] = rnd.Next(1000) ;
            row["IsActive"] = isActive;
            dt.Rows.Add(row);                
        }

        this.dataGridView1.DataSource = dt;

        //making sure all columns are sortable
        foreach (DataGridViewColumn col in this.dataGridView1.Columns)
        {
            col.SortMode = DataGridViewColumnSortMode.Automatic;
        }            


    }

Upvotes: 1

Related Questions