C# 用c语言进行gridview编程
如何使用c#代码在gridview中获取多个选定行&这些选定行必须以另一种形式显示,这种形式也具有gridviewC# 用c语言进行gridview编程,c#,winforms,C#,Winforms,如何使用c#代码在gridview中获取多个选定行&这些选定行必须以另一种形式显示,这种形式也具有gridview公共部分类WindowForm:form public partial class WindowForm: Form { private DataTable dataTable = new DataTable(); //This will contain all the selected rows. private List<
公共部分类WindowForm:form
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;
}
}
}
}
{
私有数据表=新数据表();
//这将包含所有选定的行。
私有列表selectedRows=新列表();
公共窗体()
{
初始化组件();
dataTable.Columns.Add(“Column1”);
dataTable.Columns.Add(“Column2”);
dataTable.Columns.Add(“Column3”);
对于(int i=0;i<30;i++)
{
添加(i,“行”+i.ToString(),“项”+i.ToString());
}
dataGridView1.DataSource=dataTable;
//这将选择网格的整行
dataGridView1.SelectionMode=DataGridViewSelectionMode.FullRowSelect;
//这将允许多重选择
dataGridView1.MultiSelect=true;
dataGridView1.CurrentCellChanged+=新事件处理程序(dataGridView1\u CurrentCellChanged);
dataGridView1.CellBeginEdit+=新的DataGridViewCellCancelEventHandler(dataGridView1\u CellBeginEdit);
}
void dataGridView1_CellBeginEdit(对象发送方,DataGridViewCellCancelEventArgs e)
{
执行选择(dataGridView1,selectedRows);
}
void dataGridView1\u CurrentCellChanged(对象发送方,事件参数e)
{
if(selectedRows.Contains(dataGridView1.CurrentRow))
{
selectedRows.Remove(dataGridView1.CurrentRow);
}
其他的
{
selectedRows.Add(dataGridView1.CurrentRow);
}
执行选择(this.dataGridView1,selectedRows);
}
私有void性能选择(DataGridView dgv,列表selectedRowsCollection)
{
foreach(dgv.Rows中的DataGridViewRow dgvRow)
{
如果(selectedRowsCollection.Contains(dgvRow))
{
dgvRow.Selected=true;
}
其他的
{
dgvRow.Selected=false;
}
}
}
}
Pavan您是否获得解决方案?实际上,此列表将包含所有选定行,然后设置另一个数据网格的数据源,该数据网格将显示您选定的行