How can I add a checkbox to a datagrid?

Please support our ASP.NET advertiser: Intel Parallel Studio Home
Reply

Join Date: May 2009
Posts: 40
Reputation: phoenix_dwarf is an unknown quantity at this point 
Solved Threads: 0
phoenix_dwarf phoenix_dwarf is offline Offline
Light Poster

How can I add a checkbox to a datagrid?

 
0
  #1
Oct 13th, 2009
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)

  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.
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 51
Reputation: Dhaneshnm is an unknown quantity at this point 
Solved Threads: 6
Dhaneshnm's Avatar
Dhaneshnm Dhaneshnm is offline Offline
Junior Poster in Training
 
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.
  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...
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 205
Reputation: carobee is an unknown quantity at this point 
Solved Threads: 11
carobee carobee is offline Offline
Posting Whiz in Training
 
0
  #3
Oct 13th, 2009
you can use item template.
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 66
Reputation: reach_yousuf is an unknown quantity at this point 
Solved Threads: 12
reach_yousuf reach_yousuf is offline Offline
Junior Poster in Training
 
1
  #4
Oct 14th, 2009
Hello

You can use the following code
ASPX
  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
  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.
Yousuf
Software Developer
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 40
Reputation: phoenix_dwarf is an unknown quantity at this point 
Solved Threads: 0
phoenix_dwarf phoenix_dwarf is offline Offline
Light Poster
 
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:
  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:
  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>
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 40
Reputation: phoenix_dwarf is an unknown quantity at this point 
Solved Threads: 0
phoenix_dwarf phoenix_dwarf is offline Offline
Light Poster
 
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
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 40
Reputation: phoenix_dwarf is an unknown quantity at this point 
Solved Threads: 0
phoenix_dwarf phoenix_dwarf is offline Offline
Light Poster
 
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
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 40
Reputation: phoenix_dwarf is an unknown quantity at this point 
Solved Threads: 0
phoenix_dwarf phoenix_dwarf is offline Offline
Light Poster
 
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:

  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
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 1
Reputation: maheswari1986 is an unknown quantity at this point 
Solved Threads: 0
maheswari1986 maheswari1986 is offline Offline
Newbie Poster
 
1
  #9
Oct 15th, 2009
Make that field data type as bit in database..then you retrieve from database check box will appear for that column..
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 40
Reputation: guru_sarkar is an unknown quantity at this point 
Solved Threads: 6
guru_sarkar guru_sarkar is offline Offline
Light Poster
 
0
  #10
Oct 15th, 2009
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.
Reply With Quote Quick reply to this message  
Reply


Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC