C# 数据绑定下拉列表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15292735/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 16:19:18  来源:igfitidea点击:

Databind a dropdownlist

c#asp.netdatabasedata-binding

提问by Will_G

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

当我尝试 databing 时 dropdownlist,得到了这个:system.data.datarowview
我做错了什么?

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

采纳答案by Ryan McDonough

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.

请注意,您需要在字符串周围使用单引号,因为在您的代码中您没有完成初始字符串,因此其余代码只是成为 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();

回答by Stephen

You need to tell it what fields to use as value and text.

您需要告诉它哪些字段用作值和文本。

ddlList.DataSource = sqlTa;
ddlList.DataValueField = "ValueField";
ddlList.DataTextField = "TextField";
ddlList.DataBind();

And your select statement is missing a ". It should be:

并且您的 select 语句缺少一个“。它应该是:

"Select Item FROM Calendar Where UserD='Test'"

An Example being:

一个例子是:

As ryan pointed out if you are pulling back one field then you can just do:

正如 ryan 指出的那样,如果您要撤回一个领域,那么您可以这样做:

        DataTable dtTable = new DataTable();

        try
        {
            using (SqlConnection sqlConnection = new SqlConnection("Your connection"))
            {
                using (SqlCommand sqlCommand = new SqlCommand("Select Item FROM Calendar Where UserD='Test'", sqlConnection))
                {
                    sqlConnection.Open();

                    using (SqlDataReader sqlDataReader = sqlCommand.ExecuteReader())
                    {
                        dtTable.Load(sqlDataReader);
                        sqlDataReader.Close();
                    }
                }
            }
        }
        catch (Exception error)
        {
            throw error;
        }

        ddlList.DataSource = dtTable;
        ddlList.DataBind();

But if you have more then one field then you can do this:

但是,如果您有多个字段,那么您可以这样做:

        DataTable dtTable = new DataTable();

        try
        {
            using (SqlConnection sqlConnection = new SqlConnection("Your connection"))
            {
                using (SqlCommand sqlCommand = new SqlCommand("Select Item, id FROM Calendar Where UserD='Test'", sqlConnection))
                {
                    sqlConnection.Open();

                    using (SqlDataReader sqlDataReader = sqlCommand.ExecuteReader())
                    {
                        dtTable.Load(sqlDataReader);
                        sqlDataReader.Close();
                    }
                }
            }
        }
        catch (Exception error)
        {
            throw error;
        }

        ddlList.DataSource = dtTable;
        ddlList.DataValueField = "id";
        ddlList.DataTextField = "item";
        ddlList.DataBind();

回答by SamuraiHyman

Try this..

尝试这个..

 ddlList.DataSource = sqlTa;                 
 ddlList.DataTextField = "class";
 ddlList.DataBind();

adding ddList.Value="somefield"is optional

添加ddList.Value="somefield"是可选的

回答by SamuraiHyman

add this line way

添加这条线的方式

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

then u miss connection string for

那么你错过了连接字符串

SqlConnection myConn="must add your connection string code here "

You have not open connection string so

你还没有打开连接字符串所以

add myconn.open()

添加 myconn.open()

for

为了

 SqlConnection myConn="must add your connection string code here "
`myconn.open()`
 SqlDataAdapter sqlDa = new SqlDataAdapter(strQuery,myConn)