按空格键并在DataGridView中使用CellContentClick事件时出错

时间:2014-09-15 15:34:30

标签: c# datagridview nullreferenceexception

我用VS2012编写C#编程。在其中一个表单中,有一个带有检查列的Datagridview(DataGridViewCheckBoxColumn)。此外,捕获CellContentClick事件以检查在用户单击的同一时间是否检查CheckBox。但是当用户按空格键时会发生NullReferenceException。我已经调试过了,并且在最后一个括号中正确执行了所有代码时发生了Expection。

主要问题是我需要清除DataGridView并重新填充它。如果我将事件更改为CellValueChanged,它可以正常工作,但它不是我需要的

我怎样才能解决这个问题?

完整的代码是:

public partial class frm_datagrid : Form
{
    bool data_initialization = false;
    List<String> orders;

    public frm_datagrid(List<String> orders)
    {
        InitializeComponent();
        this.orders = orders;
    }

    private void frm_datagrid_Load(object sender, EventArgs e)
    {
        data_initialization = true;

        createDatagrid2(this.DataGrid_Orders);
        fillDatagrid(this.DataGrid_Orders, this.orders);


        data_initialization = false;
    }

    private void createDatagrid2(KryptonDataGridView dgv)
    {
        #region Datagrid Initialization
        Utils_Presentacion.createDataGridColumns(dgv, "DataGridViewCheckBoxColumn", "Check", "VR?", false, null);
        Utils_Presentacion.createDataGridColumns(dgv, "KryptonDataGridViewTextBoxColumn", "Order", "Order", true, null);


        dgv.Columns["Check"].Width = 45;
        dgv.Columns["Order"].Width = 85;


        // Columns Sort Mode
        foreach (DataGridViewColumn col in dgv.Columns)
            col.SortMode = DataGridViewColumnSortMode.NotSortable;

        dgv.AutoGenerateColumns = false;

        #endregion
    }

    private void fillDatagrid(KryptonDataGridView dgv, List<string> list)
    {
        data_initialization = true;

        dgv.Rows.Clear();

        dgv.Rows.Add();
        DataGridViewRow r = dgv.Rows[dgv.Rows.Count - 1];
        r.Cells["Check"].Style.Alignment = DataGridViewContentAlignment.MiddleCenter;

        //r.Cells["Check"].Value = false;
        r.Cells["Order"].Value = "987w4927429";

        data_initialization = false;
    }


    private void DataGrid_Orders_CellClick(object sender, DataGridViewCellEventArgs e)
    {
        if (!data_initialization)
        {

            if (e.ColumnIndex == DataGrid_Orders.Columns["Check"].Index && e.RowIndex >= 0)
            {
                changedValue2(sender, e.RowIndex);
            }
        }
    }

    private void changedValue2(object sender, int RowIndex)
    {
        if (!data_initialization)
        {
            // Get row values
            String value_check = valueOf("Check", RowIndex);
            String value_pedido = valueOf("Order", RowIndex);


            // Find the order and check or uncheck VR option
            foreach (String s in this.orders.ToList())
            {
                String order_number = s.Substring(20, 13);

                if (value_pedido == order_number)
                {
                    if (value_check != null && value_check.Trim().ToUpper() == "TRUE")
                    {
                        MessageBox.Show(s + "Checked");
                    }
                    else
                    {
                        MessageBox.Show(s + "NOT Checked");
                    }
                }
            }


            fillDatagrid(this.DataGrid_Orders, this.orders);
        }
    }
}

0 个答案:

没有答案