日期:2014-05-18  浏览次数:21060 次

DataGridView显示中,如何做到:某一列的值为空时,另一列是不可编辑的,否则可编辑
DataGridView显示中,如何做到:某一列的值为空时,另一列是不可编辑的,否则可编辑。谢谢。

------解决方案--------------------
//设置单元格的只读属性
dgv1.rows[0].columns[1].readonly=(dgv1.rows[0].columns[0].value==dbnull.value)

// 设置 DataGridView1 的第2列整列单元格为只读
DataGridView1.Columns[1].ReadOnly = true;

// 设置 DataGridView1 的第3行整行单元格为只读
DataGridView1.Rows[2].ReadOnly = true;

// 设置 DataGridView1 的[0,0]单元格为只读
DataGridView1[0, 0].ReadOnly = true;

2) 使用 EditMode 属性
DataGridView.EditMode 属性被设置为 DataGridViewEditMode.EditProgrammatically 时,用户就不能手动编辑单元格的内容了。但是可以通过程序,调用 DataGridView.BeginEdit 方法,使单元格进入编辑模式进行编辑。
[C#]
DataGridView1.EditMode = DataGridViewEditMode.EditProgrammatically;

------解决方案--------------------
#1 正解 直接控制列的只读属性即可。(#2 我发错帖子,抱歉)
------解决方案--------------------
C# code
 private void dgv_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            DataGridViewRow row = this.dgvPathWay.Rows[e.RowIndex];
            bool isNull = false ;
            if (e.ColumnIndex ==1)
            {
                if (row != null)
                {
                    if (row.Cells["name1"].Value.ToString() == "")
                    {
                        isNull = true;
                    }

                }
            }
            if (e.ColumnIndex ==2)
            {
                if (row != null)
                {
                    if (isNull)
                        row.Cells["name2"].ReadOnly = true;

                }
            }
        }