C# 添加与行相关的按钮
我试图在值为2的行中添加两个按钮,但这些按钮仅与dataGridView相关 如果我没有给他们一个特定的位置,他们将出现在dataGridView的顶部,当我给他们一个特定的位置时,他们仍然会出现在dataGridView的相关位置 如何使它们相对于每个值为2的行显示 我想让按钮显示在底部或特定行的内部。我一直在寻找答案很长时间了,但我什么也找不到C# 添加与行相关的按钮,c#,C#,我试图在值为2的行中添加两个按钮,但这些按钮仅与dataGridView相关 如果我没有给他们一个特定的位置,他们将出现在dataGridView的顶部,当我给他们一个特定的位置时,他们仍然会出现在dataGridView的相关位置 如何使它们相对于每个值为2的行显示 我想让按钮显示在底部或特定行的内部。我一直在寻找答案很长时间了,但我什么也找不到 Button buttonOk = new Button() { Width = 100, Height = 50 }; Button button
Button buttonOk = new Button() { Width = 100, Height = 50 };
Button buttonCancel = new Button() { Width = 100, Height = 50 };
public void method1(DataGridView dataGridView1)
{
foreach (DataGridViewRow row in dataGridView1.Rows)
{
if ((string)row.Cells[2].Value == ("1"))
{
row.DefaultCellStyle.ForeColor = Color.Green;
}
else if ((string)row.Cells[2].Value == ("2"))
{
row.DefaultCellStyle.ForeColor = Color.Blue;
dataGridView1.Controls.Add(buttonOk);
dataGridView1.Controls.Add(buttonCancel);
//buttonOk.Location = new Point(00, 74);
}
else if ((string)row.Cells[2].Value == ("3"))
{
//do something
}
}
}
无法将按钮添加到
DataGridView
。但是,您可以将列定义为DataGridViewButtonColumn
。当然,问题是按钮将显示在所有行中。您可以实现一个自定义的DataGridViewButtonColumn
,如下所示。使用自定义按钮列,您现在可以根据需要隐藏按钮。这看起来是解决此问题的一个不错的选择。蒂米克