【发布时间】:2011-09-12 16:49:33
【问题描述】:
如何在数据网格视图中为一行中的单元格而不是整个列添加一个按钮?
【问题讨论】:
标签: c# vb.net visual-studio
如何在数据网格视图中为一行中的单元格而不是整个列添加一个按钮?
【问题讨论】:
标签: c# vb.net visual-studio
我认为阿德里安的回答很接近。试试这样的。
if ((string)table.Rows[0].Cells[0].Value == "I should be a button") {
// you can add formatting or values to the button before assigning it here
table.Rows[0].cells[0] = new DataGridViewButtonCell();
}
【讨论】:
我认为如果在这里找到最好的答案: Hide gridview button。您需要做的就是在需要按钮的地方添加 DataGridViewButtonCell,在不需要的地方添加 DataGridViewTextBoxCell。该列必须是 DataGridViewButton 类型。
【讨论】:
在 SO 中看到这个类似的帖子,可能会有所帮助
如果赢得表格,请查看此 MSDN 帖子
Column Types in the Windows Forms DataGridView Control
或者这个代码项目帖子......虽然它给出了添加图像按钮的示例
DataGridView Image Button Cell
@tmax 在这种情况下,您可能可以将按钮创建代码放在GridView_RowCreated 事件中,如下所示
void GridView_RowCreated(Object sender, GridViewRowEventArgs e)
{
if(e.Row.RowType == DataControlRowType.Header)
{
//Button creation code here
}
}
【讨论】:
private void dataGridView1_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e)
{
Button btn = new Button();
//btn attributes
dataGridView1.Rows[0].Cells[3].Value = new Button();
}
试试这样的。
【讨论】:
我最终做的是将一个 DataGridView 堆叠在另一个之上。我关闭了边框、网格线和滚动条。然后创建动态按钮列以仅与一行按钮匹配主datagridview。然后我使用 ColumnWidthChanged 事件处理程序一起调整两个 DataGridViews 的大小。无论如何,这是我现在的解决方法。
【讨论】:
DataGridViewButtonColumn dataGridViewButtonColumn = new DataGridViewButtonColumn();
dataGridViewButtonColumn.Name = "Select";
dataGridViewButtonColumn.HeaderText = "Select";
dataGridViewButtonColumn.ReadOnly = false;
dataGridView1.Columns.Add(dataGridViewButtonColumn);
【讨论】:
我不确定这是否会有所帮助,但您也可以考虑使用 TableLayoutPanel。
【讨论】: