Reputation: 93
i try to add values in dropdownlist in gridview query works fine but it appears as this ..
gridview html
<asp:BoundField HeaderText="ApproveID" DataField="ApproveID" ></asp:BoundField>
<asp:TemplateField>
<ItemTemplate>
<asp:Label ID="lblCountry" runat="server" Text='<%#
Eval("ApproveID") %>' Visible = "false" />
<asp:DropDownList ID="DropDownList4" runat="server"
class="vpb_dropdown">
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
code
protected void OnRowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
//Find the DropDownList in the Row
DropDownList ddlCountries = (e.Row.FindControl("DropDownList4") as
DropDownList);
ddlCountries.DataSource = GetData("SELECT ApproveID,ApproveType FROM
ApproveType");
ddlCountries.DataTextField = "ApproveType";
ddlCountries.DataValueField = "ApproveID";
ddlCountries.DataBind();
//Add Default Item in the DropDownList
ddlCountries.Items.Insert(0, new ListItem("Please select"));
//Select the Country of Customer in DropDownList
//string country = (e.Row.FindControl("lblCountry") as Label).Text;
//ddlCountries.Items.FindByValue(country).Selected = true;
}
}
values are not inside in dropdownlist ..how to show values in dropdown?? and when i debug the code it cant show me any error
getdata code
private DataSet GetData(string query)
{
string conString =
ConfigurationManager.ConnectionStrings["mydms"].ConnectionString;
SqlCommand cmd = new SqlCommand(query);
using (SqlConnection con = new SqlConnection(conString))
{
using (SqlDataAdapter sda = new SqlDataAdapter())
{
cmd.Connection = con;
sda.SelectCommand = cmd;
using (DataSet ds = new DataSet())
{
sda.Fill(ds);
return ds;
}
}
}
Upvotes: 0
Views: 1481
Reputation: 2985
After your last comment you should check if the DataSet
contains the record you want ot set as selected:
if (ddlCountries.Items.FindByValue(country) != null)
{
ddlCountries.Items.FindByValue(country).Selected = true;
}
Upvotes: 0
Reputation: 1549
Set
DropDownList1.SelectedValue = "Some Value";
then you will get the value as default
Upvotes: 1
Reputation: 332
In a project I participated we returned the DataTable instead of DataSet and it was working fine with dropdowns. We had code like this:
if( ds.Tables.Count == 1)
return ds.Tables[0];
else
return new DataTable();
Besides I would change the way of databinding. In my opinion using ObjectDataSource is a better approach becuase the event is called only when the data is needed and you don't have to do checks like this:
if (e.Row.RowType == DataControlRowType.DataRow)
Upvotes: 0
Reputation: 26
Get data function returns null or empty value so it wont through error or exception while debugging.Your code working fine better check data in the database.
Upvotes: 0