gpt4 book ai didi

c# - Win form DataGridView 动态添加按钮列

转载 作者:太空宇宙 更新时间:2023-11-03 18:01:18 25 4
gpt4 key购买 nike

我想在填充后向 DataGridView 动态添加一个按钮列。添加后按钮列可见,但是当我尝试遍历 DataGridView 行时,每个单元格中的按钮都为 null。

var buttonCol = new DataGridViewButtonColumn();
buttonCol.Name = "ButtonColumnName";
buttonCol.HeaderText = "Header";
buttonCol.Text = "Button Text";

dataGridView.Columns.Add(buttonCol);

foreach (DataGridViewRow row in dataGridView.Rows)
{
var button = (Button)row.Cells["ButtonColumnName"].Value;
// button is null here!
}

最佳答案

使用这个:

foreach (DataGridViewRow row in dataGridView.Rows) 
{
// DataGridViewButtonCell button = (row.Cells["ButtonColumnName"] as DataGridViewButtonCell);
row.Cells["ButtonColumnName"].Value = "ButtonText";
}

关于c# - Win form DataGridView 动态添加按钮列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/964135/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com