简体   繁体   English

如何在下拉列表中添加表字段的2列值

[英]how to add 2 column value of table field in dropdown

I have a dropdown. 我有一个下拉菜单。 I need to bind with two field of datatable value here 我需要在此处绑定数据表值的两个字段

ddlitem.datasource= dtitem
ddlitem.datatextfield = dtitem.cloumn["name"].tostring()+"-"+dtitem.cloumn["tagname"].tostring() 

I am doing like this I am getting exception here. 我这样做我在这里越来越例外。 How can I combine both the column value in dropdown 如何合并下拉列表中的两个列值

and when I retrieve the selected value of the dropdown 当我检索下拉菜单的选定值时

I should get only the value of this column value: dtitem.cloumn["name"].tostring() 我应该只获取此列值的值: dtitem.cloumn["name"].tostring()

string stritem=  ddlitem.selecteditem.Text.Tostrigng();

so how can I do the splitting the value of the string? 那么我该如何分割字符串的值呢?

you need to iterate 1 of the column rows and need to cancatinate your items rather whole column 您需要迭代列中的1行,并且需要对项目进行分类,而不是整个列

Edit : 编辑:

should be like... 应该像...

for (int count = 0; count < dtitem.Rows.Count; count++)
    {
        dtitem.Rows[count]["name"] = dtitem.Rows[count]["name"].tostring() + "-" + dtitem.Rows[count]["tagname"].tostring();
    }

I assume that dtitem is a DataRow object. 我假设dtitem是一个DataRow对象。 I recommend you to create a dictionary object and fill it like 我建议您创建一个字典对象并像这样填充它

Dictionary<string,string> dicts = new Dictionary<string,string>();

foreach(DataRow row in dt.Rows)
{
  string key = row["name"].ToString();
  string val = String.Format("{0}-{1}",row["name"],row["tagname"]);
}

then bind it to dropdown 然后将其绑定到下拉列表

dropdown.DataSource = dicts;
dropdown.DataValueField = "Key";
dropdown.DataTextField = "Value";
dropdown.DataBind();

when you get dropdown.SelectedValue , its already the name so you don't need to separate it from tagname 当您获得dropdown.SelectedValue ,它已经是name因此您无需将其与tagname分开

DropDownList.DataTextField will only work with on column/field of the object you're binding to it. DropDownList.DataTextField仅适用于您要绑定到其的对象的列/字段。 If you need to display multiples values, you have some options: 如果需要显示倍数值,则有一些选择:

  • Edit your query to concatenate the values before the object is passed to your dropdown for binding. 在将对象传递到下拉列表进行绑定之前,请编辑查询以连接值。
  • Create a collection that is derived from your dataobject, which concatenates the values and then bind THAT to your dropdown. 创建一个从数据对象派生的集合,该集合将这些值连接起来,然后将THAT绑定到您的下拉列表中。
  • Add each item to your dropdown programatically, concatenating the values as you go. 以编程方式将每个项目添加到您的下拉菜单中,并随身携带这些值。

Try using the following : 尝试使用以下内容:

public void Populate_DriverDropDown()
    {
        //Opening and closing connection not required while using SqlDataAdapter 

        //sql command to select the drivers and their ids (displaying both the drivername and firstSurname)
        String sqlQuery = "SELECT driverID, nameDriver+' '+firstSurname AS driverFullname FROM driver ORDER BY driverID ASC";
        //end

        //using SqlDataAdapter and DataSet
       SqlDataAdapter Da = new SqlDataAdapter(sqlQuery, DBConn);

        DataSet Ds = new DataSet();

        Da.Fill(Ds, "driver");

        if (Ds.Tables[0].Rows.Count > 0)
        {
            //populating each dropdown row with driver name and id.
            foreach (DataRow Dr in Ds.Tables[0].Rows)
            {
                VehicleDriver.Items.Add(new ListItem(Dr["driverFullname"].ToString(), Dr["driverID"].ToString()));
            }
        }

        //to have select option at the top of the dropdown list
        VehicleDriver.Items.Insert(0, "Select a driver");

    }
Dictionary<string,string> dicts = new Dictionary<string,string>();

foreach(DataRow row in dt.Rows)
{
  string key = row["name"].ToString();
  string val = String.Format("{0}-{1}",row["name"],row["tagname"]);
}

Please Rewrite this to 请重写为

Dictionary<string,string> dicts = new Dictionary<string,string>();

foreach(DataRow row in dt.Rows)
{
  string key = row["name"].ToString();
  string val = String.Format("{0}-{1}",row["name"],row["tagname"]);
  dicts.Add(key, val);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM