展开全部
public Form1()
{
InitializeComponent();
this.Load += new EventHandler(Form1_Load);
}
List<string> strSourec = new List<string> { "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());
}
参考:
http://zhidao.baidu.com/link?url=JImY3prv3dVBJlYSnTOIenUJnx6xyRRVFZYpnmg_flu755xA7RgADZtRswoKr1QMcCjClxWjmbHtRgsyFnZYya
不明白的再私信我
{
InitializeComponent();
this.Load += new EventHandler(Form1_Load);
}
List<string> strSourec = new List<string> { "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());
}
参考:
http://zhidao.baidu.com/link?url=JImY3prv3dVBJlYSnTOIenUJnx6xyRRVFZYpnmg_flu755xA7RgADZtRswoKr1QMcCjClxWjmbHtRgsyFnZYya
不明白的再私信我
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询