1

I have this code that copy 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);

this is my code to create Columns in 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;
    }

i have two problems in this code:
the first one is when i run the code it only add the first record and then i get error message of duplicate .
the second one i want to show layout control only the first time.
thanks in advance and sorry for my english.

M.Bouabdallah
  • 530
  • 10
  • 29
  • It seems to me that you are copying the same row from gridView4 over and over again. Where you fill up your `DataRow r` you should use the next selected row and I dont see that in your code – GuidoG Oct 25 '18 at 09:04

1 Answers1

1

Looping thru selected rows from a devexpress gridview and getting a row can be much simpler like this

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

Please note that with testing for doubles at this place will cause all records to be copied until a duplicate is found. So after your error message there might be some records copied and some not.
Is that how you intended this ?

I would leave out the error message and just skip duplicate records. You can still show a message with howmany records where copied if you want.

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);
     }
}
GuidoG
  • 11,359
  • 6
  • 44
  • 79
  • i get this error "Missing operand before '=' operator.'' on line code DataRow[] doubles = EmplDT.Select("Matricule == " + row[0].ToString()); if i skip this error i get another one "This row already belongs to another table" on line code EmplDT.Rows.Add(row); i add some informatin to my question – M.Bouabdallah Oct 25 '18 at 18:03
  • Oops, it should be only one '=` and not two. I edited my answer. The correct line is `DataRow[] doubles = EmplDT.Select("Matricule = " + row[0].ToString());` – GuidoG Oct 26 '18 at 06:48
  • Also, if the column `Matricule` is varchar than the value should be in single quotes – GuidoG Oct 26 '18 at 06:50
  • I also added a fix for the second error `This row already belongs to another table` – GuidoG Oct 26 '18 at 07:01
  • now i have another problem the doubles check sometimes work sometimes not.after doubles check fails i make break point on DataRow[] doubles = EmplDT.Select("Matricule = " + rowGridView4[0].ToString());i get this error "Min (1) must be less than or equal to max (-1) in a Range object.'".i go with solution two skipping doubles – M.Bouabdallah Oct 26 '18 at 09:13
  • what is the datatype for the column `Matricule` – GuidoG Oct 26 '18 at 09:16
  • EmplDT.Columns.Add("Matricule");nvarchar – M.Bouabdallah Oct 26 '18 at 09:17
  • what is the datatype ? is it `int` or `string` or what ? – GuidoG Oct 26 '18 at 09:17
  • the datatype is nvarchar(string) – M.Bouabdallah Oct 26 '18 at 09:19
  • than it should be `DataRow[] doubles = EmplDT.Select("Matricule = '" + rowGridView4[0].ToString() + "'");` I edited my answer – GuidoG Oct 26 '18 at 09:20
  • Can the value be empty sometimes ? – GuidoG Oct 26 '18 at 09:21
  • it works like charm,thank you very much GuidoG you've been a big help – M.Bouabdallah Oct 26 '18 at 09:29
  • No problem glad I could help – GuidoG Oct 26 '18 at 09:30
  • sorry GuidoG ,one more time please .can i show LayoutControl one time, now if select five rows it show me five times – M.Bouabdallah Oct 26 '18 at 09:39
  • I never use `LayoutControl` so I have little knowledge about that, sorry – GuidoG Oct 26 '18 at 09:47