C#datagridview根据条件触发编辑与不可编辑

C#datagridview根据条件触发编辑与不可编辑

问题描述:

图片说明

如图所示,C#datagridview,第二列选择了ARRAY则后面4列必须填入内容,不许为空。

如果选择了不是ARRAY,则后面的4列灰显,不可以编辑。

应该用到datagridview哪个事件??

添加CellEndEdit事件,用下面的代码就行



        private delegate void setState();
        private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            DataGridViewCell cur = dataGridView1.CurrentCell;
            bool setFucos = false;
            if (e.ColumnIndex == 1)
            {
                bool readOnly = cur.Value.ToString() != "ARRAY";
                for (int i = 2; i < 6; i++)
                {
                    dataGridView1.CurrentRow.Cells[i].ReadOnly = readOnly;
                    dataGridView1.CurrentRow.Cells[i].Style.BackColor = readOnly ? Color.Gray : Color.White;
                    if (!readOnly)
                    { //设置下一个单元格获取焦点
                        setFucos = true;
                        cur = dataGridView1.CurrentRow.Cells[2];
                    }
                }
            }
            if (e.ColumnIndex > 1 && e.ColumnIndex < 6)
            {

                if (cur.Value == null || cur.Value.ToString() == "")
                {
                    MessageBox.Show("不能为空!");
                    setFucos = true;
                }
            }
            if (setFucos)
            {//重置焦点
                System.Timers.Timer t = new System.Timers.Timer(100);
                t.Elapsed += (tsender, te) =>
                {
                    t.Dispose();
                    this.Invoke(new setState(() =>
                    {

                        dataGridView1.CurrentCell = dataGridView1.CurrentRow.Cells[2];
                        dataGridView1.BeginEdit(true);
                    }));
                };
                t.Enabled = true;
            }
        }

你哪来的这么多c币?而且你是学生吗? 你这个ARRAY应该是单元格值吧?那就在加载数据的方法里加上(应该有事件,有事件就用事件):
1.获取CATEGORY这列单元格值,然后判断值,根据索引设置其他列单元格readonly=true;
这里解释下,你去找找datagridview事件,我用Dev 插件控件Gridview有对应事件。用事件的话,索引很好控制,不然你就得循环数据了。

给Category列的cellviewchanged事件上做一些操作
触发之后判断当前变化的值,
下面是禁止编辑的属性。
this.DataGridView.Rows[index].Cell[index].ReadOnly = true;

不懂再问。

Me.DataGridView1.ReadOnly = True

设置cell方法就行啊,当值改边成为一个array,就可以直接使用

用服务器端控件不好,需要回到服务器处理事件,建议使用客户客户端控件,然后通过JQUERY来解决。