正如我在评论中已经提到的,你应该使用 aDataSource
作为你DataGridView
的 s 并且DataGridView
只使用 a 作为视图。
在这里,您有完整的示例演示如何将 aDataTable
用作DataSource
aDataGridView
并在它们之间移动 a DataRow
。
- 创建一个新的 windows 窗体项目并复制粘贴代码。
- 单击一行,方法是单击行中的左侧(用小箭头 > 表示)
DataGridView
- 单击
Button
下面的DataGridView
将选定的移动DataRow
到另一个DataGridView
注意 1:所有控件都在 Form1_Load 中创建,所以不要费心在表单上拖放控件。
注意 2:如果您只选择了一列,则不会移动任何行。您必须选择整行。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
this.Load +=Form1_Load;
}
public DataGridView ViewA, ViewB;
public DataTable DataA, DataB;
public Button MoveSelectedFromViewAToB, MoveSelectedFromViewBToA;
private void Form1_Load(object sender, EventArgs e)
{
//Create two DataGridViews on the form
ViewA = new DataGridView()
{
Location = new Point(0, 0),
Size = new Size(300, 100),
MultiSelect = false
};
ViewB = new DataGridView()
{
Location = new Point(300, 0),
Size = new Size(300, 100),
MultiSelect = false
};
this.Controls.Add(ViewA);
this.Controls.Add(ViewB);
//Add Two buttons
Button MoveSelectedFromViewAToB = new Button()
{
Text = "A => B",
Location = new Point(10, 120),
};
MoveSelectedFromViewAToB.Click += MoveSelectedFromViewAToB_Click;
MoveSelectedFromViewBToA = new Button()
{
Text = "A <= B",
Location = new Point(310, 120)
};
MoveSelectedFromViewBToA.Click += MoveSelectedFromViewBToA_Click;
this.Controls.Add(MoveSelectedFromViewAToB);
this.Controls.Add(MoveSelectedFromViewBToA);
//Make sure the form has appropriate size
this.Size = new Size(600, 200);
//Create a DataTable and add some data
DataA = new DataTable();
DataA.Columns.Add("Key", typeof(System.String));
DataA.Columns.Add("Value", typeof(System.String));
DataA.Rows.Add(new object[] { "KeyA", "ValueA" });
DataA.Rows.Add(new object[] { "KeyB", "ValueB" });
DataA.Rows.Add(new object[] { "KeyC", "ValueC" });
DataA.Rows.Add(new object[] { "KeyD", "ValueD" });
//Make sure DataB has the same layout as DataA
DataB = DataA.Clone();
//Assign both datatables to the views
ViewA.DataSource = new BindingSource() { DataSource = DataA };
ViewB.DataSource = new BindingSource() { DataSource = DataB };
}
/// <summary>
/// Moves rows from view A to B
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
void MoveSelectedFromViewAToB_Click(object sender, EventArgs e)
{
if (ViewA.SelectedRows.Count == 0 || DataA.Rows.Count == 0 || ViewA.SelectedRows[0].Index > DataA.Rows.Count -1) return;//No row selected, or data table is empty
DataB.ImportRow(DataA.Rows[ViewA.SelectedRows[0].Index]);
DataA.Rows.RemoveAt(ViewA.SelectedRows[0].Index);
}
/// <summary>
/// Moves rows from view B to A
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
void MoveSelectedFromViewBToA_Click(object sender, EventArgs e)
{
if (ViewB.SelectedRows.Count == 0 || DataB.Rows.Count == 0 || ViewB.SelectedRows[0].Index > DataB.Rows.Count -1) return; //No row selected, or data table is empty
DataA.ImportRow(DataB.Rows[ViewB.SelectedRows[0].Index]);
DataB.Rows.RemoveAt(ViewB.SelectedRows[0].Index);
}
}
}
有了这个,你应该能够实现你的逻辑,什么时候把一行移到你的grid_detail
,什么时候把它移回grid_display
.