GridView控件编程用c锋利

问题描述:

如何使用选择的行我有另一种形式也有gridview的GridView控件编程用c锋利

public partial class WindowForm: Form 
    { 
     private DataTable dataTable = new DataTable(); 
     //This will contain all the selected rows. 
     private List<DataGridViewRow> selectedRows = new List<DataGridViewRow>(); 

     public WindowForm() 
     { 
      InitializeComponent(); 
      dataTable .Columns.Add("Column1"); 
      dataTable .Columns.Add("Column2"); 
      dataTable .Columns.Add("Column3"); 
      for (int i = 0; i < 30; i++) 
      { 
       dataTable .Rows.Add(i, "Row" + i.ToString(), "Item" + i.ToString()); 
      } 
      dataGridView1.DataSource = dataTable ; 
      //This will select full row of a grid 
      dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect; 
      //This will allow multi selection 
      dataGridView1.MultiSelect = true; 

      dataGridView1.CurrentCellChanged += new EventHandler(dataGridView1_CurrentCellChanged); 
      dataGridView1.CellBeginEdit += new DataGridViewCellCancelEventHandler(dataGridView1_CellBeginEdit); 
     } 

     void dataGridView1_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e) 
     { 
      PerformSelection(dataGridView1, selectedRows); 
     } 

     void dataGridView1_CurrentCellChanged(object sender, EventArgs e) 
     { 
      if (selectedRows.Contains(dataGridView1.CurrentRow)) 
      { 
       selectedRows.Remove(dataGridView1.CurrentRow); 
      } 
      else 
      { 
       selectedRows.Add(dataGridView1.CurrentRow); 
      } 
      PerformSelection(this.dataGridView1, selectedRows); 

     } 

     private void PerformSelection(DataGridView dgv, List<DataGridViewRow> selectedRowsCollection) 
     { 
      foreach (DataGridViewRow dgvRow in dgv.Rows) 
      { 
       if (selectedRowsCollection.Contains(dgvRow)) 
       { 
        dgvRow.Selected = true; 
       } 
       else 
       { 
        dgvRow.Selected = false; 
       } 
      } 
     } 
    } 
+0

帕做u得到您的解决方案,以显示C#代码&我得到的GridView多选择行? – TechGiant 2011-04-22 17:49:27

+0

+1的例子 – 2011-04-22 17:50:21

+0

其实这个列表将包含所有选定行,然后设置另一个数据网格,将显示您所选行 – TechGiant 2011-04-22 17:50:32