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

时间:2018-10-25 07:56:49

标签: c# winforms devexpress-gridcontrol

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

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;
    }

我在此代码中有两个问题:
第一个是当我运行代码时,它仅添加第一条记录,然后出现重复的错误消息。
第二个我只想第一次显示布局控件。
在此先感谢您,我的英语很抱歉。

1 个答案:

答案 0 :(得分:0)

像这样,从devexpress网格视图中遍历选定的行并获取行可以更简单

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);
     }
}