2017-08-10 84 views
0

我使用我的數據網格選擇要生成報告的項目。但是現在我需要按住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

他並不需要放置在那裏的'{}'括號如果你的'if'定義的任務是一個內襯。這基本上是一個簡短的表格。哇刪除自己的評論是一個非常聰明的想法,讓我們看起來像傻瓜。 –

+0

你使用WinForm還是WPF? –

+0

@ifconfig感謝您的注意,但你可以有一個沒有大括號的if語句 – karcs

回答

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; 
    } 
相關問題