Link to home
Start Free TrialLog in
Avatar of doramail05
doramail05Flag for Malaysia

asked on

asp.net add and delete row programmatically in gridview

i have collected supplier ids in a list called
 List<long> lstSupplierIDs = Products.GetSupplierIDsByProductID(lgProductId);

have a supplier gridview with supplier id as row id

trying to remove rows that contains supplier ids that in the lstSupplierIDs first
and then add the existing suppliers in the lstSupplierIDs on TOP rows of the gridview which order by 'datemodified' and have them checkbox checked.

not sure how to use datarow add with the specification above.
Avatar of doramail05
doramail05
Flag of Malaysia image

ASKER

got the error in the bold : The given DataRow is not in the current DataRowCollection.

List<long> lstSupplierIDs = Products.GetSupplierIDsByProductID(lgProductId);

            DataTable dt3 = new DataTable();
            DataTable dt4 = Products.GetSupplier_DT();

           // DataTable dt4 = new DataTable();

            foreach (long lgSuppID in lstSupplierIDs)
            {
                dt3 = Products.GetSupplierRowDT(lgSuppID);
                DataRow dr = dt3.Rows[0];
                dt4.Rows.Remove(dr);
            }

            List<long> lstSupplierIDsDateModifiedDesc = Products.GetSupplierIDsByProductID_OrderByDateModified(lgProductId);

            DataTable dt = new DataTable();
           
            foreach (long lgSuppID in lstSupplierIDsDateModifiedDesc)
            {
                dt = Products.GetSupplierRowDT(lgSuppID);
                DataRow dr = dt.Rows[0];
                dt4.Rows.Add(dr);
            }

            gvSupplier.DataSource = dt4;
            gvSupplier.DataBind();

            udpSupplier.Update();
ASKER CERTIFIED SOLUTION
Avatar of doramail05
doramail05
Flag of Malaysia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
and the filldatarow :

    private void FillDataRow(DataRow valRow, DataRow targetRow)
    {
        foreach (DataColumn col in valRow.Table.Columns)
        {
            if (targetRow.Table.Columns.Contains(col.ColumnName))
            {
                targetRow[col.ColumnName] = valRow[col];
            }
        }
    }

Open in new window