添加与行相关的按钮
问题描述:
我试图将两个按钮添加到具有值2的行,但这些按钮只出现在与dataGridView相关的位置。添加与行相关的按钮
如果我不给他们一个特定的位置,他们会出现在dataGridView的顶部,当我给他们一个特定的位置时,他们仍然会出现在与dataGridView相关的位置。
我该如何让它们出现在每个有价值2的行上?
我想让按钮出现在特定行的底部或内部。我一直在寻找答案很长一段时间,我找不到任何东西。
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
,如MSDN thread所示。使用您的自定义按钮列,您现在可以根据需要隐藏按钮。
这看起来是这个问题的一个很好的选择。 Ty MikeH – Andre 2014-11-03 19:55:04