0

As of now i can only display the 1 row but i also want to add the other row in my dropdown This is my code enter image description here

This is my code in text field and value field

DropTeam.DataSource = dtt;
DropTeam.DataValueField = "TeamDescr"+"TeamCode";
DropTeam.DataTextField = "TeamDescr"+"TeamCode";
DropTeam.DataBind();
4

3 回答 3

1

您必须在 Fill 函数中提供表名,并且 Fill 需要一个 DataSet 而不是 DataTable 所以它应该是这样的sd.Fill(ds,"Branch")

这里如何使用 DataSet 和 DataTable

于 2021-09-30T07:50:24.027 回答
0

好吧,一个典型的下拉菜单(组合)将让您选择一行。典型的组合框可以让您选择一排进行显示,然后“通常”选择 PK 排。

所以 PK 行选择应该保留 PK 行值,但是为了显示?与说访问组合不同,没有多列支持多于一行。

因此,您可以做的是简单连接附加列。

假设我们有酒店名称的下拉菜单。我们这样说:

SELECT ID, HotelName from tblHotels ORDER BY HotelName

要在下拉菜单中显示酒店名称和城市?

你可以这样:

SELECT ID, HotelName + ',' + City As MyHotel from tblHotels ORDER BY HotelName

掉落只会为您返回一个 PK 值 (ID)。然后,您可以从该值中获取/使用/派生该组合的实际城市名称。

所以标记:

        <h3>Select Hotel</h3>
        <asp:DropDownList ID="cboHotel2" runat="server"
            DataValueField="ID"
            DataTextField="MyHotel"  Width="288px"
            ></asp:DropDownList>

代码:

            string strSQL = "SELECT ID, HotelName, City, " +
                            "(HotelName + ' (' + City + ')') AS MyHotel " + 
                            "FROM tblHotels ORDER BY HotelName";

            cboHotel.DataSource = MyRst(strSQL);
            cboHotel.DataBind();

输出:

在此处输入图像描述

你可以添加一个固定的字体大小。(但下拉 HTML 将删除这些空白)。

但是,您可以通过填充 '\u00A0' 字符来解决该问题。

所以,让我们采用一个固定的字体,然后这样做:

        <asp:DropDownList ID="cboHotel" runat="server"
            DataValueField="ID"
            DataTextField="HotelName"  Width="288px"
            style="font-family:courier, 'courier new', monospace;"
            ></asp:DropDownList>

为了填充,我们这样做:

            strSQL = "SELECT ID, HotelName, City " +
                     "FROM tblHotels ORDER BY HotelName";

            DataTable rst = MyRst(strSQL);
            rst.Columns[1].MaxLength = 255;

            foreach (DataRow MyRow in rst.Rows)
            {
                MyRow["HotelName"] = ((String)MyRow["HotelName"]).PadRight(35, '\u00A0') +
                    " | " + MyRow["City"];
            }
            cboHotel.DataSource = rst;
            cboHotel.DataBind();

所以,我们只是预处理表数据,我们现在得到这个:

在此处输入图像描述

现在,任何选定的行将只返回 PK 值,然后您可以从该组合选择中获取城市名称、税率或其他任何内容。

第三种方式?您将“伪造”下拉菜单,并弹出(显示)包含所有列的网格视图。但是,除非发现上述两个想法中的任何一个都不够用,否则我不会发布该解决方案。

于 2021-09-30T11:31:03.080 回答
0

您可以使用一段时间:

    string mainconn = ConfigurationManager.ConnectionStrings["Ref"].ConnectionString;
    string sqlqueryy = "select * from Branch";
    using (SqlConnection connection = new SqlConnection(mainconn))
    {
        SqlCommand command = new SqlCommand(sqlqueryy, connection);
        connection.Open();
        SqlDataReader read = command.ExecuteReader();
        while (read.Read())
            {
                string TeamDescr= read["TeamDescr"].ToString();
                string TeamCode= read["TeamCode"].ToString();
                DropTeam.Items.Add(TeamCode + " - " + TeamDescr);
            };
        read.Close();
        connection.Close();
     }
于 2021-09-30T07:52:55.090 回答