[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2018
  • Last Modified:

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.
0
doramail05
Asked:
doramail05
  • 3
1 Solution
 
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
 
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

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now