通过单击数据网格选择多个项目

时间:2017-08-10 07:16:08

标签: c# datagrid

我使用datagrid选择要生成报告的项目。但是现在我需要能够在按住CTRL键时选择多个项目。我怎样才能完成这个

private void dgvPrint_CellClick(object sender, DataGridViewCellEventArgs e) {        
  this.Clear_Print();

  if (e.RowIndex < 0)
    return;
    this.mEmpId = Convert.ToInt32(this.dgvPrint.Rows[e.RowIndex].Cells["empid_Print"].FormattedValue);
    this.lblEmpPrint.Text = this.dgvPrint.Rows[e.RowIndex].Cells["Empname_Print"].FormattedValue.ToString();

    this.btnPrint.Enabled = true;
}

1 个答案:

答案 0 :(得分:0)

您可以尝试这样的事情

    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        // Set properties
        dataGridView1.MultiSelect = true;
        dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;

        // Create fictional rows since i dont have your datasource
        dataGridView1.Columns.Add("col1", "col1");
        dataGridView1.Columns.Add("col2", "col2");
        dataGridView1.Columns.Add("col3", "col3");

        int loop = 10;
        for (int i = 0; i < loop; i++)
        {
            DataGridViewRow dgr = new DataGridViewRow();
            dataGridView1.Rows.Add(dgr);
        }
    }

    // After button click (or any other event for that matter)
    private void button1_Click(object sender, EventArgs e)
    {
        // Get all selected rows
        foreach(DataGridViewRow dgvr in dataGridView1.SelectedRows)
        {
            // execute row
            cellClickFunction(dgvr);
        }
    }

    // Pass selected row to cellClickFunction (this is NOT the click event)
    private void cellClickFunction(DataGridViewRow dgvr)
    {
        // Your code
        this.Clear_Print();

        this.mEmpId = Convert.ToInt32(this.dgvr.Cells["empid_Print"].FormattedValue);
        this.lblEmpPrint.Text = this.dgvPrint.dgvr["Empname_Print"].FormattedValue.ToString();

        this.btnPrint.Enabled = true;
    }