Click here to Skip to main content
15,896,606 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All

I have datagrid not gridview in asp.net, which has columns one of them is check box column. I need check box behave like radio button i mean i could check only single check box in datagrid rows. Can anybody help me.

some body recommend me to use checkboxlist.

please help me.



Thanks in advance.
Posted
Updated 9-Oct-11 21:09pm
v2
Comments
Sunasara Imdadhusen 10-Oct-11 9:38am    
please provide snippet of code!
Tejas Vaishnav 11-Oct-11 1:04am    
You need to provide some code as well as your design fro data grid "not gridview"

private void Page_Load(object sender, System.EventArgs e)
{
    cnnDB = new SqlConnection("server=HOME;user id=sa;password=;database=Contacts");
    if (!IsPostBack)
        bindGrid();
}
private void bindGrid()
{
    SqlDataAdapter da = new SqlDataAdapter("select * from Anz_Contacts",cnnDB);
    DataSet ds = new DataSet();
    da.Fill(ds, "Employees");
    myDataGrid.DataSource=ds.Tables["Employees"].DefaultView;
    myDataGrid.DataBind();
}



XML
<form id="Form1" name="Form1" method="post" runat="server">
<ASP:DATAGRID id="myDataGrid"
runat="server"
AutoGenerateColumns="false"
HeaderStyle-BackColor="#aaaadd"
Font-Size="8pt"
Font-Name="Verdana"
CellSpacing="0"
CellPadding="3"
ShowFooter="true"
BorderColor="Black"
BackColor="AntiqueWhite"
Width="600">
       <HeaderStyle BackColor="NavajoWhite"></HeaderStyle>
       <FooterStyle BackColor="NavajoWhite"></FooterStyle>
       <Columns>
              <asp:TemplateColumn HeaderText="contract">
                     <HeaderTemplate>
       <input type="checkbox" id="checkAll"
 onclick="CheckAll(this);" runat="server" name="checkAll">
                     </HeaderTemplate>
                     <ItemTemplate>
                           <input type="checkbox" runat="server" id="EmpId"
 onclick="CheckChanged();" checked='false' name="EmpId" />
                     </ItemTemplate>
                           </asp:TemplateColumn>
              <asp:TemplateColumn HeaderText="Id">
                     <ItemTemplate>
                           <asp:Label id="Id" Text='<%#
DataBinder.Eval(Container.DataItem, "Id") %>' runat="server" />
                     </ItemTemplate>
              </asp:TemplateColumn>
              <asp:BoundColumn DataField="FirstName"
       HeaderText="FirstName"></asp:BoundColumn>
              <asp:BoundColumn DataField="LastName"
HeaderText="LastName"></asp:BoundColumn>
              <asp:BoundColumn DataField="Designation"
HeaderText="Designation"></asp:BoundColumn>
       </Columns>
</ASP:DATAGRID>
</form >




Script to create table in SQL is as :

SQL
CREATE TABLE [dbo].[Anz_Contacts] (
       [Id] [int] IDENTITY (1, 1) NOT NULL ,
       [FirstName] [char] (10)  NULL ,
       [LastName] [char] (10)  NULL ,
       [Designation] [varchar] (50)  NULL       
) ON [PRIMARY]
GO
ALTER TABLE [dbo].[Anz_Contacts] WITH NOCHECK ADD 
       CONSTRAINT [PK_Anz_Contacts] PRIMARY KEY  CLUSTERED 
       (
              [Id]
       )  ON [PRIMARY] 
GO
 
Share this answer
 
v2
Try this js function
C#
function CheckOne(obj)
    {
        var dtgrid = obj.parentNode.parentNode.parentNode;
        var inputs = dtgrid.getElementsByTagName("input");
        for(var i=0;i<inputs.length;i++)
        {
            if (inputs[i].type =="checkbox")
            {
                if(obj.checked && inputs[i] != obj && inputs[i].checked)
                {
                    inputs[i].checked = false;
                }
            }
        }
    }


<asp:CheckBox ID="CheckBox4" runat="server" onclick ="CheckOne(this)" />
 
Share this answer
 
v2
What stops you from using a Radiobutton then? :) I guess when there is a control that works like you need it to, i don't see any reason why would one want to use something else that is designed to work in a different way..

Cheers...
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900