English 中文(简体)
无法调制数据绑定组合框
原标题:Can t databinding combobox

Sorry for my new question, but I have problem: I want to add combobox items with databinding, but see NullException ("Object reference not set to an instance of an object."), but I have data in table. This is my code:

BesenicaDataSet ds = new BesenicaDataSet();
ds.ReadXml("BesenicaDS.xml");
comboBox1.DataSource = ds.Tables["Kategorii"].DefaultView;
comboBox1.DisplayMember = "Kategoriq";

更新:

现在,我用这个修了它 无法破例

comboBox1 = new ComboBox(); 
comboBox1.DataSource = ds.Tables["Kategorii"].DefaultView; 
comboBox1.DisplayMember = "Kategoriq"; //second column in table 
comboBox1.ValueMember = "KategoriqID"; //Primary Key column 

但是现在ComboBox1.Trounds.Counts.Count = = 0,但我在这个桌子上有东西。为什么和如何修复它?

问题回答

很难用您提供的内容来描述。 设置一个断点, 并查看您的 DataSet 中是否有数据。 如果选中的话, 显示成员是正确拼写, 还是表格? 我注意到它们非常相似, 但用一个字母关闭。 检查该表是否存在( 设置为监视器或 Hover ) 。 更多信息会有所帮助 。

假设文件 xml 存在且有效,则唯一可能的原因是

comboBox1.DataSource = ds.Tables["Kategorii"].DefaultView; 

表示您在 DataSet 中没有名为“ Kategorii” 的表格

试试这个

DataTable dt = ds.Tables["Kategorii"];
if(dt == null)
    throw new Exception("There is no datatable Kategorii");
else
    comboBox1.DataSource = dt;

我敢打赌,无论出于什么原因, DataSet 中没有任何名为“ Kategorii” 的表格, 这会导致在尝试访问 < code> DefaultView 时出现无效引用例外。 请检查您的 xml, 在调试条件下检查 DataSet 中哪些表格。

如果你能给我们这个例外的堆叠追踪器 和你的Xml文件的样本 会有帮助

另一个猜想是 comboBox1 本身无效吗?

Bonus points
I just wanted to check that you re deliberately not setting ValueMember on the combobox, or simply omitted it from this example code.

看来你漏掉了以下几行:

comboBox1.DataBind();




相关问题
Bring window to foreground after Mutex fails

I was wondering if someone can tell me what would be the best way to bring my application to the foreground if a mutex was not able to be created for a new instance. E.g.: Application X is running ...

How to start WinForm app minimized to tray?

I ve successfully created an app that minimizes to the tray using a NotifyIcon. When the form is manually closed it is successfully hidden from the desktop, taskbar, and alt-tab. The problem occurs ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

Handle DataTable.DataRow cell change event

I have a DataTable that has several DataColumns and DataRow. Now i would like to handle an event when cell of this DataRow is changed. How to do this in c#?

Apparent Memory Leak in DataGridView

How do you force a DataGridView to release its reference to a bound DataSet? We have a rather large dataset being displayed in a DataGridView and noticed that resources were not being freed after the ...

ALT Key Shortcuts Hidden

I am using VS2008 and creating forms. By default, the underscore of the character in a textbox when using an ampersand is not shown when I run the application. ex. "&Goto Here" is not ...

WPF-XAML window in Winforms Application

I have a Winforms application coded in VS C# 2008 and want to insert a WPF window into the window pane of Winforms application. Could you explain me how this is done.

热门标签