C#winform中如何给datagridview的每一行添加按钮?

2024-11-15 00:55:10
推荐回答(1个)
回答1:

添加Data GridView Button Column。

具体代码如下:

public Form1()
{
InitializeComponent();
this.Load += new EventHandler(Form1_Load);
}

List strSourec = new List { "1", "2" };

void Form1_Load(object sender, EventArgs e)
{
foreach (string item in strSourec)
{
DataGridViewButtonColumn Column1 = new DataGridViewButtonColumn();
Column1.HeaderText = item;
this.dataGridView1.Columns.Add(Column1);
}
DataGridViewRow dr = new DataGridViewRow();

for (int i = 0; i < strSourec.Count; i++)
{
DataGridViewButtonCell dgvbc = new DataGridViewButtonCell();
dgvbc.Value = strSourec[i];
dr.Cells.Add(dgvbc);