【问题标题】:Copy multiple selected rows from one grid to another将多个选定行从一个网格复制到另一个网格
【发布时间】:2019-03-29 18:21:05
【问题描述】:

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

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

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

【问题讨论】:

  • 在我看来,您一遍又一遍地从 gridView4 复制同一行。在您填写DataRow r 的地方,您应该使用下一个选定的行,我在您的代码中看不到这一点

标签: c# winforms devexpress-gridcontrol


【解决方案1】:

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

【讨论】:

  • 我在行代码 DataRow[] doubles = EmplDT.Select("Matricule == " + row[0].ToString()) 上收到此错误“'=' operator.'之前缺少操作数。” ; 如果我跳过这个错误,我会在代码 EmplDT.Rows.Add(row) 上得到另一个“该行已经属于另一个表”;我在我的问题中添加了一些信息
  • 糟糕,它应该只有一个 '=` 而不是两个。我编辑了我的答案。正确的行是DataRow[] doubles = EmplDT.Select("Matricule = " + row[0].ToString());
  • 另外,如果 Matricule 列是 varchar,则值应该用单引号括起来
  • 我还添加了对第二个错误This row already belongs to another table的修复
  • 现在我有另一个问题,双打检查有时不起作用。双打检查失败后,我在 DataRow[] doubles = EmplDT.Select("Matricule = " + rowGridView4[0].ToString ());我收到此错误“范围对象中的最小值 (1) 必须小于或等于最大值 (-1)。'”。我采用解决方案两个跳过双打
猜你喜欢
  • 1970-01-01
  • 2013-02-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-04-16
  • 2014-01-21
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多