C# 带有CellContentDoubleClick事件c的dataGridView列引用和嵌套if语句

C# 带有CellContentDoubleClick事件c的dataGridView列引用和嵌套if语句,c#,winforms,if-statement,datagridview,nested,C#,Winforms,If Statement,Datagridview,Nested,大家好,我看到了很多关于DGV中单元格引用和嵌套if语句的帖子。这篇文章让我走得太远了,我很快就到了,也许还没有。。。我希望能得到一些帮助和建议 非常简单,我需要能够双击dgv中的某个单元格,双击该单元格时,如果该单元格位于某个列中,它会对其相关列应用1或0 因此,双击第2列中的单元格会根据第5列以外的行应用数字1或0 so,column 2 relates to 5 6 relates to 9 10 relates to 13 14 relates to

大家好,我看到了很多关于DGV中单元格引用和嵌套if语句的帖子。这篇文章让我走得太远了,我很快就到了,也许还没有。。。我希望能得到一些帮助和建议

非常简单,我需要能够双击dgv中的某个单元格,双击该单元格时,如果该单元格位于某个列中,它会对其相关列应用1或0

因此,双击第2列中的单元格会根据第5列以外的行应用数字1或0

so,column 2 relates to 5
      6 relates to 9
      10 relates to 13
      14 relates to 17
      18 relates to 21
      22 relates to 25
这就是我到目前为止所做的。我已经做了很多事情,并且已经做了很多,但是我不能让它独立工作。因此,如果我双击所有列,则不会更改相关列。目前这不起作用。欢迎任何帮助

        private void dataGridViewAcorn_CellContentDoubleClick(object sender, DataGridViewCellEventArgs e)
    {

        commandBuilder = new SqlCommandBuilder(dataAdapter);
        dataAdapter.UpdateCommand = commandBuilder.GetUpdateCommand();//get the update command



        string HW_A = dataGridViewAcorn.CurrentRow.Cells[5].Value.ToString();
        string Crwn_A = dataGridViewAcorn.CurrentRow.Cells[9].Value.ToString();
        string HB_A = dataGridViewAcorn.CurrentRow.Cells[13].Value.ToString();
        string OV_A = dataGridViewAcorn.CurrentRow.Cells[17].Value.ToString();
        string Acorn_A = dataGridViewAcorn.CurrentRow.Cells[21].Value.ToString();
        string Brn_A = dataGridViewAcorn.CurrentRow.Cells[25].Value.ToString();

        int columnIndex = dataGridViewAcorn.CurrentCell.ColumnIndex;


        if (columnIndex == 2)
        {
            if (HW_A == ("1"))

            {
                dataGridViewAcorn.CurrentRow.Cells[5].Value = 0;

            }
            else HW_A = ("0");

            {
                dataGridViewAcorn.CurrentRow.Cells[5].Value = 1;

            }


        }



        /*
        System.Text.StringBuilder messageBoxCS = new System.Text.StringBuilder();
        messageBoxCS.AppendFormat("{0} = {1}", "ColumnIndex", e.ColumnIndex);
        messageBoxCS.AppendLine();
        messageBoxCS.AppendFormat("{0} = {1}", "RowIndex", e.RowIndex);
        messageBoxCS.AppendLine();
        MessageBox.Show(messageBoxCS.ToString(), "CellConentDoubleClick Event");

        */

        dataGridViewAcorn.DataSource = bindingSourceAcorn;
        bindingSourceAcorn.EndEdit();// updates table in memory 
        dataAdapter.Update(table);//actually the data base 
                                  //MessageBox.Show("Update Successful!");




    }

首先,创建列依赖项对象,这样可以避免多个ifs。我知道,这不是可选的内存分配,如果你想优化,你可以使用Dictionary对象,但我觉得你不想把它和你的问题混在一起

如果创建大小为23的数组,则最多可以有22个索引,这是可以双击的索引列中索引最高的列

int[23] doubleClickConnections = new int[23];
然后,您应该只定义具有双击连接的索引。所以2,6,10,14,18,22。像这样:

doubleClickConnections[2] = 5;
你的索引似乎有一种模式。左侧索引的形式为4n+2,右侧索引始终为4n+5。这个也可以用,但我把它留给你

完成此操作后,应该使用DataGridViewCellEventArgs类的属性、ColumnIndex和RowIndex。它们返回双击单元格的索引:

int columnToModify = doubleClickConnections[e.ColumnIndex]; //if e.ColumnIndex is 2, this sets the variable to 5 for example.

int valueToAssign = (new object[] {null, 1}).Contains(
    dataGridViewAcorn.Rows[e.RowIndex].Cells[e.ColumnIndex].Value
) ? 0 : 1;
dataGridViewAcorn.Rows[e.RowIndex].Cells[columnToModify].Value = valueToAssign;
这假设在要修改的单元格中输入0或1的逻辑总是相同的,如果单击的单元格为0,则输入1;如果单击的单元格为1,则输入0

如果要使用Dictionary对象执行第一部分:


请注意,我不是在电脑前,所以可能会有一些小问题,但这应该会给你一个想法。谢谢你,我已经研究了你的两个建议。使用第一个-当依赖单元格以1开头时,我遇到了问题。你原来的编辑不喜欢最后的第一部分?0 : 1; 我确信这是我错过的或我不理解的一些非常基本的东西。但是,单元格的原始状态为null或空,因此为null?1:0还是空?0:1在原始状态单元格上起作用,但只有一种方式。如果该值为1,则会将其还原为0,然后继续为1或0,具体取决于之后的情况。当在原始状态单元格上测试为空时,所有内容都独立工作,并解决了我所陷入的小混乱中的很大一部分。cheers.int columnToModify=双击连接[e.ColumnIndex]//例如,如果e.ColumnIndex为2,则将变量设置为5。dataGridViewAcorn.Rows[e.RowIndex]。单元格[columnToModify]。值=dataGridViewAcorn.Rows[e.RowIndex]。单元格[e.ColumnIndex]。值==null?1 : 0;因此,最初双击的单元格未设置为0或1,但为空?如果单元格值为空或不是0或1,则计划是什么?从空到1
Dictionary<int, int> doubleClickConnections = new Dictionary<int, int>(6) {
    { 2, 5 },
    { 6, 9 },
    ...
};
doubleClickConnections[2] //which equals 5