用null DateTimes对DataGridView进行排序



我有一个带有DateTime值列的DataGridView,其中一些为null。直到我尝试按该列进行排序之前,才有问题。我已经实施了SortCompare代码,如下所示

datagridview在dateTime列中使用nulls进行排序

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

Object must be of type DateTime.

我在SortCompare事件处理程序中有一个try-catch,我能够成功地浏览所有代码。只有当事件处理程序返回时,就会抛出异常。

如何防止此例外?

代码:

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

然后运行程序,然后尝试对列进行排序以投掷异常。

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

相关内容

  • 没有找到相关文章

最新更新