2017-08-10 2 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'定義のあなたのタスクは、1つのライナーがある場合。それは基本的に短い形式です。うわー、あなた自身のコメントを削除することは非常に巧妙な考えです、私たちは愚か者のように見える。 –

+0

WinFormまたはWPFを使用していますか? –

+0

注意していただきありがとうございますが、中括弧なしで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; 
    } 
関連する問題