使用c sharp编程Gridview

本文关键字:Gridview 编程 sharp 使用 | 更新日期: 2023-09-27 17:52:58

如何使用c#代码在gridview中获得多个选定行&选中的行我需要在另一个表单中显示同样是gridview

使用c sharp编程Gridview

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;
                }
            }
        }
    }