数据绑定下拉列表 [英] Databind a dropdownlist

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

问题描述

当我尝试对 dropdownlist 进行数据处理时,得到了这个:system.data.datarowview
我怎么了?

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'";

请注意,您需要在字符串周围使用单引号,因为在您的代码中您没有完成初始字符串,因此其余代码只是成为 strQuery 的一部分.

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();

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

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