如何从我的SQL表中获取数据以显示在我的DropDownList中 [英] How do I get the data from my SQL Table to show up in my DropDownList

查看:68
本文介绍了如何从我的SQL表中获取数据以显示在我的DropDownList中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试了两种方法:



1-DataRow



我有3条数据但所有打印出来的都是MyProject.Cascading1的三倍



2-DataSet



到位SubFoldetails1我添加了ds.Tables [0]



这会产生三次System.Data.DataRowView



如何从我的SQL表中获取数据以显示在我的DropDownList中?



这是我的代码:



I have tried two methods:

1-DataRow

I have 3 pieces of data but all it prints out is three times "MyProject.Cascading1"

2-DataSet

In place of SubFoldetails1 I add ds.Tables[0]

This produces three times "System.Data.DataRowView"

How do I get the data from my SQL Table to show up in my DropDownList?

Here is my code:

    try
    {
        List<Cascading1> SubFoldetails1 = new List<Cascading1>();
        DataSet ds = new DataSet();
        using (SqlConnection conn = new SqlConnection(connStr))
        {
            using (SqlCommand cmd = new SqlCommand("Select [HomeURL] From [Home];", conn))
            {
                conn.Open();
                using (SqlDataAdapter da = new SqlDataAdapter(cmd))
                {
                    da.Fill(ds);
                    conn.Close();
                }
            }
        }
        foreach (DataRow DR in ds.Tables[0].Rows)
        {
            Cascading1 cs = new Cascading1();
            cs.HomeURL = DR["HomeURL"].ToString();
            SubFoldetails1.Add(cs);
        }
        DropDownList1.DataSource = SubFoldetails1;//=ds.Tables[0];
        DropDownList1.DataBind();
    }
    catch (Exception ex)
    {
        Label1.Text = ex.ToString();
    }
}

推荐答案





在这两种方法中,它将项目列表绑定到下拉控件,它不知道显示文本和值属性。



您需要设置DropDownList控件的DataTextField和DataValueField属性,以告知您要显示的文本和列表中当前项的基础值,以便在选择任何内容时返回从列表中。试试这个..

Hi,

In both method it bind list of items to drop down control, it does not know the display text and value properties.

You need to set DataTextField and DataValueField properties of DropDownList control to tell what text you want to display and underlying value of current item in list to return when you select any thing from list. Try with this..
foreach (DataRow DR in ds.Tables[0].Rows)
{
    Cascading1 cs = new Cascading1();
    cs.HomeURL = DR["HomeURL"].ToString();
    SubFoldetails1.Add(cs);
}
DropDownList1.DataSource = SubFoldetails1;//=ds.Tables[0];
DropDownList1.DataTextField = "HomeURL";
DropDownList1.DateValueField= "HomeURL";
DropDownList1.DataBind();





这将返回相同的 HomeUrl 值作为回报当您从下拉列表中选择项目时。



如果您希望显示不同的值并在选择任何项目时返回,您可以提取并将该值提供给DateValueField属性例如ID。因此,当您从列表中选择任何文本时,它将返回此值作为该项的键。



This will return you same HomeUrl value in return when you select item from drop down list.

If you want different value to be display and return when you select any item you can fetct and provide that value to DateValueField property e.g. ID. So when you select any text from list it will return you this value as key to that item.

DropDownList1.DataTextField = "HomeURL";
DropDownList1.DateValueField= "ID VALUE FOR CURRENT ITEM";


这篇关于如何从我的SQL表中获取数据以显示在我的DropDownList中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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