0

我需要做什么: 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;
                }
            }

            }

我只能做一个新的行。但不要在其中输入选定的行。有什么帮助吗?

4

3 回答 3

2

正如我在评论中已经提到的,你应该使用 aDataSource作为你DataGridView的 s 并且DataGridView只使用 a 作为视图。

在这里,您有完整的示例演示如何将 aDataTable用作DataSourceaDataGridView并在它们之间移动 a DataRow

  1. 创建一个新的 windows 窗体项目并复制粘贴代码。
  2. 单击一行,方法是单击行中的左侧(用小箭头 > 表示)DataGridView
  3. 单击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.

于 2013-08-16T05:41:48.413 回答
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。谢谢。

于 2013-08-20T05:07:23.070 回答
-1

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

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

希望对您有所帮助... :D

于 2014-05-12T09:32:43.007 回答