Sorting DataGridView with null datetimes

I have a datagridview with a column of DateTime values and some of them are null. There isn't a problem until I try to sort by that column. I've implemented the SortCompare code as found here

DataGridView sorting with nulls in DateTime column

but the debugger is still throwing exceptions and pointing to my Application.Run(new MainForm()); line, saying

Object must be of type DateTime.

I've got a try-catch in my SortCompare event handler, and I'm able to step through all the code successfully. It's only when the event handler returns that the exception is thrown.

How can I prevent this exception?

CODE:

Open up a new c# windows forms project in Visual Studio and throw a datagridview on the form with a Full dockstyle. In Form1.cs replace the public Form1() method with the following code:

public Form1()
    {
        InitializeComponent();

        //Create datatable and add a datetime column
        DataTable dTable = new DataTable();
        DataColumn dColumn = new DataColumn("DateTimes", typeof(DateTime));
        dTable.Columns.Add(dColumn);

        //Add rows
        dTable.Rows.Add(DateTime.Now);
        dTable.Rows.Add(DBNull.Value);

        //Create a datagridview column and add it to the datagridview
        DataGridViewTextBoxColumn dgvColumn = new DataGridViewTextBoxColumn();
        dgvColumn.Name = dColumn.ColumnName;
        dgvColumn.ValueType = dColumn.DataType;
        this.dataGridView1.Columns.Add(dgvColumn);


        //Add rows to the datagridview based on datatable data.
        foreach (DataRow dRow in dTable.Rows)
        {
            DataGridViewRow dgvRow = new DataGridViewRow();
            dgvRow.CreateCells(this.dataGridView1);
            dgvRow.SetValues(dRow.ItemArray);
            this.dataGridView1.Rows.Add(dgvRow);
        }

        //Hook up sortcompare event handler
        this.dataGridView1.SortCompare += new DataGridViewSortCompareEventHandler(dataGridView1_SortCompare);

        //Style datagridview
        this.dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
    }

    private void dataGridView1_SortCompare(object sender, DataGridViewSortCompareEventArgs e)
    {
        try
        {
            if (DBNull.Value.Equals(e.CellValue1) || DBNull.Value.Equals(e.CellValue2))
            {
                if (DBNull.Value.Equals(e.CellValue1) || e.CellValue1.Equals(null))
                {
                    e.SortResult = 1;
                }
                else if (DBNull.Value.Equals(e.CellValue2) || e.CellValue2.Equals(null))
                {
                    e.SortResult = -1;
                }
            }
            else
            {
                e.SortResult = (e.CellValue1 as IComparable).CompareTo(e.CellValue2 as IComparable);
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.ToString());
        }
    }

Then run the program and try to sort the column to throw the exception.

Answers


You are missing e.Handled = true; Set it to true will fix your issue.

 private void dataGridView1_SortCompare(object sender, DataGridViewSortCompareEventArgs e)
    {
        try
        {
            if (DBNull.Value.Equals(e.CellValue1) || DBNull.Value.Equals(e.CellValue2))
            {
                if (DBNull.Value.Equals(e.CellValue1) || e.CellValue1.Equals(null))
                {
                    e.SortResult = 1;
                }
                else if (DBNull.Value.Equals(e.CellValue2) || e.CellValue2.Equals(null))
                {
                    e.SortResult = -1;
                }
            }
            else
            {
                e.SortResult = (e.CellValue1 as IComparable).CompareTo(e.CellValue2 as IComparable);
            }
e.Handled = true
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.ToString());
        }
    }

Need Your Help

Android add my application intent to edittext menu options

android android-intent

I need to add a option to my app after select,copy,copy all, paste of menu items of edit text. I have to add intent action in that menu so that when user click the option it will trigger to my

When it is OK to use SELECT * in SQL

sql select

I understand that in general it is a bad idea to use SELECT * in SQL. Are there any exception cases? Thanks!