| | |
How can I add a checkbox to a datagrid?
Please support our ASP.NET advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: May 2009
Posts: 40
Reputation:
Solved Threads: 0
Hi, i'm trying to add a checkbox to a datagrid but don't seem to get anywhere. I got to add a new column but that's where it stops.
I'm reading data out of my database and then deleting the first column of that dataset's table as it is not needed, then adding another column for the checkbox to be in.
Here is my code: Can anybody please give me some advice?
(I have visited alot of sites and other forums sofar but don't seem to get a solution)
(Please note that i'm using a masterpage)
I'm reading data out of my database and then deleting the first column of that dataset's table as it is not needed, then adding another column for the checkbox to be in.
Here is my code: Can anybody please give me some advice?
(I have visited alot of sites and other forums sofar but don't seem to get a solution)

(Please note that i'm using a masterpage)
ASP.NET Syntax (Toggle Plain Text)
//DATA GRID VIEW FILL clsUploadCallArray getallcalls = new clsUploadCallArray(emp.E_mobile.ToString()); int c = getallcalls.UploadCallsObj.Count; DataSet uploadDS = getallcalls.UplCallDS; DataTable uploadDT = uploadDS.Tables["Upload"]; uploadDT.Columns.Remove("Upl_ID"); uploadDT.Columns.Add(); dgv_Calls.DataSource = uploadDT; dgv_Calls.DataBind(); dgv_Calls.Visible = true; //set column names //header text dgv_Calls.HeaderRow.Cells[0].Text = "Tel"; dgv_Calls.HeaderRow.Cells[1].Text = "Start Date/Time"; dgv_Calls.HeaderRow.Cells[2].Text = "Duration(seconds)"; dgv_Calls.HeaderRow.Cells[3].Text = "Transaction"; dgv_Calls.HeaderRow.Cells[4].Text = "Destination"; dgv_Calls.HeaderRow.Cells[5].Text = "Amount"; dgv_Calls.HeaderRow.Cells[6].Text = "Roaming"; dgv_Calls.HeaderRow.Cells[7].Text = "Discount"; dgv_Calls.HeaderRow.Cells[8].Text = "Mark"; //footer text dgv_Calls.FooterRow.Cells[0].Text = "Tel"; dgv_Calls.FooterRow.Cells[1].Text = "Start Date/Time"; dgv_Calls.FooterRow.Cells[2].Text = "Duration(seconds)"; dgv_Calls.FooterRow.Cells[3].Text = "Transaction"; dgv_Calls.FooterRow.Cells[4].Text = "Destination"; dgv_Calls.FooterRow.Cells[5].Text = "Amount"; dgv_Calls.FooterRow.Cells[6].Text = "Roaming"; dgv_Calls.FooterRow.Cells[7].Text = "Discount"; dgv_Calls.FooterRow.Cells[8].Text = "Mark"; dgv_Calls.Attributes["style"] = "border-color:black";
Last edited by phoenix_dwarf; Oct 13th, 2009 at 2:48 pm.
0
#2 Oct 13th, 2009
I think we cannot add a checkbox to a datagrid on code behind.But if you are good at javascript we can find a way to do that.
The above code adds a checkbox to each row of a datagrid.
I dont know wether its good programming practice or not...
ASP.NET Syntax (Toggle Plain Text)
function AddColumn() { var tab =document.getElementById('GridView1');//Your DataGrid Id var rows =tab.getElementsByTagName("tr"); for(var i=0;i<rows.length;i++) { var currentrow= rows[i]; var newcell=currentrow.insertCell(-1); newcell.innerHTML="<input type='checkbox' />"; } }
The above code adds a checkbox to each row of a datagrid.
I dont know wether its good programming practice or not...
•
•
Join Date: Sep 2007
Posts: 66
Reputation:
Solved Threads: 12
1
#4 Oct 14th, 2009
Hello
You can use the following code
ASPX
ASPX.VB
Note : datagrid = "dg"
To fine any data from the datagrid use the following
Mark as solved if it helps you!!!
You can use the following code
ASPX
ASP.NET Syntax (Toggle Plain Text)
<asp:TemplateColumn> <headertemplate> <asp:checkbox id="chkSelectAll" runat="server" Width="30px" Enabled="False" ></asp:checkbox> </headertemplate> <itemtemplate> <asp:checkbox id="chkSelect" runat="server" Width="30px" SecurityContextID="0" Enabled="False" Checked='<%# DataBinder.Eval(Container, "DataItem.IsSelected") %>' AutoFocus="False" ></asp:checkbox> </itemtemplate> <headerstyle width="30px"></headerstyle> </asp:TemplateColumn>
Note : datagrid = "dg"
To fine any data from the datagrid use the following
ASP.NET Syntax (Toggle Plain Text)
Private Function _validateEachEmployeeRow() As String Dim i As Int16 Dim chkSelect As CheckBox 'Dim txtEmpName, txt As HASIB.Controls.HasibTextBox Dim lbl, lblEmpName As HASIB.Controls.HasibLabel For i = 0 To dg.Items.Count - 1 chkSelect = Me.dg.Items(i).FindControl("chkSelect") If chkSelect.Checked Then lblEmpName = Me.dg.Items(i).FindControl("lblEmployeeName") Next Return lblEmpName End Function
Mark as solved if it helps you!!!
Last edited by reach_yousuf; Oct 14th, 2009 at 1:52 am.
•
•
Join Date: May 2009
Posts: 40
Reputation:
Solved Threads: 0
0
#5 Oct 14th, 2009
Well thanx for all the replies but reach_yousuf your code gives an error by me...it says that the <asp:TemplateColumn> is an unknown element...and a bucnh of other errors...any ideas?
(I put this piece of code in my masterpages header and inside the asp:Gridview block but still same errors.)
Non-Masterpage code:
Master Page Code:
(I put this piece of code in my masterpages header and inside the asp:Gridview block but still same errors.)
Non-Masterpage code:
ASP.NET Syntax (Toggle Plain Text)
<%@ Page Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="Calls.aspx.cs" Inherits="Calls" Title="Untitled Page" %> <asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server"> </asp:Content> <asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolderMain" Runat="Server"> <asp:Label ID="lbl_Welcome1" runat="server" Text="Welcome: "></asp:Label> <asp:Label ID="lbl_Welcome" runat="server" Text="N/A"></asp:Label> <asp:Button ID="btn_Back" runat="server" OnClick="btn_Back_Click" Text="Back" /> <asp:Button ID="btn_Logout" runat="server" OnClick="btn_Logout_Click" Text="Log Out" /><br /> <asp:GridView ID="dgv_Calls" runat="server" ForeColor="#333333" GridLines="Vertical" ShowFooter="True"> <HeaderStyle BackColor="#5D7B9D" BorderColor="Black" BorderStyle="Double" BorderWidth="2px" Font-Bold="True" ForeColor="White" HorizontalAlign="Center" /> <RowStyle BackColor="#F7F6F3" ForeColor="#333333" HorizontalAlign="Center" VerticalAlign="Middle" /> <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" HorizontalAlign="Center" VerticalAlign="Middle" /> <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" /> <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" /> <EditRowStyle BackColor="#999999" /> <AlternatingRowStyle BackColor="White" ForeColor="#284775" /> <asp:TemplateColumn> <headertemplate> <asp:checkbox id="chkSelectAll" runat="server" Width="30px" Enabled="False" ></asp:checkbox> </headertemplate> <itemtemplate><asp:checkbox id="chkSelect" runat="server" Width="30px" SecurityContextID="0" Enabled="False" Checked='<%# DataBinder.Eval(Container, "DataItem.IsSelected") %>' AutoFocus="False" > </asp:checkbox> </itemtemplate> <headerstyle width="30px"></headerstyle> </asp:TemplateColumn> </asp:GridView> </asp:Content>
Master Page Code:
ASP.NET Syntax (Toggle Plain Text)
<%@ Master Language="C#" AutoEventWireup="true" CodeFile="MasterPage.master.cs" Inherits="MasterPage" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server"> <title>BASF ~ Login</title> <script language="javascript"> window.history.forward(1); </script> <asp:TemplateColumn> <headertemplate> <asp:checkbox id="chkSelectAll" runat="server" Width="30px" Enabled="False" ></asp:checkbox> </headertemplate> <itemtemplate><asp:checkbox id="chkSelect" runat="server" Width="30px" SecurityContextID="0" Enabled="False" Checked='<%# DataBinder.Eval(Container, "DataItem.IsSelected") %>' AutoFocus="False" > </asp:checkbox> </itemtemplate> <headerstyle width="30px"></headerstyle> </asp:TemplateColumn> <asp:ContentPlaceHolder id="head" runat="server"> </asp:ContentPlaceHolder> <link href="Styles.css" rel="stylesheet" type="text/css" /> </head>
•
•
Join Date: May 2009
Posts: 40
Reputation:
Solved Threads: 0
0
#6 Oct 14th, 2009
I visited this page aswell but i'm afraid that in picture 6 i don't have the ItemTemplate...only EmptyDataTemplate and PagerTemplate...Any ideas???
http://www.asp.net/Learn/Data-Access...ial-52-cs.aspx
http://www.asp.net/Learn/Data-Access...ial-52-cs.aspx
•
•
Join Date: May 2009
Posts: 40
Reputation:
Solved Threads: 0
0
#7 Oct 14th, 2009
I found a way that is almost working for me...it adds the checkboxes now but my columns now goes haywire...but i'll keep you posted!
http://www.highoncoding.com/Articles...20Control.aspx
http://www.highoncoding.com/Articles...20Control.aspx
•
•
Join Date: May 2009
Posts: 40
Reputation:
Solved Threads: 0
0
#8 Oct 14th, 2009
Oky all is well now but now i ran into the problem of selecting certain rows and only retrieving those rows data....here is my code:
This function is run when i press the submit button...Any thoughts?
ASP.NET Syntax (Toggle Plain Text)
public void ValidateCheck() { int count = 0; // Select the checkboxes from the GridView control for (int i = 0; i < dgv_Calls.Rows.Count; i++) { GridViewRow row = dgv_Calls.Rows[i]; bool isChecked = ((CheckBox)row.FindControl("chkSelect")).Checked; if (isChecked) { count++; telD.Text = "YAY!!!" + count; } } //telD.Text = "YAY!!!" + count; }
This function is run when i press the submit button...Any thoughts?
Last edited by phoenix_dwarf; Oct 14th, 2009 at 11:16 am. Reason: comment something out
![]() |
Similar Threads
- How to add a colimn in Datagrid at runtime (C#)
- Checkbox in Datagrid has to click twice to uncheck. (VB.NET)
- to add checkbox in asp.net in gridview (ASP.NET)
- How to add a new row to a datagrid (C#)
- Selcet/Deselcet All checkbox in Datagrid using Javascript (JavaScript / DHTML / AJAX)
- add column to datagrid form dropdownlist selectitem (VB.NET)
- checkbox in datagrid (ASP.NET)
Other Threads in the ASP.NET Forum
- Previous Thread: GridView editing
- Next Thread: Install Silver Light
| Thread Tools | Search this Thread |






