winforms 数据网格视图中的C#按钮名称

vjrehmav  于 2022-11-17  发布在  C#
关注(0)|答案(1)|浏览(150)

我想改变按钮的背面颜色。我知道这一点:button1.BackColor = System.Drawing.Color.Yellow;
但是我不知道如何从DataGridView获取按钮的名称。我尝试了这个方法,但是没有效果

string butttonn = "table"+c.ToString(); 
butttonn.BackColor = System.Drawing.Color.Yellow;

如果按钮名称已存在,我想将按钮背景颜色更改为绿色,如果按钮名称不存在,则将按钮背景颜色更改为红色
我不懂英语。我希望我能表达自己的意思。

sbtkgmzw

sbtkgmzw1#

谢谢你们,我修复了这个:

// MASA AÇIK MI KAPALI MI ?
        string[] aktifmasalar = new string[dataGridView2.Rows.Count - 1];
        
        for (int c = 0; c < dataGridView2.Rows.Count - 1; c++)
        {
            aktifmasalar[c] = dataGridView2.Rows[c].Cells["Masaad"].Value.ToString();
        }

       
            foreach (DataGridViewRow rows in dataGridView2.Rows)
            {
                if (rows.Cells["Masaad"].Value.ToString() == "MASA 1")
                {
                    masa1.BackColor = System.Drawing.Color.GreenYellow;
                    continue;
                }
                else if (rows.Cells["Masaad"].Value.ToString() == "MASA 2")
                {
                    masa2.BackColor = System.Drawing.Color.GreenYellow;
                    continue;
                }
                else if (rows.Cells["Masaad"].Value.ToString() == "MASA 3")
                {
                    masa3.BackColor = System.Drawing.Color.GreenYellow;
                    continue;
                }
                else if (rows.Cells["Masaad"].Value.ToString() == "MASA 4")
                {
                    masa4.BackColor = System.Drawing.Color.GreenYellow;
                    continue;
                }
                else if (rows.Cells["Masaad"].Value.ToString() == "MASA 5")
                {
                    masa5.BackColor = System.Drawing.Color.GreenYellow;
                    continue;
                }
                else if (rows.Cells["Masaad"].Value.ToString() == "MASA 6")
                {
                    masa6.BackColor = System.Drawing.Color.GreenYellow;
                    continue;
                }
                else if (rows.Cells["Masaad"].Value.ToString() == "MASA 7")
                {
                    masa7.BackColor = System.Drawing.Color.GreenYellow;
                    continue;
                }
                else if (rows.Cells["Masaad"].Value.ToString() == "MASA 8")
                {
                    masa8.BackColor = System.Drawing.Color.GreenYellow;
                    continue;
                }
                else if (rows.Cells["Masaad"].Value.ToString() == "MASA 9")
                {
                    masa9.BackColor = System.Drawing.Color.GreenYellow;
                    continue;
                }
                else if (rows.Cells["Masaad"].Value.ToString() == "MASA 10")
                {
                    masa10.BackColor = System.Drawing.Color.GreenYellow;
                    continue;
                }
                else if (rows.Cells["Masaad"].Value.ToString() == "MASA 11")
                {
                    masa11.BackColor = System.Drawing.Color.GreenYellow;
                    continue;
                }
                else if (rows.Cells["Masaad"].Value.ToString() == "MASA 12")
                {
                    masa12.BackColor = System.Drawing.Color.GreenYellow;
                    continue;
                }
                else if (rows.Cells["Masaad"].Value.ToString() == "MASA 13")
                {
                    masa13.BackColor = System.Drawing.Color.GreenYellow;
                    continue;
                }
                else if (rows.Cells["Masaad"].Value.ToString() == "MASA 14")
                {
                    masa14.BackColor = System.Drawing.Color.GreenYellow;
                    continue;
                }
                else if (rows.Cells["Masaad"].Value.ToString() == "MASA 15")
                {
                    masa15.BackColor = System.Drawing.Color.GreenYellow;
                    continue;
                }
                else if (rows.Cells["Masaad"].Value.ToString() == "MASA 16")
                {
                    masa16.BackColor = System.Drawing.Color.GreenYellow;
                    continue;
                }
                else if (rows.Cells["Masaad"].Value.ToString() == "MASA 17")
                {
                    masa17.BackColor = System.Drawing.Color.GreenYellow;
                    continue;
                }
                else if (rows.Cells["Masaad"].Value.ToString() == "MASA 18")
                {
                    masa18.BackColor = System.Drawing.Color.GreenYellow;
                    continue;
                }
                else if (rows.Cells["Masaad"].Value.ToString() == "MASA 19")
                {
                    masa19.BackColor = System.Drawing.Color.GreenYellow;
                    continue;
                }
                else if (rows.Cells["Masaad"].Value.ToString() == "MASA 20")
                {
                    masa20.BackColor = System.Drawing.Color.GreenYellow;
                    continue;
                }

           }

相关问题