我将DataGridView创建到TableLayoutPanel中。 那么,如何为每个datagridview右击事件? 这是Dynamic datagridview创建源代码
public void makeDataGridView(int num)
{
for (int i = 0; i < num; i++)
{
DataGridView[] dgv = new DataGridView[num];
dgv[i] = new DataGridView();
dgv[i].Name = "dgv" + i.ToString();
tableLayoutPanel1.Controls.Add(dgv[i]);
}
}
答案 0 :(得分:0)
你可以试试这个
public void makeDataGridView(int num)
{
for (int i = 0; i < num; i++)
{
DataGridView[] dgv = new DataGridView[num];
dgv[i] = new DataGridView();
dgv[i].Name = "dgv" + i.ToString();
dgv[i].MouseDown += onMouseDown;
tableLayoutPanel1.Controls.Add(dgv[i]);
}
}
private void onMouseDown(object sender, MouseEventArgs e)
{
//var dgv = sender as DataGridView;
if (e.Button == MouseButtons.Right)
{
//perform task ...
}
}