Jump to content

Radio Buttons in Datagrid


Siddhesh

Recommended Posts

Hi, I m trying to fill a datagrid with Some values from DB. Now first colomn is a raio button and u can select only one of them.But the thing is not happening.Can anyone tell me solution (without using any third party component)My Grid is as shown below.

<asp:DataGrid id="DataGrid1" runat="server" Width="700px" AutoGenerateColumns="False" PageSize="10"	PagerStyle-Mode="NumericPages" AllowPaging="True">	<Columns>  <asp:TemplateColumn HeaderText="Select"> 	 <HeaderStyle Width="20%"></HeaderStyle> 	 <ItemTemplate>    <asp:RadioButton GroupName="Rdo1" ID="Rd"></asp:RadioButton> 	 </ItemTemplate>  </asp:TemplateColumn>  <asp:BoundColumn DataField="ContactName" HeaderText="Name"> 	 <HeaderStyle Width="20%"></HeaderStyle>  </asp:BoundColumn>  <asp:BoundColumn DataField="City" HeaderText="City"> 	 <HeaderStyle Width="20%"></HeaderStyle>  </asp:BoundColumn>  <asp:HyperLinkColumn Text="Go to search Engine" Target="_blank" DataNavigateUrlField="City" DataNavigateUrlFormatString="http://Google.com/search?hl=en&lr=&q={0}" 	 HeaderText="Search"> 	 <HeaderStyle Width="20%"></HeaderStyle> 	 <ItemStyle Wrap="False"></ItemStyle>  </asp:HyperLinkColumn>  <asp:ButtonColumn Text="Click Me" ButtonType="PushButton" HeaderText="<B>Button</B>"></asp:ButtonColumn>	</Columns>	<PagerStyle Mode="NumericPages"></PagerStyle></asp:DataGrid></TD>

Link to comment
Share on other sites

But the thing is not happening.Can anyone tell me solution (without using any third party component)
What thing is not happening? Are the radio buttons not showing up? do you get an error message???You need to be more specific about what your problem is....and post your full code
Link to comment
Share on other sites

You need to be more specific about what your problem is....and post your full code

Sorry for the mistakeHere is the code
<%@ Page Language="vb" AutoEventWireup="false" Codebehind="Home_Page.aspx.vb" Inherits="Test_Sid.WebForm1"%><HTML>	<HEAD>	</HEAD>	<body MS_POSITIONING="GridLayout">   	 <form id="Form1" method="post" runat="server">      <TABLE height="492" cellSpacing="0" cellPadding="0" width="856" border="0" ms_2d_layout="TRUE">     	 <TR vAlign="top">             	 <TD >       	 <asp:DataGrid id="DataGrid1" runat="server" Width="700px" AutoGenerateColumns="False" PageSize="10"          PagerStyle-Mode="NumericPages" AllowPaging="True">          <Columns>         	 <asp:TemplateColumn HeaderText="Select">            <HeaderStyle Width="20%"></HeaderStyle>            <ItemTemplate>           	 <asp:RadioButton GroupName="Rdo1" ID="Rd"></asp:RadioButton>            </ItemTemplate>         	 </asp:TemplateColumn>         	 <asp:BoundColumn DataField="ContactName" HeaderText="Name">            <HeaderStyle Width="20%"></HeaderStyle>         	 </asp:BoundColumn>         	 <asp:BoundColumn DataField="City" HeaderText="City">            <HeaderStyle Width="20%"></HeaderStyle>         	 </asp:BoundColumn>         	 <asp:HyperLinkColumn Text="Go to search Engine" Target="_blank" DataNavigateUrlField="City" DataNavigateUrlFormatString="http://Google.com/search?hl=en&lr=&q={0}"            HeaderText="Search">            <HeaderStyle Width="20%"></HeaderStyle>            <ItemStyle Wrap="False"></ItemStyle>         	 </asp:HyperLinkColumn>         	 <asp:ButtonColumn Text="Click Me" ButtonType="PushButton" HeaderText="<B>Button</B>"></asp:ButtonColumn>          </Columns>          <PagerStyle Mode="NumericPages"></PagerStyle>       	 </asp:DataGrid></TD>     	 </TR>      </TABLE>   	 </form>	</body></HTML>

And the code is as follows

Public Class WebForm1    Inherits System.Web.UI.Page#Region " Web Form Designer Generated Code "    'This call is required by the Web Form Designer.    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()    End Sub    Protected WithEvents DataGrid1 As System.Web.UI.WebControls.DataGrid    'NOTE: The following placeholder declaration is required by the Web Form Designer.    'Do not delete or move it.    Private designerPlaceholderDeclaration As System.Object    Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init        'CODEGEN: This method call is required by the Web Form Designer        'Do not modify it using the code editor.        InitializeComponent()    End Sub#End Region    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load        'Put user code to initialize the page here            BindGrid()    End Sub    Private Sub BindGrid()        Dim SDA As SqlClient.SqlDataAdapter        Dim DS As New Data.DataSet        Dim StrSqlQry As New SqlClient.SqlCommand("Select * From Customers", Session("con"))        Session("con").Open()        SDA = New SqlClient.SqlDataAdapter(StrSqlQry)        SDA.Fill(DS, "Customers")        DataGrid1.DataSource = DS.Tables("Customers")        DataGrid1.DataBind()        Session("con").Close()    End Sub

The Connection string is as follows

      Dim strCon As String        strCon = "data source=localhost;persist security info=False;initial catalog=NorthWind;user id=sa;PWD=;"        Session("con") = New SqlClient.SqlConnection(strCon)
The problem is that I want to select only one row of the grid for which i have to checked radio button, and if I click on the other radio button then 1st should get deselect. But currently i can select any number of radio buttons. So now I want to group them. I tried with with group name, but it is not working.
Link to comment
Share on other sites

I am not sure how to fix your problem but I am surre it has to do with the ID. Everyone is being created with the same id. They can share the same GroupName but the ID has to be unique.

Thanks for taking efforts for my problem.I m still searching for the solution
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...