windows Datagridview 不显示数据
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5584746/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Datagridview does not showing data
提问by MAC
I created one Windows application for displaying Gmail conversations in a datagrid. All are the items except Datagridview binding are working. I don't know what happened in Binding to the Gridview.. Please review my code snippet and give me a solution for resolving this issue. Thanks in advance.. My code is appending below...
我创建了一个 Windows 应用程序,用于在数据网格中显示 Gmail 对话。除了 Datagridview 绑定之外的所有项目都在工作。我不知道在 Binding to the Gridview 中发生了什么.. 请查看我的代码片段并为我提供解决此问题的解决方案。提前致谢..我的代码附加在下面......
GmailItem _gItem = null;
List<GmailItem> lstMail = new List<GmailItem>();
for (int i = 0; i < mailCount; i++)
{
_gItem = new GmailItem();
_gItem = client.GetMailItem(i);
lstMail.Add(_gItem);
}
_bindingMails.DataSource = lstMail;
dgMails.DataSource = _bindingMails;
And in designer page, this is the code for Datagridview
在设计器页面中,这是 Datagridview 的代码
this.dgMails.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
this.dgMails.Location = new System.Drawing.Point(6, 19);
this.dgMails.Name = "dgMails";
this.dgMails.Size = new System.Drawing.Size(504, 150);
this.dgMails.TabIndex = 0;
Also, i added this code in our .CS page
此外,我在我们的 .CS 页面中添加了此代码
dgMails.Dock = DockStyle.Fill;
dgMails.AutoGenerateColumns = true;
回答by Tergiver
1) Check that the BindingSource does not have a value for DataMember. The DataMember property tells the BindingSource to find a field/property with that name to get the IEnumerable from.
1) 检查 BindingSource 是否没有 DataMember 的值。DataMember 属性告诉 BindingSource 查找具有该名称的字段/属性以从中获取 IEnumerable。
2) Make sure that DataGridView.AutoGenerateColumns
is True. This property is True by default and is not visible on a PropertyGrid, so you would have had to set it False in your code behind.
2)确保这DataGridView.AutoGenerateColumns
是真的。此属性默认为 True,在 PropertyGrid 上不可见,因此您必须在后面的代码中将其设置为 False。
Updated
更新
3) Does GMailItem contain public properties? Fields do not work.
3) GMailItem 是否包含公共属性?字段不起作用。
回答by Marco
Besides JonH's comment, I'd like to point out that your code would read better this way:
除了 JonH 的评论之外,我想指出您的代码会以这种方式更好地阅读:
List<GmailItem> lstMail = new List<GmailItem>();
for (int i = 0; i < mailCount; i++)
{
lstMail.Add(client.GetMailItem(i));
}
_bindingMails.DataSource = lstMail;
dgMails.DataSource = _bindingMails;
回答by Rhapsody
If you don't need updating through the grid you could do:
如果您不需要通过网格进行更新,您可以执行以下操作:
dgMails.DataSource = lstMail; // Without the BindingSource
On the MSDN Examplethey bind the BindingSource to the DataGrid beforeadding data to the BindingSource
在MSDN 示例中,他们在将数据添加到 BindingSource之前将BindingSource 绑定到 DataGrid