DataGridView的行出现,但没有数据 [英] DataGridView rows show up but no data

查看:122
本文介绍了DataGridView的行出现,但没有数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想绑定的集合(从继承的BindingList)到一个DataGridView。网格标题显示了罚款和我得到我期望的行数。然而,细胞是空的。任何人都有这个问题?如果是这样,你是怎么解决的呢?我已经说过了别人谁有同样的问题,但他们不记得他们是如何解决它。我试图创建一个简单的版本,显示问题,但还没有任何运气。所以,我很抱歉,我没有任何包含code。

I am trying to bind a collection (that inherits from BindingList) to a DataGridView. The grid headers show up fine and the I get the number of rows that I expect. However, the cells are empty. Has anyone else had this problem? If so, how did you resolve it? I've talked to someone else who had the same problem but they can't remember how they resolved it. I've tried to create a simple version that shows the problem but haven't had any luck. So I'm sorry, I haven't included any code.

编辑#1:

我不认为这code会帮助,但以防万一。我从有概述我们code的47层简化为prevent的事情。但是就像我说的,我不能像这样一个很简单的例子再现问题。我真的不想要code分析 - 智慧从那些谁碰到这个问题空谈。当然,我不是唯一的一个。

I don't think this code will help but just in case. I have simplified things in order to prevent from having to outline the 47 layers of our code. But like I said, I can't recreate the problem with a very simple example like this. I'm not really wanting code analysis--just words of wisdom from those who have run into this problem. Surely I'm not the only one.

public interface ISearchResultCollection : IList<ISearchResult>
{
...
}

public class SearchResultCollection : BindingList<ISearchResult>, ISearchResultCollection
{
...
}

public interface ISearchResult
{
  ILineNum LineNumber {get; set;}
  string Text {get; set;}
}

public class SearchResult
{
...
}

ISearchResultCollection results = objectToSearch.Find("searchstring");
dataGridView1.DataSource = results;

编辑#2:

我想我已经得到了领先优势。所有那些重新presents一个项目(ISearchResult)我的界面上的公共属性是接口类型为好。我添加了一个字符串属性和它的数据被奇迹般地显示出来。因此,在上述的例子中,文字列的数据将显示出来。不过,该行号列的数据不会因为它是一个接口类型(ILineNum)的。我想通的ToString()将被调用来填充这些网格。现在任何想法?

I think I've got a lead. All of the public properties on my interface that represents an item (ISearchResult) are interface types as well. I added a string property and its data is magically showing up. So, in the above example, the Text column's data would show up. But, the LineNumber column's data would not since it is of an interface type (ILineNum). I figured ToString() would be called to populate the grid on these. Any ideas now?

推荐答案

在数据绑定对象的一些控制,所以你得到一个体面的字符串重新$ P $的数据绑定接口会自动调用的ToString()你的对象psentation在任何控制要绑定到。在DataGridView的情况下,你将看到底层对象的ToString'd重新presentation在相应的单元格。但是,当您进行数据绑定到一个接口,ToString方法没有这样的电话()制成。这意味着你的DataGridView将显示一个空单元,但这是非常误导,因为绑定的对象实际上是有,因为它应该是 - 它只是没有在单元格中显示任何值

When you databind an object to some control, the databinding interfaces will automatically call "ToString()" on your object so you get a decent string representation in whatever control you are binding to. In the case of a DataGridView you will see the ToString'd representation of the underlying object in the appropriate cell. However, when you databind to an interface, no such call to ToString() is made. This means that your DataGridView will display an empty cell but this is quite misleading as the bound object is actually there as it should be - it just doesn't have any value to display in the cell.

要解决这个问题,你需要处理的显示值自己的格式。在一个DataGridView,这可以通过处理CellFormatting事件来实现。此事件被触发,只要任何细胞都需要,所以你一定要小心,不要放太多沉重的逻辑此处理它的值设置格式。在你的情况,你需要做的是底层对象随叫随到的ToString()。所以,你可以处理CellFormatting事件再使用这样的事情在你的处理程序:

To fix this you need to deal with the formatting of a display value yourself. In a DataGridView, this can be achieved by handling the CellFormatting event. This event is fired whenever any cell needs to format its value so you must be careful not to put too much heavy logic in this handler. In your case, all you need to do is call "ToString()" on the underlying object. So you can handle the CellFormatting event then use something like this in your handler:

private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
    if (e.Value != null && dataGridView1.Columns[e.ColumnIndex] == theRelevantColumn)
    {
        e.Value = e.Value.ToString();
    }
}

在执行逻辑之前,我们检查的特定列,但是这是可选的上述片段,你自己的情况可能会有所不同。要注意的要点是,我们得到的事件参数传递的原始对象和所有我们需要做的是设置格式化值回到事件参数。在这种情况下,是通过调用的ToString()底层对象上产生的格式的值。在你的DataGridView,你现在会在适当的单元格中看到该格式化值。

In the above snippet we are checking for a specific column before performing the logic but this is optional, your own scenario may be different. The main point to note is that we get the original object passed in on the event args and all we need to do is set the formatted value back in the event args. In this case, the formatted value is generated by calling "ToString()" on the underlying object. In your DataGridView, you will now see this formatted value in the appropriate cell.

希望有所帮助。

这篇关于DataGridView的行出现,但没有数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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