Introduction

This article demonstrates an interesting and very useful concept in GridView.

Question: What is a GridView with Radio Button Binding?

In simple terms, it enables binding to the GridView, by selecting an appropriate radio button item in a grid.

Step 1: Create a new web application

Output1.jpg

Step 2: The complete code of WebForm1.aspx is as in the following:

  1. <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="GridViewRadioButtonBindApp.WebForm1" %>
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  3. <html xmlns="http://www.w3.org/1999/xhtml">
  4. <head id="Head1" runat="server">
  5. <title></title>
  6. </head>
  7. <body>
  8. <form id="form1" runat="server">
  9. <center>
  10. <div>
  11. <table>
  12. <tr>
  13. <td colspan="2" align="center">
  14. <asp:Label ID="Label1" runat="server" Text="GridView Radio Button Bind App" Font-Bold="true"
  15. Font-Size="Large" Font-Names="Verdana" ForeColor="Maroon"></asp:Label>
  16. </td>
  17. </tr>
  18. <tr>
  19. <td colspan="2" align="center">
  20. <asp:GridView ID="GridView1" runat="server" CssClass="grid" BackColor="LightGoldenrodYellow"
  21. BorderColor="Tan" BorderWidth="1px" CellPadding="2" AutoGenerateColumns="false"
  22. ForeColor="Black" GridLines="None" OnRowDataBound="GridView1_RowDataBound">
  23. <AlternatingRowStyle BackColor="PaleGoldenrod" />
  24. <FooterStyle BackColor="Tan" />
  25. <HeaderStyle BackColor="Tan" Font-Bold="True" />
  26. <PagerStyle BackColor="PaleGoldenrod" ForeColor="DarkSlateBlue" HorizontalAlign="Center" />
  27. <SelectedRowStyle BackColor="DarkSlateBlue" ForeColor="GhostWhite" />
  28. <SortedAscendingCellStyle BackColor="#FAFAE7" />
  29. <SortedAscendingHeaderStyle BackColor="#DAC09E" />
  30. <SortedDescendingCellStyle BackColor="#E1DB9C" />
  31. <SortedDescendingHeaderStyle BackColor="#C2A47B" />
  32. <Columns>
  33. <asp:TemplateField HeaderText="FirstName">
  34. <ItemTemplate>
  35. <asp:Label runat="server" ID="lblFName" Text='<%# Bind("FirstName") %>'></asp:Label></ItemTemplate>
  36. </asp:TemplateField>
  37. <asp:TemplateField HeaderText="LastName">
  38. <ItemTemplate>
  39. <asp:Label runat="server" ID="lblLName" Text='<%# Bind("LastName") %>'></asp:Label></ItemTemplate>
  40. </asp:TemplateField>
  41. <asp:TemplateField HeaderText="Gender">
  42. <ItemTemplate>
  43. <asp:RadioButtonList ID="RadioButtonList1" runat="server">
  44. <asp:ListItem Text="Male" Value="1"></asp:ListItem>
  45. <asp:ListItem Text="Female" Value="2"></asp:ListItem>
  46. </asp:RadioButtonList>
  47. </ItemTemplate>
  48. </asp:TemplateField>
  49. </Columns>
  50. </asp:GridView>
  51. </td>
  52. </tr>
  53. </table>
  54. </div>
  55. </center>
  56. </form>
  57. </body>
  58. </html>

Step 3: The complete code of WebForm1.aspx.cs is as in the following:

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.UI;
  6. using System.Web.UI.WebControls;
  7. using System.Data.SqlClient;
  8. using System.Data;
  9. namespace GridViewRadioButtonBindApp
  10. {
  11. public partial class WebForm1 : System.Web.UI.Page
  12. {
  13. SqlConnection con = new SqlConnection("Data Source=WIN-KV3BO1RQQF7;Initial Catalog=Company;Integrated Security=True");
  14. protected void Page_Load(object sender, EventArgs e)
  15. {
  16. if (!IsPostBack) { con.Open();
  17. SqlCommand cmd = new SqlCommand("select FirstName, LastName, Gender from Employee", con);
  18. SqlDataAdapter da = new SqlDataAdapter(cmd);
  19. DataSet ds = new DataSet();
  20. da.Fill(ds);
  21. con.Close();
  22. GridView1.DataSource = ds;
  23. GridView1.DataBind();
  24. }
  25. }
  26. protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
  27. {
  28. if (e.Row.RowType == DataControlRowType.DataRow)
  29. {
  30. int genderValue = (int)DataBinder.Eval(e.Row.DataItem, "Gender");
  31. RadioButtonList rb = (RadioButtonList)e.Row.FindControl("RadioButtonList1");
  32. rb.Items.FindByValue(genderValue.ToString()).Selected = true;
  33. }
  34. }
  35. }
  36. }

Step 4: The output for the application is shown below:

Output2.png

I hope this article was useful for you.