绑定到数据表DataGridView的已经列定义 [英] Bind DataTable to Datagridview that already have column defined

查看:164
本文介绍了绑定到数据表DataGridView的已经列定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图绑定 DateTable DataGridView的已经有使用列在设计师设计的VS.对于数据表
来源是SQL数据库。
我试图做到这一点使用下面的代码添加了在DataGridView中只有空白行。

  dataGridView1.AutoGenerateColumns = FALSE ; //列

禁用自动生成数据表DT =新的DataTable();

的foreach(COL的DataGridViewColumn在dataGridView1.Columns)
{
dt.Columns.Add(col.Name);
}
DT = get_data_table(选择MYTABLE *);
dataGridView1.DataSource = DT;


解决方案

DataGridView的列有属性命名 DataPropertyName 只需将它们设置为你的数据表列名和你设置。

 的foreach(在dataGridView1.Columns的DataGridViewColumn列)
{
dt.Columns.Add(col.Name);
col.DataPropertyName = col.Name;
}

这应该工作。


I am trying to Bind DateTable to Datagridview that already have columns designed using Designer in VS. Source for DataTable is sql database. I am trying to do this using following code which adds only blank rows in datagridview.

dataGridView1.AutoGenerateColumns = false;  // Disable autogeneration of columns

DataTable dt = new DataTable();

foreach (DataGridViewColumn col in dataGridView1.Columns)
{
    dt.Columns.Add(col.Name);
}
dt = get_data_table("select * from Mytable");
dataGridView1.DataSource = dt;

解决方案

DataGridView columns have property named DataPropertyName just set them to your DataTable column names and you are set.

foreach (DataGridViewColumn col in dataGridView1.Columns)
{
    dt.Columns.Add(col.Name);
    col.DataPropertyName = col.Name;
}

this should work.

这篇关于绑定到数据表DataGridView的已经列定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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