【问题标题】:Button in DataGridView with condition C#带有条件 C# 的 DataGridView 中的按钮
【发布时间】:2016-11-08 14:10:12
【问题描述】:

我想将按钮添加到 DataGridView 中的列...但有一个条件:当 ROW 的颜色为红色时, 这是数据网格视图:

现在,“结帐”列中的所有按钮都可以使用...与红色行或白色行...我的代码:

int row = (int)this.dataGridView1.CurrentCell.OwningRow.Cells[0].Value;

            if (MessageBox.Show("Check-out?",
                              "Message de confirmation",
                              MessageBoxButtons.YesNo) == DialogResult.Yes)
            {    MessageBox.Show("Success!!");
}

仅按钮与 Red ROW 一起使用, 我试试这段代码:

 if (dataGridView1.SelectedRows[0].DefaultCellStyle.BackColor == Color.Red)
        {
            int row = (int)this.dataGridView1.CurrentCell.OwningRow.Cells[0].Value;

            if (MessageBox.Show("Check-out?",
                              "Message de confirmation",
                              MessageBoxButtons.YesNo) == DialogResult.Yes)
            {MessageBox.Show("Success!!");
}

我有错误:索引超出限制。它不能是负数,并且必须小于集合的大小。排队:

 if (dataGridView1.SelectedRows[0].DefaultCellStyle.BackColor == Color.Red)

错误:

谢谢,

【问题讨论】:

    标签: c# datagridview colors conditional-statements


    【解决方案1】:

    我找到了正确答案:我应该修改这一行:

    if (dataGridView1.SelectedRows[0].DefaultCellStyle.BackColor == Color.Red)
    

    与:

     if(dataGridView1.CurrentRow.DefaultCellStyle.BackColor==Color.Red)
    

    所以我们使用 DataGridView.CurrentRow

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-05-29
      • 1970-01-01
      • 2023-03-25
      • 1970-01-01
      • 1970-01-01
      • 2021-12-16
      • 1970-01-01
      相关资源
      最近更新 更多