通过DataTable设置DataSet后未正确设置项目 [英] Items not set correctly after setting a DataSet by a DataTable

查看:79
本文介绍了通过DataTable设置DataSet后未正确设置项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请查看以下代码:



Please look at the following code:

comboBox1.DropDownStyle = ComboBoxStyle.DropDown;
     comboBox1.AutoCompleteMode = AutoCompleteMode.None;
     comboBox1.AutoCompleteSource = System.Windows.Forms.AutoCompleteSource.ListItems;
     comboBox1.DisplayMember = comboBox1.ValueMember = "Note";
     var myConn = new SqlConnection("...");
     var sqlStr = "SELECT Note FROM ...";
     var dAdapter = new SqlDataAdapter(sqlStr, myConn);
     DataTable dTable = new DataTable();
     dAdapter.Fill(dTable);
     dAdapter.Dispose();
     comboBox1.DataSource = dTable;





comboBox1正确显示结果,但填充此字符串的项目为{System.Data .DataRowView}所以我不能以编程方式使用它们。

感谢您的帮助。



comboBox1 shows the result correctly but the Items filled with this string "{System.Data.DataRowView}" so i can't use them programmaticly.
Thanks for your help.

推荐答案

comboBox1.DropDownStyle = ComboBoxStyle.DropDown;
       SqlConnection conn = new SqlConnection(@"server=.\sqlexpress;database=test;Integrated Security=True");
       //conn.Open();
       comboBox1.AutoCompleteMode = AutoCompleteMode.None;
       comboBox1.AutoCompleteSource = System.Windows.Forms.AutoCompleteSource.ListItems;
       comboBox1.DisplayMember = comboBox1.ValueMember = "Note";

       DataTable t = new DataTable();
       SqlDataAdapter dap = new SqlDataAdapter("select note from notes",conn);
       dap.Fill(t);
       dap.Dispose();
       comboBox1.DataSource = t;



当你不设置


There's no problem in your code actually when you will not set

comboBox1.DisplayMember

属性时你的代码没有问题你会看看

property you will see

System.Data.DataRowView

但是你已经设置了那些。所以你的代码没有问题。再次构建并运行。希望它能正常工作

But you have set those .So there is no problem in your code.Build Again and run.Hope it should work fine


我投射项目并得到正确的结果:



I cast the item and get the correct result:

Quote:

var s =(comboBox1.Items [i] as DataRowView)[0];

var s = (comboBox1.Items[i] as DataRowView)[0];


这篇关于通过DataTable设置DataSet后未正确设置项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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