944,017 Members | Top Members by Rank

Ad:
  • ASP.NET Discussion Thread
  • Unsolved
  • Views: 3624
  • ASP.NET RSS
You are currently viewing page 1 of this multi-page discussion thread
Oct 13th, 2009
0

How can I add a checkbox to a datagrid?

Expand Post »
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)

ASP.NET Syntax (Toggle Plain Text)
  1. //DATA GRID VIEW FILL
  2. clsUploadCallArray getallcalls = new clsUploadCallArray(emp.E_mobile.ToString());
  3. int c = getallcalls.UploadCallsObj.Count;
  4. DataSet uploadDS = getallcalls.UplCallDS;
  5. DataTable uploadDT = uploadDS.Tables["Upload"];
  6. uploadDT.Columns.Remove("Upl_ID");
  7. uploadDT.Columns.Add();
  8. dgv_Calls.DataSource = uploadDT;
  9. dgv_Calls.DataBind();
  10. dgv_Calls.Visible = true;
  11.  
  12. //set column names
  13. //header text
  14. dgv_Calls.HeaderRow.Cells[0].Text = "Tel";
  15. dgv_Calls.HeaderRow.Cells[1].Text = "Start Date/Time";
  16. dgv_Calls.HeaderRow.Cells[2].Text = "Duration(seconds)";
  17. dgv_Calls.HeaderRow.Cells[3].Text = "Transaction";
  18. dgv_Calls.HeaderRow.Cells[4].Text = "Destination";
  19. dgv_Calls.HeaderRow.Cells[5].Text = "Amount";
  20. dgv_Calls.HeaderRow.Cells[6].Text = "Roaming";
  21. dgv_Calls.HeaderRow.Cells[7].Text = "Discount";
  22. dgv_Calls.HeaderRow.Cells[8].Text = "Mark";
  23. //footer text
  24. dgv_Calls.FooterRow.Cells[0].Text = "Tel";
  25. dgv_Calls.FooterRow.Cells[1].Text = "Start Date/Time";
  26. dgv_Calls.FooterRow.Cells[2].Text = "Duration(seconds)";
  27. dgv_Calls.FooterRow.Cells[3].Text = "Transaction";
  28. dgv_Calls.FooterRow.Cells[4].Text = "Destination";
  29. dgv_Calls.FooterRow.Cells[5].Text = "Amount";
  30. dgv_Calls.FooterRow.Cells[6].Text = "Roaming";
  31. dgv_Calls.FooterRow.Cells[7].Text = "Discount";
  32. dgv_Calls.FooterRow.Cells[8].Text = "Mark";
  33.  
  34. dgv_Calls.Attributes["style"] = "border-color:black";
Last edited by phoenix_dwarf; Oct 13th, 2009 at 2:48 pm.
Similar Threads
Reputation Points: 11
Solved Threads: 0
Junior Poster in Training
phoenix_dwarf is offline Offline
57 posts
since May 2009
Oct 13th, 2009
0
Re: How can I add a checkbox to a datagrid?
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.
ASP.NET Syntax (Toggle Plain Text)
  1. function AddColumn()
  2. {
  3. var tab =document.getElementById('GridView1');//Your DataGrid Id
  4.  
  5. var rows =tab.getElementsByTagName("tr");
  6. for(var i=0;i<rows.length;i++)
  7. {
  8. var currentrow= rows[i];
  9. var newcell=currentrow.insertCell(-1);
  10. newcell.innerHTML="<input type='checkbox' />";
  11. }
  12.  
  13. }

The above code adds a checkbox to each row of a datagrid.
I dont know wether its good programming practice or not...
Reputation Points: 22
Solved Threads: 7
Junior Poster in Training
Dhaneshnm is offline Offline
52 posts
since Mar 2009
Oct 13th, 2009
0
Re: How can I add a checkbox to a datagrid?
you can use item template.
Reputation Points: 10
Solved Threads: 12
Posting Whiz in Training
carobee is offline Offline
209 posts
since Dec 2007
Oct 14th, 2009
1
Re: How can I add a checkbox to a datagrid?
Hello

You can use the following code
ASPX
ASP.NET Syntax (Toggle Plain Text)
  1. <asp:TemplateColumn>
  2. <headertemplate>
  3. <asp:checkbox id="chkSelectAll" runat="server" Width="30px" Enabled="False" ></asp:checkbox> </headertemplate>
  4. <itemtemplate>
  5. <asp:checkbox id="chkSelect" runat="server" Width="30px" SecurityContextID="0" Enabled="False" Checked='<%# DataBinder.Eval(Container, "DataItem.IsSelected") %>' AutoFocus="False" ></asp:checkbox>
  6. </itemtemplate>
  7. <headerstyle width="30px"></headerstyle>
  8. </asp:TemplateColumn>
ASPX.VB
Note : datagrid = "dg"

To fine any data from the datagrid use the following
ASP.NET Syntax (Toggle Plain Text)
  1. Private Function _validateEachEmployeeRow() As String
  2. Dim i As Int16
  3. Dim chkSelect As CheckBox
  4. 'Dim txtEmpName, txt As HASIB.Controls.HasibTextBox
  5. Dim lbl, lblEmpName As HASIB.Controls.HasibLabel
  6. For i = 0 To dg.Items.Count - 1
  7. chkSelect = Me.dg.Items(i).FindControl("chkSelect")
  8. If chkSelect.Checked Then
  9. lblEmpName = Me.dg.Items(i).FindControl("lblEmployeeName")
  10.  
  11. Next
  12. Return lblEmpName
  13. End Function
  14.  

Mark as solved if it helps you!!!
Last edited by reach_yousuf; Oct 14th, 2009 at 1:52 am.
Reputation Points: 12
Solved Threads: 38
Junior Poster
reach_yousuf is offline Offline
194 posts
since Sep 2007
Oct 14th, 2009
0
Re: How can I add a checkbox to a datagrid?
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:
ASP.NET Syntax (Toggle Plain Text)
  1. <%@ Page Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="Calls.aspx.cs" Inherits="Calls" Title="Untitled Page" %>
  2. <asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
  3. </asp:Content>
  4. <asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolderMain" Runat="Server">
  5. <asp:Label ID="lbl_Welcome1" runat="server" Text="Welcome: "></asp:Label>
  6. <asp:Label ID="lbl_Welcome" runat="server" Text="N/A"></asp:Label>
  7. &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
  8. &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
  9. &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
  10. &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<asp:Button ID="btn_Back" runat="server"
  11. OnClick="btn_Back_Click" Text="Back" />
  12. <asp:Button ID="btn_Logout" runat="server" OnClick="btn_Logout_Click" Text="Log Out" /><br />
  13. &nbsp;
  14.  
  15. <asp:GridView ID="dgv_Calls" runat="server" ForeColor="#333333" GridLines="Vertical" ShowFooter="True">
  16. <HeaderStyle BackColor="#5D7B9D" BorderColor="Black" BorderStyle="Double" BorderWidth="2px" Font-Bold="True" ForeColor="White" HorizontalAlign="Center" />
  17. <RowStyle BackColor="#F7F6F3" ForeColor="#333333" HorizontalAlign="Center" VerticalAlign="Middle" />
  18. <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" HorizontalAlign="Center" VerticalAlign="Middle" />
  19. <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
  20. <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
  21. <EditRowStyle BackColor="#999999" />
  22. <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
  23.  
  24. <asp:TemplateColumn>
  25. <headertemplate>
  26. <asp:checkbox id="chkSelectAll" runat="server" Width="30px" Enabled="False" ></asp:checkbox>
  27. </headertemplate>
  28. <itemtemplate><asp:checkbox id="chkSelect" runat="server" Width="30px" SecurityContextID="0" Enabled="False" Checked='<%# DataBinder.Eval(Container, "DataItem.IsSelected") %>' AutoFocus="False" >
  29. </asp:checkbox> </itemtemplate>
  30. <headerstyle width="30px"></headerstyle>
  31. </asp:TemplateColumn>
  32. </asp:GridView>
  33. </asp:Content>

Master Page Code:
ASP.NET Syntax (Toggle Plain Text)
  1. <%@ Master Language="C#" AutoEventWireup="true" CodeFile="MasterPage.master.cs" Inherits="MasterPage" %>
  2.  
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  4.  
  5. <html xmlns="http://www.w3.org/1999/xhtml" >
  6. <head runat="server">
  7. <title>BASF ~ Login</title>
  8.  
  9. <script language="javascript">
  10. window.history.forward(1);
  11. </script>
  12.  
  13. <asp:TemplateColumn>
  14. <headertemplate>
  15. <asp:checkbox id="chkSelectAll" runat="server" Width="30px" Enabled="False" ></asp:checkbox>
  16. </headertemplate>
  17. <itemtemplate><asp:checkbox id="chkSelect" runat="server" Width="30px" SecurityContextID="0" Enabled="False" Checked='<%# DataBinder.Eval(Container, "DataItem.IsSelected") %>' AutoFocus="False" >
  18. </asp:checkbox> </itemtemplate>
  19. <headerstyle width="30px"></headerstyle>
  20. </asp:TemplateColumn>
  21.  
  22. <asp:ContentPlaceHolder id="head" runat="server">
  23. </asp:ContentPlaceHolder>
  24. <link href="Styles.css" rel="stylesheet" type="text/css" />
  25. </head>
Reputation Points: 11
Solved Threads: 0
Junior Poster in Training
phoenix_dwarf is offline Offline
57 posts
since May 2009
Oct 14th, 2009
0
Re: How can I add a checkbox to a datagrid?
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
Reputation Points: 11
Solved Threads: 0
Junior Poster in Training
phoenix_dwarf is offline Offline
57 posts
since May 2009
Oct 14th, 2009
0
Re: How can I add a checkbox to a datagrid?
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
Reputation Points: 11
Solved Threads: 0
Junior Poster in Training
phoenix_dwarf is offline Offline
57 posts
since May 2009
Oct 14th, 2009
0
Re: How can I add a checkbox to a datagrid?
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:

ASP.NET Syntax (Toggle Plain Text)
  1. public void ValidateCheck()
  2. {
  3. int count = 0;
  4. // Select the checkboxes from the GridView control
  5. for (int i = 0; i < dgv_Calls.Rows.Count; i++)
  6. {
  7. GridViewRow row = dgv_Calls.Rows[i];
  8. bool isChecked = ((CheckBox)row.FindControl("chkSelect")).Checked;
  9. if (isChecked)
  10. {
  11. count++;
  12. telD.Text = "YAY!!!" + count;
  13. }
  14. }
  15. //telD.Text = "YAY!!!" + count;
  16. }

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
Reputation Points: 11
Solved Threads: 0
Junior Poster in Training
phoenix_dwarf is offline Offline
57 posts
since May 2009
Oct 15th, 2009
1
Re: How can I add a checkbox to a datagrid?
Make that field data type as bit in database..then you retrieve from database check box will appear for that column..
Reputation Points: 10
Solved Threads: 0
Newbie Poster
maheswari1986 is offline Offline
1 posts
since Oct 2009
Oct 15th, 2009
0
Re: How can I add a checkbox to a datagrid?
Quote ...
Oky all is well now but now i ran into the problem of selecting certain rows and only retrieving those rows data
what is the problem.
Reputation Points: 12
Solved Threads: 8
Junior Poster in Training
guru_sarkar is offline Offline
68 posts
since Jul 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in ASP.NET Forum Timeline: GridView editing
Next Thread in ASP.NET Forum Timeline: Install Silver Light





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC