给出数据源后如何绑定datagridview? [英] How to bind datagridview after giving datasource?

查看:101
本文介绍了给出数据源后如何绑定datagridview?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好先生我有一个问题

i将我的数据绑定到datagridview就像这样





< pre lang =c#> dataGridView1.DataSource = dt1;





现在基于datagridview中的一列,我没有向datagridview添加任何列,例如









这是绑定数据源后绑定到动态列的列的编码



  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);
}
}



所以现在我真正的问题现在开始

现在从数据库我必须为此绑定值动态栏目

所以代码看起来很喜欢



  foreach (DataGridViewRow dgvr  in   this  .dataGridView1.Rows)
{
foreach (DataGridViewColumn dgvc in this .dataGridView1 .Columns)
{
string query3 = 从purchase_sr_details选择pcs,其中purchase_details_id =' + dataGridView1.Rows [dgvr.Index] .Cells [ 1 ]。值+ ';


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();
}


}


}


}
}





一切正常但我无法将值追溯到列没有显示任何检索到的动态列生成但值没有绑定对于那一个这个非常困惑我在这一天被困2天



更多可以在 http://stackoverflow.com/questions/19469031/how-to-bind-datagridview-after-giving-datasource [< a href =http://stackoverflow.com/questions/19469031/how-to-bind-datagridview-after-giving-datasourcetarget =_ blanktitle =New Window> ^ ]

解决方案

更改原始查询,以便获得动态列。让sql完成工作,而不是你的代码。这就是我想要做的事情。

如果那是不可能的,你需要获取网格绑定的数据表,将列添加到它(即DataColumns),并重新绑定到新的数据表。



我按照这两步完成了这一步:)


Hello sir i have one question
i am binding my data to datagridview like this


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

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

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[^]

解决方案

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 :)


这篇关于给出数据源后如何绑定datagridview?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆