如何使datagridview的单个单元格只读?

时间:2014-09-18 07:45:36

标签: c# winforms datagridview datagridviewcolumn

我的datagridview中有一个winform。我想在特定条件下对此datagridview cell进行单独noneditable。为此我尝试了两种方法,但其中任何一种方法对我来说效果不佳。 这是代码

private void dataGridView1_CellEnter(object sender, DataGridViewCellEventArgs e)
    {
        foreach (DataGridViewRow row in dataGridView1.Rows)
        {
            try
            {
                if (row.Cells[4].Value.ToString() == "0")
                {
                    row.Cells[2].ReadOnly = true;
                    row.Cells[2].Value = "0";
                }
                else
                {

                }
            }
            catch (Exception ee)
            {

            }
        }
        //if (dataGridView1.Rows[e.RowIndex].Cells[4].Value.ToString() == "0")
        //{
        //    dataGridView1.Rows[e.RowIndex].Cells[2].ReadOnly = true;
        //    dataGridView1.Rows[e.RowIndex].Cells[2].Value = "0";
        //}
    }

inside foreach loop抛出Object reference not set to an instance of an object.异常的代码 并且commented portion of code将所有单元格readonly属性设置为true。但我想要的只是那些单元格属性设置为true cell[4].value=0 Plz建议我如何根据条件使这项工作更好。

0 个答案:

没有答案
相关问题