7

当我尝试数据时 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();
4

4 回答 4

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

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

另外如果以后带回多个字段,在绑定下拉列表时需要指定数据库中哪个字段是值,哪个是显示的文本。

ddlList.DataSource = sqlTa;
ddlList.DataValueField = "ValueFieldFromDatabaseResults";
ddlList.DataTextField = "ShownTextFieldFromDatabaseResults";
ddlList.DataBind();
于 2013-03-08T11:20:04.657 回答
2

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

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

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

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

一个例子是:

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

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

        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();
于 2013-03-08T11:20:19.717 回答
1

添加此行方式

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

然后你错过了连接字符串

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

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

添加myconn.open()

为了

 SqlConnection myConn="must add your connection string code here "
`myconn.open()`
 SqlDataAdapter sqlDa = new SqlDataAdapter(strQuery,myConn)
于 2013-03-08T11:54:04.397 回答
0

试试这个..

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

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

于 2013-03-08T11:22:14.933 回答