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 a开发者_Go百科m 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()
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
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. 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
DropDownList.DataTextField will only work with on column/field of the object you're binding to it. 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.
- 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);
}
精彩评论