RadioButtonList DropDownList In GridView Edit Mode In ASP.NET

In this example i'm explaining how to use DropDownList Or RadioButtonList In GridView Edit Mode EditItemTemaplate In ASP.NET Using C# VB.NET

RadioButton and DropDOwnList are selected in edit mode based on value saved in DataBase



HTML markup of aspx page is mentioned below
<asp:GridView ID="GridView1" runat="server" DataKeyNames="ID" 
              AutoGenerateColumns="False" 
              DataSourceID="SqlDataSource1" 
              OnRowDataBound="GridView1_RowDataBound" 
              OnRowUpdated="GridView1_RowUpdated" 
              OnRowUpdating="GridView1_RowUpdating" 
              OnRowEditing="GridView1_RowEditing">
 <Columns>
 <asp:TemplateField HeaderText="ID">
 <ItemTemplate>
 <asp:Label ID="lblID" runat="server" Text='<%#Eval("ID") %>'>
 </asp:Label>
 </ItemTemplate>
 </asp:TemplateField>
 
 <asp:BoundField DataField="Name" HeaderText="Name" 
                 SortExpression="Name" />
 <asp:TemplateField HeaderText="Gender">
 <ItemTemplate>
 <asp:Label ID="lblGender" runat="server" 
            Text='<%#Eval("Sex") %>'>
 </asp:Label>
 </ItemTemplate>
 <EditItemTemplate>
 <asp:RadioButtonList ID="rbGenderEdit" runat="server">
 <asp:ListItem>Male</asp:ListItem>
 <asp:ListItem>Female</asp:ListItem>
 </asp:RadioButtonList>
 </EditItemTemplate>
 </asp:TemplateField>
 
 <asp:TemplateField HeaderText="Marital Status">
 <ItemTemplate>
 <asp:Label ID="lblStatus" runat="server" 
            Text='<%#Eval("MaritalStatus") %>'>
 </asp:Label>
 </ItemTemplate>
 <EditItemTemplate>
 <asp:DropDownList ID="ddlStatusEdit" runat="server">
 <asp:ListItem>Single</asp:ListItem>
 <asp:ListItem>Married</asp:ListItem>
 </asp:DropDownList>
 </EditItemTemplate>
 </asp:TemplateField>
 <asp:CommandField ShowEditButton="True" />
</Columns>
</asp:GridView>

<asp:SqlDataSource ID="SqlDataSource1" runat="server" 
ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
SelectCommand="SELECT [ID], [Name], [Sex], [MaritalStatus] 
               FROM [Details]" 
UpdateCommand="Update Details Set [Name]=@Name, [Sex]=@Sex, 
              [MaritalStatus]=@MaritalStauts Where [ID]=@ID">
   <UpdateParameters>
       <asp:Parameter Name="Name" />
       <asp:Parameter Name="Sex" />
       <asp:Parameter Name="ID" />
       <asp:Parameter Name="MaritalStauts" />
   </UpdateParameters>
</asp:SqlDataSource>

C# Code Behind
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
 DataRowView dRowView = (DataRowView)e.Row.DataItem;
 if (e.Row.RowType == DataControlRowType.DataRow)
 {
   if ((e.Row.RowState & DataControlRowState.Edit) > 0)
   {
     RadioButtonList rblGender = (RadioButtonList)e.Row.FindControl("rbGenderEdit");
     DropDownList ddlStatus = (DropDownList)e.Row.FindControl("ddlStatusEdit");
     rblGender.SelectedValue = dRowView[2].ToString();
     ddlStatus.SelectedValue = dRowView[3].ToString();
   }
 }
       
}
   
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
 RadioButtonList rblGender = (RadioButtonList)GridView1.Rows[e.RowIndex].FindControl("rbGenderEdit");
 DropDownList ddlStatus = (DropDownList)GridView1.Rows[e.RowIndex].FindControl("ddlStatusEdit");
 SqlDataSource1.UpdateParameters["Sex"].DefaultValue = rblGender.SelectedValue;
 SqlDataSource1.UpdateParameters["MaritalStauts"].DefaultValue = ddlStatus.SelectedValue;
}
VB.NET Code Behind
Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
    Dim dRowView As DataRowView = DirectCast(e.Row.DataItem, DataRowView)
    If e.Row.RowType = DataControlRowType.DataRow Then
        If (e.Row.RowState And DataControlRowState.Edit) > 0 Then
            Dim rblGender As RadioButtonList = DirectCast(e.Row.FindControl("rbGenderEdit"), RadioButtonList)
            Dim ddlStatus As DropDownList = DirectCast(e.Row.FindControl("ddlStatusEdit"), DropDownList)
            rblGender.SelectedValue = dRowView(2).ToString()
            ddlStatus.SelectedValue = dRowView(3).ToString()
        End If
        
    End If
End Sub

Protected Sub GridView1_RowUpdating(ByVal sender As Object, ByVal e As GridViewUpdateEventArgs)
    Dim rblGender As RadioButtonList = DirectCast(GridView1.Rows(e.RowIndex).FindControl("rbGenderEdit"), RadioButtonList)
    Dim ddlStatus As DropDownList = DirectCast(GridView1.Rows(e.RowIndex).FindControl("ddlStatusEdit"), DropDownList)
    SqlDataSource1.UpdateParameters("Sex").DefaultValue = rblGender.SelectedValue
    SqlDataSource1.UpdateParameters("MaritalStauts").DefaultValue = ddlStatus.SelectedValue
End Sub

Hope this helps.

Download sample code attached



Other posts in C# and ASP.NET
1.OpenFileDialog in winforms windows forms C# .NET VB.NET windows application
2.Find IP Address in ASP.NET Behind Proxy
3.Export GridView to Pdf using iTextSharp ASP.NET
4.Hide GridView Columns In ASP.NET C# VB.NET
5.Change Mode of DetailsView or FormView when Default Mode is Set to Insert in ASP.NET
6.Creating winforms AutoComplete TextBox using C# in Windows application
7.Disable copy paste cut and right click in textbox on aspx page using javascript

If you like this post than join us or share

20 comments:

Anonymous said...

Hi,

I have a radiobuttonList control on my .NET page. There is also a button on click of which server.transer fires to another page. Now i want to retreive radiobuttonlist's select value on previous page. Can you guide me how can i do that.

My email is sunny198218@yahoo.co.in


Unknown said...

@Sunny:

If you are using Server.Transfer method then read link mentioned below to know how to retrieve data from previous page

Server.Transfer method in ASP.NET


Do let me know if this doesn't fulfill your requirement


Anonymous said...

how to link add button on another page on click in a datalist


Anonymous said...

Criteria:ive js addd a radiobuttonlist if a particular value is selected then edit operation is to be done to the particular row in a table in the database, based on the condition which i give...
i.e) when a user clicks the "existing" option in the radiobuttonlist, then it should b directed to the row value which already has its value.

giv me a solution


Anonymous said...

Hi,

I'm a beginner in VB.NET, I'd tried the above codings and the solution are great.
However, whenever I click "Edit", it won't take back the initial saved value but will replace with first value from the dropdown datasource.
Can you please help on this?

THanks.
Ginny.


Anonymous said...

I've been looking ALL DAY for this little bit of code ... Thanks


Anonymous said...

Thank a bunch for the above code, I've been looking for something like that. Now is there a way to incorporate a checkboxlist Gridview Edit or In the insert I need to retrieve the select items from the DB that the user check and display again.


Anonymous said...

Could not find a suitable section so I written here, how to become a moderator for your forum, that need for this?


Ayesha Farooq said...

Simply awsum!! My application was smooth..Thanx =)


Anonymous said...

I am getting the following error can u please help

Object reference not set to an instance of an object.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

Source Error:


Line 40: DropDownList ddlStatus = (DropDownList)GridView1.Rows[e.RowIndex].FindControl("ddlStatusEdit");
Line 41: SqlDataSource1.UpdateParameters["Sex"].DefaultValue = rblGender.SelectedValue;
Line 42: SqlDataSource1.UpdateParameters["MartialStatus"].DefaultValue = ddlStatus.SelectedValue;
Line 43: }
Line 44: protected void GridView1_RowUpdated(object sender, GridViewUpdatedEventArgs e)

Source File: e:\Works\RadioButtonListExample\Default2.aspx.cs Line: 42


Anonymous said...

i'm facing a problem when i wrote connectionstring can u explain me how to write that code whether in web.config file or any other place.


Anonymous said...

Hi Amit Sir,

I have to put Search buttton on my website. That should retrieve or search from database as well.

How to implement. Can you please give me any example ?


Anonymous said...

Yes, a quite good variant


rhiena said...

hii thanks for tutorial, but i am using c #, could you help me please....


sari said...

what about in linqdatasource???


mahendra said...

In detailsview I want to show values in dropdownlist from different datasource table. other fields will show from current selected datasource table. and on update two diffrent tables get updated.


Anonymous said...

If not us, who? If not now, when?


Anonymous said...

hi ,i tried this alot but nt succeeded now tell me how to get this as i want to create an attendance page for my site in which i m trying to update attendance by using dropdownlist in a gridview as a item template so that admin can select from present and absent item's from ddl and which update's the attendance and ll show current attendance to studnt. ...


Anonymous said...

Hi Amit
Could pls show how to do the same with detailsview.
Thanks


Unknown said...

@Above: Please refer DropDownList In DetailsView EditItemTemplate


Find More Articles