Tejas
Tejas

Reputation: 461

Inserting data dynamically in datagridview Cell

I added the column in data gridview dynamically, and I want to assign some value to it. I am using the following code...

        String qr = "SELECT Id, CONVERT(varchar, OrderItemId) AS Product, Quantity,CONVERT(varchar,CustomerId) AS Customer, ReturnRequestStatusId as'Return Request Status', CreatedOnUtc as Date FROM ReturnRequest";
        DataTable dtr = new DataTable();
        tp.populatedt(qr, dtr);
        dgvReturnRequests.DataSource = dtr;

        DataGridViewTextBoxColumn ord = new DataGridViewTextBoxColumn();
        ord.HeaderText = "Order #";
        ord.Name = "OrderCol";
        dgvReturnRequests.Columns.Insert(4, ord); // Here I added a new column

        foreach (DataGridViewRow dr in dgvReturnRequests.Rows)
        {
            string s = dr.Cells[1].Value.ToString();
            DataTable pn = new DataTable();
            tp.populatedt("Select p.pname,o.OrderId from Product p JOIN OrderItem o on p.Id=o.ProductId where o.Id='" + Convert.ToInt32(s) + "'", pn);
            foreach (DataRow dr1 in pn.Rows)
            {
                dr.Cells[1].Value = dr1["pname"].ToString();
                dr.Cells[4].Value = dr1["OrderId"].ToString(); // Here I am adding the value in a newly added column

            }
        }

Product Name(pname) is displayed, but the value of dr1["OrderId"].ToString() is not displaying in that cell...check the output in below image.

Output

How can I solve this?

Upvotes: 1

Views: 103

Answers (2)

Rajan Mishra
Rajan Mishra

Reputation: 1178

Maybe the reason is you didn't rebind the gridview control:

String qr = "SELECT Id, CONVERT(varchar, OrderItemId) AS Product, Quantity,CONVERT(varchar,CustomerId) AS Customer, ReturnRequestStatusId as'Return Request Status', CreatedOnUtc as Date FROM ReturnRequest";
DataTable dtr = new DataTable();
tp.populatedt(qr, dtr);
dgvReturnRequests.DataSource = dtr;

DataGridViewTextBoxColumn ord = new DataGridViewTextBoxColumn();
ord.HeaderText = "Order #";
ord.Name = "OrderCol";
dgvReturnRequests.Columns.Insert(4, ord);

foreach (DataGridViewRow dr in dgvReturnRequests.Rows)
{
    string s = dr.Cells[1].Value.ToString();
    DataTable pn = new DataTable();
    tp.populatedt("Select p.pname,o.OrderId from Product p JOIN OrderItem o on p.Id=o.ProductId where o.Id='" + Convert.ToInt32(s) + "'", pn);
    foreach (DataRow dr1 in pn.Rows)
    {
        dr.Cells[1].Value = dr1["pname"].ToString();
        dr.Cells[4].Value = dr1["OrderId"].ToString(); // Here I am adding the value in a newly added column
    }
    dtr.add(dr); // Function may differ in name. Maybe add a row.
}

dgvReturnRequests.DataSource=dtr;
dgvReturnRequests.DataBind();

Upvotes: 0

Nhan Phan
Nhan Phan

Reputation: 1302

You should make sure that dgvReturnRequests.Rows and pn.Rows are not empty. Otherwise, the loops are not reached and nothing is updated.

And in the end of your method, you should call dgvReturnRequests.Update(); to update the data grid view.

Upvotes: 1

Related Questions