How can I add a checkbox to a datagrid?
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)
//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";
phoenix_dwarf
Junior Poster in Training
57 posts since May 2009
Reputation Points: 11
Solved Threads: 0
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.
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...
Dhaneshnm
Junior Poster in Training
52 posts since Mar 2009
Reputation Points: 22
Solved Threads: 7
Well thanx for all the replies but reach_yousuf your code gives an error by me...it says that the 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:
<%@ 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" />
<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:
<%@ 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>
phoenix_dwarf
Junior Poster in Training
57 posts since May 2009
Reputation Points: 11
Solved Threads: 0
phoenix_dwarf
Junior Poster in Training
57 posts since May 2009
Reputation Points: 11
Solved Threads: 0
phoenix_dwarf
Junior Poster in Training
57 posts since May 2009
Reputation Points: 11
Solved Threads: 0
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:
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?
phoenix_dwarf
Junior Poster in Training
57 posts since May 2009
Reputation Points: 11
Solved Threads: 0
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.
guru_sarkar
Junior Poster in Training
70 posts since Jul 2008
Reputation Points: 12
Solved Threads: 8
Well if i create a bit column it retrives the data from the database and shows it as a checked or non-checked box but it is grayed out....i saw that if you want to check or uncheck the box you must then enable 'edi't but this works only row for row in other words you can only check or uncheck when you selected one row to edit - finish the edit and then move forward to the next row an do the whole cycle again...(which is very frustrating)
phoenix_dwarf
Junior Poster in Training
57 posts since May 2009
Reputation Points: 11
Solved Threads: 0