将多个选定行从一个网格复制到另一个网格

Copy multiple selected rows from one grid to another

我有这段代码可以将选定的行从一个网格复制到另一个网格

private void btnAddEmployee_Click(object sender, EventArgs e)
{
    LayoutControl lc = new LayoutControl();
    lc.Dock = DockStyle.Top;
    LookUpEdit userShift = new LookUpEdit();
    userShift.Properties.TextEditStyle = TextEditStyles.DisableTextEditor;
    userShift.Properties.DataSource = paint.GetShiftTime();
    userShift.Properties.DisplayMember = "ShiftTime";
    userShift.Properties.ValueMember = "id";
    userShift.Properties.ShowHeader = false;
    var date = DateTime.Now;

    if (8 < date.Hour && date.Hour < 16)
    {
        userShift.EditValue = 1;
    }
    else if (16 < date.Hour && date.Hour < 24)
    {
        userShift.EditValue = 2;
    }
    else
    {
        userShift.EditValue = 3;
    }

    lc.AddItem(Resources.workingHours, userShift).TextVisible = true;
    lc.Height = 50;
    this.Controls.Add(lc);
    this.Dock = DockStyle.Top;
    int[] selectedRows = gridView4.GetSelectedRows();

    for(int n=0;n< selectedRows.Length;n++)
    //foreach (int index in selectedRows)
    {
        if (DevExpress.XtraEditors.XtraDialog.Show(lc, Resources.options,                         MessageBoxButtons.OKCancel) == DialogResult.OK)
        { 
            //Prevent duplicate data
            for (int i = 0; i < gridView5.RowCount; i++)
            {
                if (gridView4.GetRowCellValue(gridView4.FocusedRowHandle, "Matricule").ToString() == gridView5.GetRowCellValue(i, "Matricule").ToString())
                {
                    XtraMessageBox.Show(Resources.employeeAlreadyAdded, Resources.error, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }
            }

            DataRow r = EmplDT.NewRow();
            r[0] = gridView4.GetRowCellValue(gridView4.FocusedRowHandle, "Matricule").ToString();
            r[1] = gridView4.GetRowCellValue(gridView4.FocusedRowHandle, "Employé").ToString();
            r[2] = userShift.Text;
            r[3] = userShift.EditValue;
            r[4] = txtDate.EditValue;
            EmplDT.Rows.Add(r);

这是我在 gridview 5 中创建列的代码

DataTable EmplDT = new DataTable();
 void CreateEmployeeTable()
    {
        EmplDT.Columns.Add("Matricule");
        EmplDT.Columns.Add("Employé");
        EmplDT.Columns.Add("Heure");
        EmplDT.Columns.Add("idShiftTime", typeof(Int32));
        EmplDT.Columns.Add("Date", typeof(DateTime));
        gridControl5.DataSource = EmplDT;
        gridView5.Columns["idShiftTime"].Visible = false;
        gridView5.Columns["Date"].Visible = false;
    }

我在这段代码中有两个问题:
第一个是当我 运行 代码时它只添加第一条记录然后我收到重复的错误消息。
第二个我只想第一次显示布局控件。
提前致谢,对不起我的英语。

从 devexpress gridview 循环遍历选定的行并获取一行可以像这样简单得多

int[] selectedRows = gridView4.GetSelectedRows();
for (int i = 0; i < selectedRows.Length; i++)
{
     // Get a DataRow and fill it with all values from the this selected row
     // This is where you went wrong, you kept using only the first selected row
     DataRow rowGridView4 = (gridView4.GetRow(selectedRows[i]) as DataRowView).Row;

     // Do a check for doubles here
     DataRow[] doubles = EmplDT.Select("Matricule = '" + rowGridView4[0].ToString() +"'");
     if (doubles.Length > 0)
     {
         XtraMessageBox.Show(Resources.employeeAlreadyAdded, Resources.error, MessageBoxButtons.OK, MessageBoxIcon.Warning);
         return;         
     }

     // fix for error  "This row already belongs to another table"
     DataRox row = EmplDT.NewRow();
     row[0] = rowGridView4[0];
     row[1] = rowGridView4[1];
     row[2] = userShift.Text;
     row[3] = userShift.EditValue;
     row[4] = txtDate.EditValue;

     EmplDT.Rows.Add(row);
}

请注意,在此位置测试双打将导致复制所有记录,直到找到重复记录。因此,在您的错误消息之后,可能会复制一些记录,而有些则没有。
这是你的本意吗?

我会省略错误消息并跳过重复记录。如果需要,您仍然可以显示一条消息,其中包含复制了多少条记录。

int[] selectedRows = gridView4.GetSelectedRows();
for (int i = 0; i < selectedRows.Length; i++)
{
     // Get a DataRow and fill it with all values from the this selected row
     // This is where you went wrong, you kept using only the first selected row
     DataRow rowGridView4 = (gridView4.GetRow(selectedRows[i]) as DataRowView).Row;

     // Do a check for doubles here
     DataRow[] doubles = EmplDT.Select("Matricule = '" + rowGridView4[0].ToString() + "'");
     if (doubles.Length == 0)
     {
         // fix for error  "This row already belongs to another table"
         DataRox row = EmplDT.NewRow();
         row[0] = rowGridView4[0];
         row[1] = rowGridView4[1];
         row[2] = userShift.Text;
         row[3] = userShift.EditValue;
         row[4] = txtDate.EditValue;

         EmplDT.Rows.Add(row);
     }
}