将一个SINGLE SELECTED行从datagridview复制到另一个datagridview

时间:2013-08-16 05:10:59

标签: c# datagridview

我需要做什么:  1.当用户点击数据视图的A ROW时。只有这一行显示在另一个datagridview中(以便可以编辑等...)

注意:grid_display =从我需要获取行的位置。 注意:grid_detail =我需要复制的单行。

我到现在为止:

   foreach (DataGridViewColumn c in grid_display.Columns)
            {
                grid_detail.Columns.Add(c.Clone() as DataGridViewColumn);
            }

            //then you can copy the rows values one by one (working on the selectedrows collection)
            foreach (DataGridViewRow r in grid_display.SelectedRows)
            {
                int index = grid_detail.Rows.Add(r.Clone() as DataGridViewRow);
                foreach (DataGridViewCell o in r.Cells)
                {
                    grid_detail.Rows[index].Cells[o.ColumnIndex].Value = o.Value;
                }
            }

            }

我只能创建一个新行。但不要在其中选择所需的行。 有什么帮助吗?

3 个答案:

答案 0 :(得分:2)

正如我在评论中提到的那样,您应该使用DataSource作为DataGridView并使用DataGridView作为视图。

此处您有一个完整的示例,演示了如何将DataTable用作DataSource DataGridView并在DataRow之间移动DataGridView

  1. Creata一个新的Windows窗体项目并复制粘贴代码。
  2. 点击一行,点击Button
  3. 中的行(用小箭头>表示)左边的行
  4. 点击DataGridView下方的DataRow,将选定的DataGridView移至另一个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); } } }
  5. 注意1:所有控件都是在Form1_Load中创建的,所以不要在表单上拖动删除控件。

    注意2:如果只选择了一列,则不会移动任何行。你必须选择整行。

    grid_detail

    有了这个,您应该能够实现何时将行移至grid_display以及何时将其移回{{1}}的逻辑。

答案 1 :(得分:0)

伙计们我必须解决这个问题。 我继续如下。

首先我克隆了列,为此我创建了一个方法

  public void change_header()
        {
            DisplayDataGrid();
            grid_detail.Columns.Clear();

            foreach (DataGridViewColumn c in grid_display.Columns)
            {
                grid_detail.Columns.Add(c.Clone() as DataGridViewColumn);
            }
        }

然后我只复制SELECTED行并将其显示在第二个datagridview中(因为标题列已被复制),现在显示的数据是可显示的。

我如何复制选定的行? 在这里。

 grid_detail.Rows.Clear(); 
            DataRow[] dRows = new DataRow[grid_display.SelectedRows.Count];
            for (int i = 0; i < grid_display.SelectedRows.Count; i++)
                dRows[i] = ((DataRowView)grid_display.SelectedRows[i].DataBoundItem).Row;
                foreach (DataRow dgvRow in dRows)
                {
                    grid_detail.Rows.Insert(0, dgvRow.ItemArray);
                }
                grid_detail.Refresh();

其中grid_display是带有FULL数据的dgv 和grid_detail是仅包含所选行的dgv。 感谢。

答案 2 :(得分:-1)

如果您的grid_display数据来自查询(如MySQL等),您可以做的是重做查询但使其具体化,以便结果数据与您单击时所选择的数据相同来自grid_display的单元格...然后将结果放入grid_detail。

这是我认为你可以做的一个解决方案......但是你不能一次做多次转移,因为你正在进行查询......

希望它有所帮助......:D