C#winform中给datagridview的每一行添加按钮
答案:5 悬赏:0 手机版
解决时间 2021-12-03 07:25
- 提问者网友:欲望失宠
- 2021-12-02 11:00
C#winform中给datagridview的每一行添加按钮
最佳答案
- 五星知识达人网友:北城痞子
- 2021-12-02 12:21
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);
}
dataGridView1.Rows.Add(dr);
this.dataGridView1.CellMouseDown += new DataGridViewCellMouseEventHandler(dataGridView1_CellMouseDown);
}
void dataGridView1_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
{
if (dataGridView1[e.ColumnIndex, e.RowIndex].Value == null) return;
MessageBox.Show(dataGridView1[e.ColumnIndex, e.RowIndex].Value.ToString());
}
{
InitializeComponent();
this.Load += new EventHandler(Form1_Load);
}
List
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);
}
dataGridView1.Rows.Add(dr);
this.dataGridView1.CellMouseDown += new DataGridViewCellMouseEventHandler(dataGridView1_CellMouseDown);
}
void dataGridView1_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
{
if (dataGridView1[e.ColumnIndex, e.RowIndex].Value == null) return;
MessageBox.Show(dataGridView1[e.ColumnIndex, e.RowIndex].Value.ToString());
}
全部回答
- 1楼网友:何以畏孤独
- 2021-12-02 17:08
点击datagridview右上方的小箭头,编辑列——》添加列------类型选”DataGridViewButtonColumn“
- 2楼网友:你哪知我潦倒为你
- 2021-12-02 16:04
在datagridview控件的右上角有一个三角形的按钮,在里面添加的时候有个选项你选择DataGridViewButtonColumn就可以了
- 3楼网友:野慌
- 2021-12-02 15:24
二楼正解~三楼的方法适合动态加载行时添加按钮,但是没必要这么复杂,按照lz的意思应该是某一列是按钮
- 4楼网友:十年萤火照君眠
- 2021-12-02 13:46
在datagridview控件的右上角有一个三角形的按钮,在里面添加的时候有个选项你选择DataGridViewButtonColumn就可以了
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯