Binding values in Dropdownlist in Gridview at runtime is not a big task.We can do it using RowDatabound event.In the given example dropdownlist is being populated at the time of editing.
At first declare a dropdownlist control in EditItemTemplate.
<asp:TemplateField HeaderText="State" SortExpression="StateName" >
<EditItemTemplate>
<asp:DropDownList ID="ddlStateNm" runat="server" DataTextField="StateName" DataValueField="StateID"> </asp:DropDownList>
<asp:Label ID="lblStateID" runat="server" Text='<%# Bind("StateID") %>' Visible="false"></asp:Label>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblStateName" runat="server" Text='<%# Bind("StateName") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<EditItemTemplate>
<asp:DropDownList ID="ddlStateNm" runat="server" DataTextField="StateName" DataValueField="StateID"> </asp:DropDownList>
<asp:Label ID="lblStateID" runat="server" Text='<%# Bind("StateID") %>' Visible="false"></asp:Label>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblStateName" runat="server" Text='<%# Bind("StateName") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
Write a Function to bind DropDownList.
public void BindStateddl(DropDownList ddl)
{
SqlDataAdapter adp = new SqlDataAdapter("SELECT [StateID],[StateName] FROM [dbo].[MST_State] order by [StateName] asc", con);
DataSet ds = new DataSet();
adp.Fill(ds);
ddl.DataSource = ds;
ddl.DataBind();
ddl.Items.Insert(0, new ListItem("---Select---", "0"));
}
{
SqlDataAdapter adp = new SqlDataAdapter("SELECT [StateID],[StateName] FROM [dbo].[MST_State] order by [StateName] asc", con);
DataSet ds = new DataSet();
adp.Fill(ds);
ddl.DataSource = ds;
ddl.DataBind();
ddl.Items.Insert(0, new ListItem("---Select---", "0"));
}
Now the most important part the RowDataBound event. Here at first check for DataRow so that it will overlook HeaderRow and then check for EditIndex so that Databinding to DropDownList will be implemented to editble row only.
protected void gvCourt_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
if (gvCourt.EditIndex == e.Row.RowIndex) //to overlook header row
{
string StateId = ((Label)e.Row.FindControl("lblStateID")).Text;
DropDownList ddlGridState = (DropDownList)e.Row.FindControl("ddlStateNm");
BindStateddl(ddlGridState);
ddlGridState.SelectedValue = StateId;
}
}
}
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
if (gvCourt.EditIndex == e.Row.RowIndex) //to overlook header row
{
string StateId = ((Label)e.Row.FindControl("lblStateID")).Text;
DropDownList ddlGridState = (DropDownList)e.Row.FindControl("ddlStateNm");
BindStateddl(ddlGridState);
ddlGridState.SelectedValue = StateId;
}
}
}
hi anukana saha,
ReplyDeletethis is bharath.
i want to populate data in dropdownlist in asp gridview,from sql server data table only.
ex: i am having table country,and i am inserting data to states table with using country name, and i want to populate country data in dropdownlist in aspgridview in states form....,
Bharath..above article is fulfilling ur purpose...only u have to write a proper filtered query
ReplyDeletehi anukana saha,
ReplyDeletethis is bharath.
i want to populate data in dropdownlist in asp gridview,from sql server data table only.
ex: i am having table Districts,and i am inserting data to Distances form with using of aspgridview in dropdownlist,in that case District table data binding to dropdownlist..
could please help me...
Bharath..do u get success to fetch the data in normal dropdown?Bocz above example is fetching data from sql server database. At first bind a simple dropdown a/c to ur query..then read this article again.
DeleteHii Mam !! How I will Populate one dropdown list in value on selection of other dropdownlist value in GridView Without using Ajax.
Deletedrop down inside a grid should have default value as select when there is a null value from a table
ReplyDeletehi anukana saha,
ReplyDeletethis is bharath.
i have a dubug in Asp.Gridview , could u help me.
i am inserting data into gridview with using of templatefields, while i am inserting into the grid duplicate records how can i find ....
Hello Bharath,
DeleteUr ques is not clear...duplicacy must be checked on some certain feild...and after finding duplicates what u want to do.U can do it in various way according to ur requirement.
YES I M AGREE WITH ANUKANA FROM JAY
ReplyDeletehi anukana saha,
ReplyDeletethis is bharath.
how to get a month total dates in sql server 2008.
i want a result of like this
1-5-2013
2-5-2013
3-5-2013
4-5-2013
.
.
.
.
31-5-2013
Your Solution is here -:
Deletehttp://dotnetknowledgebox.blogspot.in/2013/05/return-all-dates-of-selected-month.html
Hi ,this is sankar,
ReplyDeleteafter populating that drop down list data, how can i set the default selected selected value into drop down .
like this,
Hello to everybody,
ReplyDeleteMy code raising an error for
"DropDownList ddlUserEmailid = (DropDownList)e.Row.FindControl("ddlUserEmailid");"
Error: Object reference not set to an instance of an object
I also tried this:
"DropDownList ddlUserEmailid = gridImport.FindControl("ddlUserEmailid") as DropDownList;"
Please suggest.
The DataGridView .NET control supports a variety of data sources, such as any XML, custom business object and web service, including dropdown list
ReplyDeleteI am pradeep from Delhi i need one help.i select one dropdownlist value and get the result in Gridview but i select another value in dropdown means that values in same page.. please reply me..Thanks.
ReplyDelete