在下拉列表中显示列表值

问题描述:

我想通过数据表在下拉列表中显示记录.一切正常,但下拉列表始终在运行时显示第一个索引值.但是,数据表中的记录是不同的.
在错误的地方查看我的代码:

I want to display the record in dropdownlist through datatable. Everything works fine, but the dropdownlist always show the first index value at runtime. However, the record in datatable is different.
See my code where I am wrong:

{
  nsBusinessLogicLayer.BusinessesInfo objBusinessesInfo = new nsBusinessLogicLayer.BusinessesInfo();
  DataTable dt = objBusinessesInfo.Select(Convert.ToInt64(LoginUserId));

  ddlCountyr.SelectedValue = dt.Rows[0]["StateId"].ToString();
}
if (dt.Rows[0]["CountryId"] != "")
{
  ddlState.SelectedValue = dt.Rows[0]["CountryId"].ToString();
}

ddlCountyr.SelectedValue = dt.Rows[0]["StateId"].ToString();


您已经在此处编写了代码!

您需要做的是:


You have written the code here for that!

What you need to do is:

ddlCountry.Datasource = dt;
ddlCountry.DataTextField = "CountryId";
ddlCountry.DataValuefield = "CountryId"; // can change it to anything
ddlCountry.DataBind();