Search - Articles
DevASP.NET for ASP.NET, VB.NET, XML and C# (C-Sharp) Developers Tuesday, March 03, 2009
Dev Articles
Search Directory
ASP.NET
VB.Net
C-Sharp
SQL Server
 

How to use DropDownList, RadioButtonList and CheckBoxList to edit GridView cells in ASP.NET

Author: Nikki Smith
Download Source Code : 1414_GridViewEditDDLCBRB.zip

In this article, I will show you how you can use DropDownList, RadioButtonList and CheckBoxList to edit and update GridView cells in ASP.NET.

 

 
You can use DropDownList, RadioButtonList and CheckBoxList in EditItemTemplate to update database entries from GridView. When we use SqlDataSource to update SQL database table, update parameters are used along with UpdateCommand. We can easily set selected value of DropDownList, RadioButtonList and CheckBoxList as default value of UpdateParameter in RowUpdating event of GridView control.  In this article, I have used the same technique as discussed above. I have created a database Employees and a table Employees for this article.
 
 
  1. Create a new Empty Web Site in Visual Studio 2010 either in Visual Basic or Visual C#.
  2. Add a Web Form in the Web Site. No Need to change name of the Page
  3. Add a GridView control and SqlDataSource in Web Form and write code below.

    <asp:GridView ID="GridView1" AutoGenerateColumns="false" runat="server"
                DataSourceID="SqlDataSource1" onrowupdating="GridView1_RowUpdating">
       <Columns>
            <asp:BoundField DataField="ID" HeaderText="ID" />
            <asp:BoundField DataField="LastName" HeaderText="LastName" />
            <asp:BoundField DataField="FirstName" HeaderText="FirstName" />
            <asp:TemplateField HeaderText="Sex">
                <ItemTemplate>
                    <asp:Label ID="lblSex" runat="server" Text='<%#Eval("Sex") %>'> </asp:Label>
                </ItemTemplate>
                <EditItemTemplate>
                    <asp:RadioButtonList ID="rblSex" runat="server">
                        <asp:ListItem>Male</asp:ListItem>
                        <asp:ListItem>Female</asp:ListItem>
                    </asp:RadioButtonList>
                </EditItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Title">
                <ItemTemplate>
                    <asp:Label ID="lblTitle" runat="server" Text='<%#Eval("Title") %>'> </asp:Label>
                </ItemTemplate>
                <EditItemTemplate>
                    <asp:DropDownList ID="ddlTitle" runat="server">
                        <asp:ListItem>Sales Representative</asp:ListItem>
                        <asp:ListItem>Inside Sales Coordinator</asp:ListItem>
                        <asp:ListItem>Marketing Manager</asp:ListItem>
                    </asp:DropDownList>
                </EditItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="City">
                <ItemTemplate>
                   <asp:Label ID="lblCity" runat="server" Text='<%#Eval("City") %>'> </asp:Label>
               </ItemTemplate>
                <EditItemTemplate>
                    <asp:CheckBoxList ID="cblCity" runat="server">
                        <asp:ListItem>Seatle</asp:ListItem>
                        <asp:ListItem>Eugene</asp:ListItem>
                        <asp:ListItem>Anchorage</asp:ListItem>
                    </asp:CheckBoxList>
                </EditItemTemplate>
           </asp:TemplateField>
           <asp:TemplateField HeaderText="State">
                <ItemTemplate>
                    <asp:Label ID="lblState" runat="server" Text='<%#Eval("State") %>'> </asp:Label>
                </ItemTemplate>
                <EditItemTemplate>
                    <asp:CheckBoxList ID="cblState" runat="server">
                        <asp:ListItem>WA</asp:ListItem>
                        <asp:ListItem>AK</asp:ListItem>
                        <asp:ListItem>OR</asp:ListItem>
                    </asp:CheckBoxList>
                </EditItemTemplate>
           </asp:TemplateField>
           <asp:CommandField ShowEditButton="True" />
     </Columns>
    </asp:GridView>
    <asp:SqlDataSource ID="SqlDataSource1" runat="server"
        ConnectionString="<%$ ConnectionStrings:EmployeesConnectionString %>"
        SelectCommand="SELECT * FROM [Employees]"
        UpdateCommand="UPDATE [Employees] SET [LastName] = @LastName, [FirstName] = @FirstName, [Sex] = @Sex, [Title] = @Title, [City] = @City, [State] = @State WHERE [ID] = @ID">
        <UpdateParameters>
            <asp:Parameter Name="LastName" Type="String" />
            <asp:Parameter Name="FirstName" Type="String" />
            <asp:Parameter Name="Sex" Type="String" />
            <asp:Parameter Name="Title" Type="String" />
           <asp:Parameter Name="City" Type="String" />
            <asp:Parameter Name="State" Type="String" />
            <asp:Parameter Name="ID" Type="Int32" />
        </UpdateParameters>
    </asp:SqlDataSource>
     
     
  4. Write code below in code behind file

    In RowUpdating event of GridView control, DropDownList, RadioButtonList and CheckBoxList controls are found using RowIndex property and FindControl() method. Selected values for all three controls are set to default value of that UpdateParameter in SqlDataSource.
     
    Visual Basic
     
    Protected Sub GridView1_RowUpdating(sender As Object, e As System.Web.UI.WebControls.GridViewUpdateEventArgs) Handles GridView1.RowUpdating
        Dim rblSex As RadioButtonList = DirectCast(GridView1.Rows(e.RowIndex).FindControl("rblSex"), RadioButtonList)
        SqlDataSource1.UpdateParameters("Sex").DefaultValue = rblSex.SelectedValue
        Dim ddlTitle As DropDownList = DirectCast(GridView1.Rows(e.RowIndex).FindControl("ddlTitle"), DropDownList)
        SqlDataSource1.UpdateParameters("Title").DefaultValue = ddlTitle.SelectedValue
        Dim cblCity As CheckBoxList = DirectCast(GridView1.Rows(e.RowIndex).FindControl("cblCity"), CheckBoxList)
        SqlDataSource1.UpdateParameters("City").DefaultValue = cblCity.SelectedValue
        Dim cblState As CheckBoxList = DirectCast(GridView1.Rows(e.RowIndex).FindControl("cblState"), CheckBoxList)
        SqlDataSource1.UpdateParameters("State").DefaultValue = cblState.SelectedValue
    End Sub
     
    Visual C#
     
    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        RadioButtonList rblSex = (RadioButtonList)GridView1.Rows[e.RowIndex].FindControl("rblSex");
        SqlDataSource1.UpdateParameters["Sex"].DefaultValue = rblSex.SelectedValue;
        DropDownList ddlTitle = (DropDownList)GridView1.Rows[e.RowIndex].FindControl("ddlTitle");
        SqlDataSource1.UpdateParameters["Title"].DefaultValue = ddlTitle.SelectedValue;
        CheckBoxList cblCity = (CheckBoxList)GridView1.Rows[e.RowIndex].FindControl("cblCity");
        SqlDataSource1.UpdateParameters["City"].DefaultValue = cblCity.SelectedValue;
        CheckBoxList cblState = (CheckBoxList)GridView1.Rows[e.RowIndex].FindControl("cblState");
        SqlDataSource1.UpdateParameters["State"].DefaultValue = cblState.SelectedValue;
    }

     
  5. Now you can see website in your browser

 

   
Add Article Comment:
Name :
Email Address :
   
Comments :
 
   
<< Partial classes in ASP.NET

Disclaimer - Privacy
© 2002-2017 DevASP.net