如何将鼠标右键单击事件动态创建datagridview

时间:2015-07-13 04:50:51

标签: c# wpf wpfdatagrid

我将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]);
        }
    }

1 个答案:

答案 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 ...
    }
}