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.
LVL 1
doramail05Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

doramail05Author Commented:
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();
0
doramail05Author Commented:
solved with this:

private DataTable getSupplierSource()
    {
        DataTable dt3 = new DataTable();
        DataTable dt4 = Products.GetSupplier_DT();

        var counter = 0;
        for (int i = dt4.Rows.Count - 1; i >= 0; i--)
        {
            counter++;
            if (counter > 2) break;
            dt4.Rows.RemoveAt(i);
        }

        dt3 = Products.GetSupplier_DT();
        counter = 0;
        for (int i = dt3.Rows.Count - 1; i >= 0; i--)
        {
            counter++;
            if (counter > 2) break;
            DataRow tempRow = dt4.NewRow();
            FillDataRow(dt3.Rows[i], tempRow);
            dt4.Rows.InsertAt(tempRow, 0);
        }

        return dt4;
    }

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
doramail05Author Commented:
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

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.