使用null日期时间对DataGridView进行排序

时间:2014-03-13 19:30:07

标签: c# sorting datetime datagridview null

我有一个带有DateTime值列的datagridview,其中一些是null。在我尝试按该列排序之前没有问题。我已经实现了这里的SortCompare代码

DataGridView sorting with nulls in DateTime column

但调试器仍然抛出异常并指向我的Application.Run(new MainForm()); 行,说

Object must be of type DateTime.

我的SortCompare事件处理程序中有try-catch,我能够成功完成所有代码。只有在事件处理程序返回时才会抛出异常。

如何防止此异常?

CODE:

在Visual Studio中打开一个新的c#windows窗体项目,并在窗体上使用Full dockstyle抛出一个datagridview。在Form1.cs中使用以下代码替换public Form1()方法:

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());
        }
    }

然后运行程序并尝试对列进行排序以抛出异常。

1 个答案:

答案 0 :(得分:4)

你错过了e.Handled = true;将其设置为true将解决您的问题。

 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());
        }
    }