0

I have two Janus GridEXs. When an specific button is clicked, I want to add rows of first grid to another grid; but I want to check that already these rows do'nt exist in the second grid, so I used this code block with linq:

    DataTable dtSelectedRows = new DataTable();
    dtSelectedRows = firstGrid.GetDataSource().Clone();
    foreach (GridEXRow row in rows)
    {
        DataRow dr = ((DataRowView)row.DataRow).Row;
        if (dtSelectedRows.AsEnumerable().Count() > 0)
        {        
            if (dtSelectedRows.AsEnumerable().Where(t => t.Field<Int32>("myColumn") == Convert.ToInt32(dr["myColumn"])).Count() == 0)
            {
                dtSelectedRows.ImportRow(dr);
            }
        }
        else
            dtSelectedRows.ImportRow(dr);

        }
    }
    secondGrid.SetDataSource(dtSelectedRows);

but unfortunately it did'nt work and dtSelectedRows is always empty. So I forced to rewrite the block as:

    GridEXRow[] rows = firstGrid.GetCheckedRows();
    DataTable dtSelectedRows = new DataTable();
    dtSelectedRows = firstGrid.GetDataSource().Clone();
    foreach (GridEXRow row in rows)
    {
        if (row.RowType == Janus.Windows.GridEX.RowType.Record)
        {        
            DataRow dr = ((DataRowView)row.DataRow).Row;
            bool rowExists = false;
            foreach (DataRow r in dtSelectedRows.Rows)
            {
                if (Convert.ToInt32(r["myColumn"]) == Convert.ToInt32(dr["myColumn"]))
                {
                    rowExists = true;
                    break;
                }
            }

            if (!rowExists)
                dtSelectedRows.ImportRow(dr);}
    }
    secondGrid.SetDataSource(dtSelectedRows);

and fortunately it just worked. So how can I correct the first code block?

Katy
  • 286
  • 2
  • 18
  • try `Convert.ToInt32(t["myColumn"])` instead of `t.Field("myColumn")`, also `Count()>0` is same `Any()`, `Count()==0` is same `!Any()` – Grundy Jan 11 '14 at 15:48

1 Answers1

0

try this :

     dtSelectedRows = firstGrid.GetDataSource().AsEnumerable().ToList();

instead of :

     dtSelectedRows = firstGrid.GetDataSource().Clone();
Iraj
  • 1,492
  • 5
  • 18
  • 42