将GridView绑定到动态数据表 [英] Binding GridView to dynamic DataTable
问题描述
我有一个GridView,我将它绑定到正在被运行时填充的DataTable。列数和行数不固定。 DataTable填充后,我将GridView的绑定源设置为这个数据表:
Me.User_infoDTBindingSource.DataSource = User_infoDT
现在的问题是,网格视图根本不显示任何列或行。
我用过:
pre $ Me $ User $ G $ View $
现在我得到了GridView中的所有列,但是我在DataTable中设置的列标题都丢失了。 GridView中的行ar仍然是空的。我也使用了 OptionView.EnableAppearanceEvenRow
,所以我可以看到在我的GridView中生成的行数(它应该是这样),但仍然看不到任何数据。
我使用相同的数据源(数据表)实现了与Windows DataGridView和TreeList相同的代码,并且它工作正常。但是在这里我找不到任何没有得到这些行的原因。
你知道这些行在哪里吗?当我将绑定源的数据源设置为数据表时,我调试了代码并检查了DataTable。那时GridView.DataRowCount和GridView.Columns.Count都是正确的。但仍然网格视图不显示行中的任何数据。
UPDATE:
我现在已经移除绑定源并将GridView的数据源直接设置为DataTable它的工作完美!我很惊讶为什么当我在两者之间使用绑定源时行是不可见的。除此之外,这里现在所有的列都有正确的标题(我在DataTable中设置),当我使用绑定源时,这些标题正在丢失。
我得到了答案。我添加了以下几行:
Me.UserListGridView.Columns.Clear()
Me.UserListGridView.OptionsBehavior.AutoPopulateColumns = True
Me.User_infoDTBindingSource.DataSource = _user_info.User_infoDT
Me.User_infoDTBindingSource.RaiseListChangedEvents = True
Me.User_infoDTBindingSource.ResetBindings(True)
设置数据源后,提高列表更改事件并重置绑定可解决问题。
I have a GridView and I am binding it to a DataTable which is being filled at run time. Number of columns and rows are not fixed. After the DataTable is filled I am setting the binding source of the GridView to this data table:
Me.User_infoDTBindingSource.DataSource = User_infoDT
Now the prblem is, gird view is not showing any columns or rows at all. I used :
Me.UserListGridView.PopulateColumns(User_infoDT)
Now I am getting all the columns in the GridView but the column captions which I setted in DataTable are all lost. The rows ar still empty in the GridView. I have also used OptionView.EnableAppearanceEvenRow
so I can see that number of rows are generating in my GridView (as it is supposed to be) but still I can't see any data.
I have implemented the same code with Windows DataGridView and TreeList using the same data source (data table) and it is working perfectly. But here I can not find any reason of not getting the rows.
Any idea where are these rows? I debugged the code and checked the DataTable when I am setting the binding source's data source to data table. At that time GridView.DataRowCount and GridView.Columns.Count are correct. But still grid view is not showing any data in the rows.
UPDATE: I have now removed the binding source and setted the GridView's datasource directly to the DataTable and it is working perfectly!! I am amazed why the rows are invisble when I am using a binding source in between. Apart from this, here now all the columns have correct captions also (which I setted in DataTable) which are being lost when I am using binding source.
Well I got the answer. I added following lines:
Me.UserListGridView.Columns.Clear()
Me.UserListGridView.OptionsBehavior.AutoPopulateColumns = True
Me.User_infoDTBindingSource.DataSource = _user_info.User_infoDT
Me.User_infoDTBindingSource.RaiseListChangedEvents = True
Me.User_infoDTBindingSource.ResetBindings(True)
After setting the datasource, raising the list changed events and resetting binding resolved the issue.
这篇关于将GridView绑定到动态数据表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!