数据绑定一个DropDownList [英] Databind a dropdownlist

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

问题描述

当我尝试databing 的DropDownList ,得到这个: system.data.datarowview 结果
我该怎么错了?

 字符串strQuery =测试,日历选择其中UserD =项目;
 的SqlConnection的myconn;
 SqlDataAdapter的SQLDA =新SqlDataAdapter的(strQuery,的myconn);
 数据表sqlTa =新的DataTable(测试);
 da.Fill(sqlTa);
 ddlList.DataSource = sqlTa;
 ddlList.DataBind();


解决方案

 字符串strQuery =从日历凡UserD =测试选择项;

请注意,你需要使用字符串周围单引号,因为在你的code你没有完成inital串过,所以code其余刚刚成为strQuery的一部分。

此外如果您带回一个以上的领域在未来,当你绑定一个下拉列表,你需要指定哪个字段从数据库的价值和它显示的文本。

  ddlList.DataSource = sqlTa;
ddlList.DataValueField =ValueFieldFromDatabaseResults;
ddlList.DataTextField =ShownTextFieldFromDatabaseResults;
ddlList.DataBind();

When I try to databing dropdownlist, got this: system.data.datarowview
what do I wrong?

 string strQuery = "Select Item FROM Calendar Where UserD="Test";
 SqlConnection myConn;
 SqlDataAdapter sqlDa = new SqlDataAdapter(strQuery,myConn);
 DataTable sqlTa = new DataTable("Test");
 da.Fill(sqlTa);
 ddlList.DataSource = sqlTa;
 ddlList.DataBind();

解决方案

string strQuery = "Select Item FROM Calendar Where UserD='Test'";

Note you need to use single quotations around the string, as in your code you didn't finish the inital string ever, so the rest of the code just became part of strQuery.

In addition if you bring back more than one field in the future, when you bind a dropdown list you need to specify which field from the database is the value and which is the displayed text.

ddlList.DataSource = sqlTa;
ddlList.DataValueField = "ValueFieldFromDatabaseResults";
ddlList.DataTextField = "ShownTextFieldFromDatabaseResults";
ddlList.DataBind();

这篇关于数据绑定一个DropDownList的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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