DataGridView Checkbox Issue
Hello Gentleman,
I am trying to use a checkbox column in the DataGridView to identify a column so that I can extract a line of information from it to use in an SQL parameter. However, I can not find a simply way to identify when the column is in a checked state. The code below is producing the error :
Operator '==' cannot be applied to operands of type 'object' and 'bool'
This does not make sense to me, since the same code is used (with a single equal sign) to set the checked state of the box. Any assistance would be greatly appreciated...Pat
int count = dataGridViewEMail.Rows.Count;
string mailId = "";
for (int i = 0; i < count; i++)
{
if (dataGridViewEMail.Rows[i].Cells["Delete"].Value == true)
{
mailId = dataGridViewEMail["Id", i].ToString();
}
}
Thank You..:-)
bhargavPosted Apr 18, 2007, 8:25 AM
ckeckboxes.aspx
=============
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 id="Head1" runat="server"> <title>Untitled Pagetitle> <script language="javascript" type="text/javascript"> function ChangeCheckBoxState(id, checkState){
var cb = document.getElementById(id); if (cb != null)cb.checked = checkState;
}
function HeaderCheckbox(chkHeader){
var cb = document.getElementById(chkHeader); if(cb != null){
if(cb.checked){
checkAll(
'1');}
elsecheckAll(
'0');}
}
function checkAll(checkStates){
var checkState = false; if(checkStates == 1)checkState =
true; if (AllCheckBoxs != null){
for (var i = 0; i < AllCheckBoxs.length; i++)ChangeCheckBoxState(AllCheckBoxs[i], checkState);
}
}
function popups(){
window.open(
"..\Admin\ViewDatagridDetails.aspx","mywindow","menubar=0,resizable=0,width=350,height=250,scrollbars=yes,resizable=yes");}
script> head><
body> <form id="form1" runat="server"> <div> <%--SelectCommand="SELECT * FROM [tblEnquiry]">
left: 24px; position: absolute; top: 116px"
Text="View Enquiry Details" /> <asp:Panel ID="Laxmi" runat="server" BackColor="#E0E0E0" Font-Bold="True" Font-Italic="True" Font-Size="X-Large" ForeColor="Red" Height="82px" Style="z-index: 101; left: 0px;position: absolute; top: 0px"
Width="200%"> <br /> Laxmi Hosting Administrator Panelasp:Panel> <asp:Label ID="lblTotalRows" runat="server" Style="z-index: 102; left: 37px; position: absolute;top: 86px">
asp:Label> <asp:GridView ID="gvDetails" runat="server" BackColor="#DEBA84" BorderColor="#DEBA84" BorderStyle="None" BorderWidth="1px" CellPadding="3" CellSpacing="2" Style="z-index: 103;left: 15px; position: absolute; top: 157px"
AutoGenerateColumns="False"> <FooterStyle BackColor="#F7DFB5" ForeColor="#8C4510" /> <RowStyle BackColor="#FFF7E7" ForeColor="#8C4510" /> <SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="White" /> <PagerStyle ForeColor="#8C4510" HorizontalAlign="Center" /> <HeaderStyle BackColor="#A55129" Font-Bold="True" ForeColor="White" /> <Columns > <asp:TemplateField > <HeaderTemplate> <asp:CheckBox ID="chkHeader" runat="server" /> HeaderTemplate> <ItemTemplate > <asp:CheckBox ID="chkCheck" runat="server" /> ItemTemplate> asp:TemplateField> <asp:TemplateField HeaderText = "EnquiryID"> <ItemTemplate > <asp:Label ID="lblEnquiryId" runat="server" Text='<%# GetData(Eval("EnquiryID")) %>' /> ItemTemplate> asp:TemplateField> <asp:TemplateField HeaderText = "Name" > <ItemTemplate > <asp:Label ID="lblName" runat="server" Text='<%# GetData(Eval("Name")) %>' Visible="true" /> ItemTemplate> asp:TemplateField> <asp:TemplateField HeaderText = "Email ID" > <ItemTemplate > <asp:Label ID="lblEmail" runat="server" Text='<%# GetData(Eval("Email")) %>' Visible="true" /> ItemTemplate> asp:TemplateField> <asp:TemplateField HeaderText = "Mobile No." > <ItemTemplate > <asp:Label ID="lblMobile" runat="server" Text='<%# GetData(Eval("Mobile")) %>' Visible="true" /> ItemTemplate> asp:TemplateField> <asp:TemplateField HeaderText = "URL" > <ItemTemplate > <asp:Label ID="lblURL" runat="server" Text='<%# GetData(Eval("URL")) %>' Visible="true" /> ItemTemplate> asp:TemplateField> <asp:TemplateField HeaderText = "Landline" Visible="False" > <ItemTemplate > <asp:Label ID="lblLandline" runat="server" Text='<%# GetData(Eval("Landline")) %>' Visible="false" /> ItemTemplate> asp:TemplateField> <asp:TemplateField HeaderText = "Country" Visible="False" > <ItemTemplate > <asp:Label ID="lblCountry" runat="server" Text='<%# GetData(Eval("Country")) %>' Visible="false" /> ItemTemplate> asp:TemplateField> <asp:TemplateField HeaderText = "Address" Visible="False" > <ItemTemplate > <asp:Label ID="lblAddress" runat="server" Text='<%# GetData(Eval("Address")) %>' Visible="false" /> ItemTemplate> asp:TemplateField> <asp:TemplateField HeaderText = "Platform" Visible="False" > <ItemTemplate > <asp:Label ID="lblPlatform" runat="server" Text='<%# GetData(Eval("Platform")) %>' Visible="false" /> ItemTemplate> asp:TemplateField> <asp:TemplateField HeaderText = "HostingPlan" Visible="False" > <ItemTemplate > <asp:Label ID="lblHostingPlan" runat="server" Text='<%# GetData(Eval("HostingPlan")) %>' Visible="false" /> ItemTemplate> asp:TemplateField> <asp:TemplateField HeaderText = "No. of Years" Visible="False" > <ItemTemplate > <asp:Label ID="lblYears" runat="server" Text='<%# GetData(Eval("Years")) %>' Visible="false" /> ItemTemplate> asp:TemplateField> <asp:TemplateField HeaderText = "WebsiteSize" Visible="False" > <ItemTemplate > <asp:Label ID="lblWebsiteSize" runat="server" Text='<%# GetData(Eval("WebsiteSize")) %>' Visible="false" /> ItemTemplate> asp:TemplateField> <asp:TemplateField HeaderText = "RegistrationType" Visible="False" > <ItemTemplate > <asp:Label ID="lblRegistrationType" runat="server" Text='<%# GetData(Eval("RegistrationType")) %>' Visible="false" /> ItemTemplate> asp:TemplateField> <asp:TemplateField HeaderText = "ServiceType" Visible="False" > <ItemTemplate > <asp:Label ID="lblServiceType" runat="server" Text='<%# GetData(Eval("ServiceType")) %>' Visible="false" /> ItemTemplate> asp:TemplateField> <asp:TemplateField HeaderText = "Description" Visible="False" > <ItemTemplate > <asp:Label ID="lblDescription" runat="server" Text='<%# GetData(Eval("Description")) %>' Visible="false" /> ItemTemplate> asp:TemplateField> <asp:TemplateField HeaderText = "LITKnowsBy" Visible="False" > <ItemTemplate > <asp:Label ID="lblLITKnowsBy" runat="server" Text='<%# GetData(Eval("LITKnowsBy")) %>' Visible="false" /> ItemTemplate> asp:TemplateField> <asp:TemplateField HeaderText="View Profile"> <ItemTemplate> <asp:HyperLink ID="hplViewEmpleyerProfile" runat="server" Text="View" NavigateUrl='<%# NavigateToViewPostedJob(Eval("EnquiryID")) %>'> asp:HyperLink> ItemTemplate> asp:TemplateField> Columns> asp:GridView> <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:LITHostingConnectionString %>" SelectCommand="SELECT [EnquiryID], [Name] FROM [tblEnquiry]">asp:SqlDataSource> <asp:Button ID="btnReplyMail" runat="server" Style="z-index: 104; left: 295px; position: absolute;top: 114px"
Text="Reply For Selected One" /> <asp:Button ID="btnSendMail" runat="server" Style="z-index: 105; left: 518px; position: absolute;top: 118px"
Text="Send Mail To Selected People" /> <asp:Button ID="btnTest" runat="server" OnClick="btnTest_Click" Style="z-index: 106;left: 814px; position: absolute; top: 118px"
Text="Test" /> <asp:Label ID="lblStatus" runat="server" Style="z-index: 107; left: 22px; position: absolute;top: 368px">
asp:Label> <asp:Label ID="lblStoreName" runat="server" Style="z-index: 108; left: 87px; position: absolute;top: 409px">
asp:Label> div> form> body> html>ckeckboxes.aspx.cs
===============
using
System;using
System.Data;using
System.Configuration;using
System.Collections;using
System.Web;using
System.Web.Security;using
System.Web.UI;using
System.Web.UI.WebControls;using
System.Web.UI.WebControls.WebParts;using
System.Web.UI.HtmlControls;using
System.Configuration;using
System.Data.SqlClient;using
System.Text;public
partial class Admin_Datagridtest : System.Web.UI.Page{
protected void Page_Load(object sender, EventArgs e){
}
protected void btnSubmit_Click(object sender, EventArgs e){
string constring = ConfigurationSettings.AppSettings["constring"]; SqlConnection con = new SqlConnection(constring);con.Open();
SqlCommand cmd = new SqlCommand("select * from tblEnquiry", con); SqlDataReader dr;dr = cmd.ExecuteReader();
if (dr.Read()){
gvDetails.DataSource = dr;
gvDetails.DataBind();
//foreach (GridViewRow gvRow in gvDetails.Rows) //{ // CheckBox cb = (CheckBox)gvRow.FindControl("chkCheck"); // ClientScript.RegisterArrayDeclaration("AllCheckBoxes", "'" + cb.ClientID + "'"); //}dr.Close();
}
string sql = "select count(*) from tblEnquiry"; SqlCommand cmd1 = new SqlCommand(sql, con);lblTotalRows.Text =
"Total No. of Records in Enquiry form is :" + cmd1.ExecuteScalar();con.Close();
}
public string GetData(object o){
string str = Convert.ToString(o); if ((str == null) || (str == string.Empty) || (str == "-1.00")) return "-"; else return str.Trim();}
public string NavigateToViewPostedJob(object o){
string str = o.ToString(); //Session["Employer"] = str; //Label lblEnquiryIdVal = (Label)myCbSelected.FindControl("lblEnquiryId"); //Label lblNameVal = (Label)myCbSelected.FindControl("lblName"); //Label lblEmailVal = (Label)myCbSelected.FindControl("lblEmail"); //Label lblMobileVal = (Label)myCbSelected.FindControl("lblMobile"); //Label lblLandlineVal = (Label)myCbSelected.FindControl("lblLandline"); //Label lblCountryVal = (Label)myCbSelected.FindControl("lblCountry"); //Label lblAddressVal = (Label)myCbSelected.FindControl("lblAddress"); //Label lblPlatformVal = (Label)myCbSelected.FindControl("lblPlatform"); //Label lblHostingPlanVal = (Label)myCbSelected.FindControl("lblHostingPlan"); //Label lblYearsVal = (Label)myCbSelected.FindControl("lblYears"); //Label lblURLVal = (Label)myCbSelected.FindControl("lblURL"); //Label lblWebsiteSizeVal = (Label)myCbSelected.FindControl("lblWebsiteSize"); //Label lblRegistrationTypeVal = (Label)myCbSelected.FindControl("lblRegistrationType"); //Label lblServiceTypeVal = (Label)myCbSelected.FindControl("lblServiceType"); //Label lblDescriptionVal = (Label)myCbSelected.FindControl("lblDescription"); //Label lblLITKnowsByVal = (Label)myCbSelected.FindControl("lblLITKnowsBy"); //lblStoreName.Text += "" + " You Have Selected :" + // "" + // "Enquiry ID
Name
Email ID
Mobile No
Landline
Country
Address
Platform
Hosting Plan
Years
Domain Name
Site Size
Registration Type
Type Of Services Requesting
Description
Laxmi Hosting Knows By
Session[
"rec"] = lblStoreName.Text; return "ViewDatagridDetails.aspx?RecordID=" + str.Trim();}
protected void btnTest_Click(object sender, EventArgs e){
lblStatus.Text =
"";lblStoreName.Text =
"";lblStatus.Text =
"You selected the Following items:
"; StringBuilder EnquiryIds = new StringBuilder(); foreach (GridViewRow myCbSelected in gvDetails.Rows)
{
CheckBox chkSelected = (CheckBox)myCbSelected.FindControl("chkCheck"); if (chkSelected.Checked == true){
Label lblEnquiryIdVal = (Label)myCbSelected.FindControl("lblEnquiryId"); Label lblNameVal = (Label)myCbSelected.FindControl("lblName"); Label lblEmailVal = (Label)myCbSelected.FindControl("lblEmail"); Label lblMobileVal = (Label)myCbSelected.FindControl("lblMobile"); Label lblLandlineVal = (Label)myCbSelected.FindControl("lblLandline"); Label lblCountryVal = (Label)myCbSelected.FindControl("lblCountry"); Label lblAddressVal = (Label)myCbSelected.FindControl("lblAddress"); Label lblPlatformVal = (Label)myCbSelected.FindControl("lblPlatform"); Label lblHostingPlanVal = (Label)myCbSelected.FindControl("lblHostingPlan"); Label lblYearsVal = (Label)myCbSelected.FindControl("lblYears"); Label lblURLVal = (Label)myCbSelected.FindControl("lblURL"); Label lblWebsiteSizeVal = (Label)myCbSelected.FindControl("lblWebsiteSize"); Label lblRegistrationTypeVal = (Label)myCbSelected.FindControl("lblRegistrationType"); Label lblServiceTypeVal = (Label)myCbSelected.FindControl("lblServiceType"); Label lblDescriptionVal = (Label)myCbSelected.FindControl("lblDescription"); Label lblLITKnowsByVal = (Label)myCbSelected.FindControl("lblLITKnowsBy");lblStoreName.Text +=
"" + " You Have Selected :" + "" + "Enquiry ID
Name
Email ID
Mobile No
Landline
Country
Address
Platform
Hosting Plan
Years
Domain Name
Site Size
Registration Type
Type Of Services Requesting
Description
Laxmi Hosting Knows By
"; //string s=Convert.ToString(lblStoreName.Text); //Response.Write(s);
Session[
"rec"] = lblStoreName.Text;}
}
}
protected void gvDetails_DataBound(object sender, EventArgs e){
CheckBox cb = (CheckBox)gvDetails.HeaderRow.FindControl("chkHeader");cb.Attributes.Add(
"onclick", "HeaderCheckbox('" + cb.ClientID + "')");}
}
SimonPosted Apr 7, 2007, 9:16 AM