Click here to Skip to main content
15,891,905 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello sir i have one question
i am binding my data to datagridview like this


C#
dataGridView1.DataSource = dt1;


Now based upon one of the column in datagridview i am adding no of columns to datagridview for example




this is the coding of binding columns to dynamic columns after binding datasource

C#
for (int j = 0; j < i; j++)
     {
    if (j >= (dataGridView1.Columns.Count - 10))
    {
      DataGridViewColumn col = new DataGridViewTextBoxColumn();
     col.DataPropertyName = "";
     col.HeaderText = j.ToString();
     col.Name = j.ToString();
       dataGridView1.Columns.Add(col);
      }
     }

So now my real question starts now
now from database i have to bind values for this dynamic columns
so code looks likes

C#
foreach (DataGridViewRow dgvr in this.dataGridView1.Rows)
                      {
                          foreach (DataGridViewColumn dgvc in this.dataGridView1.Columns)
                          {
                              string query3 = "select pcs from purchase_sr_details where purchase_details_id='" + dataGridView1.Rows[dgvr.Index].Cells[1].Value + "'";


                              if (dgvc.Index > 8)
                              {
                                  DataTable dt2 = connection.getexecuted(query3);
                                  if (dt2.Rows.Count > 0)
                                  {

                                      for (int i = 0; i < dt2.Rows.Count; i++)
                                      {
                                          dataGridView1.Rows[dgvr.Index].Cells[""+i+""].Value = dt2.Rows[i]["pcs"].ToString();
                                      }


                                  }


                              }


                          }
                      }


All is working fine but i can't retrivew values to columns nothing displays nothing retrieved dynamic columns generated but values not binded to that one this one is very confused i am stuck 2 days in this one

More can be found at http://stackoverflow.com/questions/19469031/how-to-bind-datagridview-after-giving-datasource[^]
Posted
Updated 19-Oct-13 8:14am
v3

1 solution

Change your original query so that you get the dynamic columns as well. Let sql do the work, not your code. That's what I would try and do.
If that is not possible you need to get the datatable that the grid is binding to, add your columns to it (i.e. DataColumns), and rebind to the new datatable.

I follow this two steps and that's done :)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900