C#的形式与对照结合的datagridview到数据表 [英] c# binding datagridview to datatable in form vs control
本文介绍了C#的形式与对照结合的datagridview到数据表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下的code:
的DataGridView lGrid =新的DataGridView();
BindingSource的_bind =新的BindingSource();
数据表表=新的DataTable(); this.Controls.Add(lGrid);
lGrid.AutoGenerateColumns = TRUE;
清单<串GT; ColumnsNames =新的List<串GT;();
ColumnsNames.Add(ID);
ColumnsNames.Add(NAME); 的foreach(在ColumnsNames字符串名称)
Table.Columns.Add(姓名); 山坳的DataColumn = Table.Columns [ID];
DataColumn的[]键=新的DataColumn [1];
键[0] = Table.Columns [ID];
Table.PrimaryKey =键; lGrid.DataSource = _bind;
_bind.DataSource =表; INT I = lGrid.Columns.Count;
该填充lGrid在数据表中的列只是这个code形式的构造函数执行罚款。然而,当我将它移动到控制构造绑定不工作,我= 0,为什么会这样,我可以做些什么呢?
UPDATE1
确定。构造函数是最简单的
公共部分Form1类:表格
{
公共Form1中()
{
的InitializeComponent();
// code GOES HERE
}
}
VS
公共类mycontrol:控制
{
公共mycontrol()
{
// code GOES HERE
}
}
公共部分Form1类:表格
{ 公共Form1中()
{
的InitializeComponent(); mycontrol LL =新mycontrol();
this.Controls.Add(Ⅱ);
}
}
解决方案
使用想如果你
lDataGrid.Bind();
那么你会得到伯爵()
或检查的AfterDataBinding事件计数。
I have the following code:
DataGridView lGrid = new DataGridView();
BindingSource _bind = new BindingSource();
DataTable Table = new DataTable();
this.Controls.Add(lGrid);
lGrid.AutoGenerateColumns = true;
List<string> ColumnsNames = new List<string>();
ColumnsNames.Add("ID");
ColumnsNames.Add("NAME");
foreach (string Name in ColumnsNames)
Table.Columns.Add(Name);
DataColumn col = Table.Columns["ID"];
DataColumn[] keys = new DataColumn[1];
keys[0] = Table.Columns["ID"];
Table.PrimaryKey = keys;
lGrid.DataSource = _bind;
_bind.DataSource = Table;
int i = lGrid.Columns.Count;
which populates lGrid with columns in datatable just fine with this code executing in the form constructor. However, when I move it to the control constructor binding doesn't work and i = 0. Why is it so and what can i do about it?
Update1
OK. the constructors are the most simple
public partial class Form1 : Form {
public Form1()
{
InitializeComponent();
//CODE GOES HERE
}
}
VS
public class mycontrol : Control
{
public mycontrol()
{
//CODE GOES HERE
}
}
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
mycontrol ll = new mycontrol();
this.Controls.Add(ll);
}
}
解决方案
Use think if you
lDataGrid.Bind();
then you will get the Count()
or check the Count on the AfterDataBinding Event.
这篇关于C#的形式与对照结合的datagridview到数据表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文