private void btThem_Click(object sender, EventArgs e)
        {
            for(int i=0;i<dataGridView1.Rows.Count-1;i++)
            {
                
                if(dataGridView1.Rows[i].Cells["Checked"].Value ==true)
                {
                   
                    cmTheoHocLop.Parameters["@X"].Value = tbMSSV.Text;
                    cmTheoHocLop.Parameters["@Y"].Value = dataGridView1.Rows[i].Cells["MaLop"].Value;
                    sqlConnection1.Open();
                    cmTheoHocLop.ExecuteNonQuery();
                    sqlConnection1.Close();
                }
            
            }
           
        }

it has a error :Error 3 Operator '==' cannot be applied to operands of type 'object' and 'bool' D:\lvthao\QLHP\QLHP\FrmDKHP.cs 48 20 QLHP

Thanks so much!!

Recommended Answers

All 16 Replies

Try casting the left side.

if((bool)dataGridView1.Rows[i].Cells["Checked"].Value ==true)

i did it,but it has error: Specified cast is not valid.
Please help me!
Thank you!!

#
if(dataGridView1.Rows.Cells["Checked"].Value ==true)
It is incorect as cells cant be checked

CheckBox cb;
cb = (CheckBox) e.Item.FindControl("CheckBox2");

then check for checkbox if its ok

it's image error

send me whole code then

private void btThem_Click(object sender, EventArgs e)
        {
            
                for (int i = 0; i < dataGridView1.Rows.Count - 1; i++)
                {
                   
                 
                    if ((bool)dataGridView1.Rows[i].Cells["Chon"].Value == true)
                    {

                        cmTheoHocLop.Parameters["@X"].Value = tbMSSV.Text;
                        cmTheoHocLop.Parameters["@Y"].Value = dataGridView1.Rows[i].Cells["MaLop"].Value;
                        sqlConnection1.Open();
                        cmTheoHocLop.ExecuteNonQuery();
                        sqlConnection1.Close();
                    }

                }
           
        }

it has a error :Object reference not set to an instance of an object.

aspx page and code i want to see where is your check box
right now u r taking cell value and not checkbox value

i don't understand !

you just send code what you wrote on btThem_Click
can you send me aspx page also so that i can see where is your datagrid and checkbox

sample of my page

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="AttendanceInputScreen.aspx.cs"
    Inherits="AttendanceMobilePortal.AttendanceInputScreen" %>

<%@ Register Assembly="C1.Web.iPhone.3" Namespace="C1.Web.iPhone.C1Button" TagPrefix="C1Button" %>
<%@ Register Assembly="C1.Web.iPhone.3" Namespace="C1.Web.iPhone.C1PickerView" TagPrefix="C1PickerView" %>
<%@ Register Assembly="C1.Web.iPhone.3" Namespace="C1.Web.iPhone.C1ViewPort" TagPrefix="cc1" %>
<%@ Register Assembly="C1.Web.iPhone.3" Namespace="C1.Web.iPhone.C1NavigationList"
    TagPrefix="C1NavigationList" %>
<%@ Register Assembly="C1.Web.iPhone.3" Namespace="C1.Web.iPhone.C1SegmentedButtons"
    TagPrefix="C1SegmentedButtons" %>
<%@ Register Assembly="C1.Web.iPhone.3" Namespace="C1.Web.iPhone.C1Calendar" TagPrefix="C1Calendar" %>
<%@ Register Assembly="C1.Web.iPhone.3" Namespace="C1.Web.iPhone.C1Dialog" TagPrefix="C1Dialog" %>
<!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>Attendance Input Screen</title>
    <meta name="apple-mobile-web-app-capable" content="yes" />
    <link rel="Stylesheet" href="StyleSheet.css" type="text/css" media="all" />
    <style type="text/css" media="all">
        .C1CustomItemSample
        {
            width: 100%;
            height: 30px;
            display: block;
            position: relative;
        }
        .C1CustomItemSample input
        {
            position: absolute;
            top: 2px;
            left: 145px;
            width: 90px;
            height: 30px;
            -webkit-border-radius: 5px;
            font-family: Arial;
            font-size: 14px; /*border: none;*/
            margin-top: 2px;
        }
        .C1CustomItemSample img
        {
            position: absolute;
            top: 5px;
            left: 260px;
            -webkit-border-radius: 5px;
            font-family: Arial;
            font-size: 16px;
        }
        .session
        {
            position: absolute;
            top: 2px;
            left: 140px;
            width: 125px;
            height: 30px;
            -webkit-border-radius: 5px;
            font-family: Arial;
            font-size: 16px;
            text-align: center;
            color: white;
        }
        .C1Landscape .C1CustomItemSample input
        {
            width: 300px;
        }
        .C1CustomItemSample .C1SampleLabel
        {
            position: absolute;
            margin-left: 10px;
            display: block;
            top: 12px;
            left: 5px;
            font-family: Helvetica;
            font-size: 14px;
            font-weight: bold;
            color: #000000;
            text-shadow: #FFFFFF 0px 1px 1px;
        }
        .C1CustomItemSample .C1SampleRightLabel
        {
            position: absolute;
            display: block;
            top: 12px;
            right: 5px;
            font-weight: bold;
            float: right;
            font-family: Helvetica;
            font-size: 16px;
            font-weight: bold;
            color: #324F85;
            text-shadow: #FFFFFF 0px 1px 1px;
        }
        .C1SampleCustomTextStyle span, .C1SampleCustomTextStyle div
        {
            font-size: 17px;
            font-weight: bold;
            color: #FFFFFF;
            text-shadow: #000000 1px 1px 2px;
        }
        .C1SampleWrongInputItem *
        {
            color: Red;
        }
        .C1SampleApprovedItem *
        {
            color: Green;
        }
        .C1SegmentedButton
        {
            margin-top: 2px !important;
            width: 10px !important;
            padding: 0 !important;
            text-align: center !important;
        }
        .C1Selected .C1TextNode
        {
            color: #FFFFFF !important;
            font-style: normal !important;
        }
        .C1First .C1TextNode
        {
            padding-left: 10px !important;
            font-style: normal !important;
        }
        .C1Last .C1TextNode
        {
            padding-left: 14px !important;
            font-style: normal !important;
        }
        .C1SegmentedButtons_IPhone
        {
            left: 135px !important;
        }
    </style>
</head>
<body onload="fillcurrentdate();">
    <form id="form1" runat="server">
    <asp:ScriptManager ID="ScriptManager1" runat="server" />

    <script language="javascript" type="text/javascript">

    function fillcurrentdate() {
            var date = new Date();
            var dayNumber = date.getDate();
            var monthNumber = date.getMonth() + 1;

            if (dayNumber < 10)
            {
                dayNumber = "0" + dayNumber;
            }
            if (monthNumber < 10)
            {
                monthNumber = "0" + monthNumber;
            }

            document.getElementById("txtRoasterDate").value = monthNumber + "/" + dayNumber + "/" + date.getFullYear();
        }

    function showCalendar() {
            Sys.Application.findComponent("<%=C1Dialog1.ClientID%>").show();
        }

        function onCalendarSelectionChanged(calendar, selectionEventArgs){
            var selectedDates = selectionEventArgs.get_selectedDates();

            document.getElementById("txtRoasterDate").value = (selectedDates[0].getMonth()+1) + "/" + selectedDates[0].getDate() + "/" + selectedDates[0].getFullYear();

            /*var navList = Sys.Application.findComponent("<%=C1NavigationList1.ClientID%>");  
            var newItem = new C1.Web.iPhone.C1NavigationList.C1NavigationListItem();
            navList.get_items().add(newItem);
            newItem.set_text(selectedDates[0]);
            newItem.set_topText("" + selectedDates[0].getFullYear() + " / " + (selectedDates[0].getMonth()+1) + " / " + selectedDates[0].getDay());
            newItem.set_bottomText("Just added");
            newItem.set_commandName("JustAddedDate");            
            */

            Sys.Application.findComponent("<%=C1Dialog1.ClientID%>").hide();

            //document.location.href = "AttendanceInputScreen.aspx?status=take&dt=" + (selectedDates[0].getMonth()+1) + "/" + selectedDates[0].getDate() + "/" + selectedDates[0].getFullYear();
        }
        /*function OnClientItemClick_C1NavigationList1(item) {
            if(item.get_commandName() == "JustAddedDate") {
                $().c1iPhone().c1confirm(function(){item.remove();}, "Remove " + item.get_text() + "?", {ok:"Remove", cancel:"No, cancel"});
            }
        }*/

    function showRosterIDPicker() {
            Sys.Application.findComponent("<%=C1Dialog2.ClientID%>").show();
        }

    function doDialogChoose()
     {  
            // get dialog results and close dialog:
            var oPickerView = Sys.Application.findComponent("<%=C1PickerView_RosterIDs.ClientID%>");    

            //alert(document.getElementById("C1Dialog2_CntPnl_ctl00").value);

            var selectedValue = oPickerView.getSelectedItemValue("rosterids");            
            document.getElementById("txtRoasterID").value = selectedValue

            //_activeItem.set_topText(selectedValue);

            Sys.Application.findComponent("<%=C1Dialog2.ClientID%>").hide();
     }
     function doDialogCancel()
     {
        // close dialog:
         Sys.Application.findComponent("<%=C1Dialog2.ClientID%>").hide(); 
     }

    function validateAndSubmitForm() {
        var dbn = document.getElementById("txtSchoolDBN").value;
        var roasterDate = document.getElementById("txtRoasterDate").value;
        var roasterID = document.getElementById("txtRoasterID").value;
        if(dbn == "")
        {
            alert("School DBN cannot be empty.");
            return false;
        } 
        else if(roasterDate == "") 
        {
            alert("Roaster Date cannot be empty.");
            return false;
        }
        else if(roasterID == "") 
        {
            alert("Roaster ID cannot be empty.");
            return false;
        }
        else
        {
            return true;
        }

        /*__doPostBack();*/
    }
    </script>

    <div class="C1SampleContainer">
        <cc1:C1ViewPort ID="C1ViewPort1" runat="server" Text="Attendance" UrlBarVisible="false"
            ToolBarVisible="true" CssClass="C1SampleContainer">
            <ToolBar>
                <table width="100%">
                    <tr>
                        <td align="right">
                            <img src="images/nyc_logo.gif" height="35px" alt="" />
                        </td>
                    </tr>
                </table>
            </ToolBar>
            <LeftButton ButtonType="Back" Text="Home" NavigateUrl="Default.aspx" />
            <RightButton ID="RightButton1" Text="Log Out" OnClick="OnLogOutClick" runat="server"
                AutoPostBack="true" />
            <Content>
                <C1NavigationList:C1NavigationList ID="C1NavigationList1" runat="server" NavigationListType="RoundedCornersList"
                    LoadOnDemand="false" TrackItemsStructure="true">
                    <Items>
                        <C1NavigationList:C1NavigationListGroupItem ID="C1NavigationListGroupItem1" runat="server">
                            <Items>
                                <C1NavigationList:C1NavigationListItem ID="C1NavigationListItem1" runat="server">
                                    <Template>
                                        <div class="C1CustomItemSample">
                                            <span class="C1SampleLabel">School DBN</span>
                                            <input type="text" id="txtSchoolDBN" name="txtSchoolDBN" size="12" value="11X545" />
                                        </div>
                                    </Template>
                                </C1NavigationList:C1NavigationListItem>
                                <C1NavigationList:C1NavigationListItem ID="C1NavigationListItem2" runat="server">
                                    <Template>
                                        <div class="C1CustomItemSample">
                                            <span class="C1SampleLabel">Roster Date</span>
                                            <input type="text" id="txtRoasterDate" name="txtRoasterDate" size="12" readonly="readonly"
                                                onclick="javascript:showCalendar();" />
                                            &nbsp;&nbsp;
                                            <img src="images/calendar.png" title="click here to select date" onclick="javascript:showCalendar();"
                                                alt="" />
                                        </div>
                                    </Template>
                                </C1NavigationList:C1NavigationListItem>
                                <C1NavigationList:C1NavigationListItem ID="C1NavigationListItem3" runat="server">
                                    <Template>
                                        <div class="C1CustomItemSample">
                                            <span class="C1SampleLabel">Roaster Identifier</span>
                                            <input type="text" id="txtRoasterID" name="txtRoasterID" size="12" value="H8R-01"
                                                onclick="javascript:showRosterIDPicker();" />
                                        </div>
                                    </Template>
                                </C1NavigationList:C1NavigationListItem>
                                <C1NavigationList:C1NavigationListItem ID="C1NavigationListItem4" runat="server">
                                    <Template>
                                        <div class="C1CustomItemSample">
                                            <span class="C1SampleLabel">Session</span>
                                            <C1SegmentedButtons:C1SegmentedButtons CssClass="session" ID="C1SegmentedButtons1"
                                                runat="server">
                                                <Buttons>
                                                    <C1SegmentedButtons:C1SegmentedButton Text="AM" Height="38px" Width="40px" />
                                                    <C1SegmentedButtons:C1SegmentedButton Text="PM" Height="38px" Width="40px" />
                                                </Buttons>
                                            </C1SegmentedButtons:C1SegmentedButtons>
                                        </div>
                                    </Template>
                                </C1NavigationList:C1NavigationListItem>
                            </Items>
                        </C1NavigationList:C1NavigationListGroupItem>
                    </Items>
                </C1NavigationList:C1NavigationList>
            </Content>
            <RightButton ID="RightButton"></RightButton>
        </cc1:C1ViewPort>
    </div>
    <C1Dialog:C1Dialog runat="server" ID="C1Dialog1" DisplayVisible="false">
        <Content>
            <br />
            <div id="DialogTitle1" style="width: 100%; text-align: center; font-family: Helvetica;
                color: #FFFFFF; font-size: 16px; font-weight: bold;">
                Choose date.
            </div>
            <br />
            <C1Calendar:C1Calendar runat="server" ID="C1Calendar1" 
                OnClientSelectedDatesChanged="onCalendarSelectionChanged">
            </C1Calendar:C1Calendar>
            <br />
        </Content>
    </C1Dialog:C1Dialog>
    <C1Dialog:C1Dialog runat="server" ID="C1Dialog2" DisplayVisible="false">
        <Content>
            <br />
            <div id="Div1" style="width: 100%; text-align: center; font-family: Helvetica; color: #FFFFFF;
                font-size: 16px; font-weight: bold;">
                Choose Roster ID.
            </div>
            <br />
            <C1PickerView:C1PickerView runat="server" ID="C1PickerView_RosterIDs" DisplayVisible="true">
                <Pickers>
                    <C1PickerView:C1Picker Name="rosterids" Width="150px"></C1PickerView:C1Picker>
                </Pickers>
            </C1PickerView:C1PickerView>
            <br />
            <div style="width: 90%; margin-left: 5%; margin-right: 5%">
                <c1button:c1button id="C1Button1" runat="server" text="Choose" buttontype="Primary"
                    width="97%" onclientclick="doDialogChoose" />
                <br />
                <br />
                <c1button:c1button id="C1Button2" runat="server" text="Cancel" buttontype="Destructive"
                    width="97%" onclientclick="doDialogCancel" />
            </div>
            <br />
        </Content>
    </C1Dialog:C1Dialog>
    </form>
</body>
</html>
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace QLHP
{
    public partial class FrmDKHP : Form
    {
        public FrmDKHP()
        {
            InitializeComponent();
        }

        private void FrmDKHP_Load(object sender, EventArgs e)
        {
            daNganhHoc.Fill(myDS1.NganhHoc);
        }
        private void btHienThi_Click(object sender, EventArgs e)
        {

            daLopHoc.SelectCommand.Parameters["@A"].Value = cbTenNganh.SelectedValue;
            daLopHoc.SelectCommand.Parameters["@B"].Value = cbHocKy.SelectedItem;
            //daLopHoc.SelectCommand.Parameters["@D"].Value = tbNam.Text;
            daLopHoc.Fill(myDS1.LopHoc);
            /*for (int i = 0; i < dataGridView1.Rows.Count - 1; i++)
            {
                dataGridView1.Rows[i].Cells["Chon"].Value = true;
            }*/
        }

        private void btThoat_Click(object sender, EventArgs e)
        {
            this.Hide();
            FrmPDT f2 = new FrmPDT();
            f2.ShowDialog();
            this.Close();
        }

        private void btThem_Click(object sender, EventArgs e)
        {
            
                for (int i = 0; i < dataGridView1.Rows.Count - 1; i++)
                {
                   
                 
                    if ((bool)(dataGridView1.Rows[i].Cells["Chon"]).Value == true)
                    {

                        cmTheoHocLop.Parameters["@X"].Value = tbMSSV.Text;
                        cmTheoHocLop.Parameters["@Y"].Value = dataGridView1.Rows[i].Cells["MaLop"].Value;
                        sqlConnection1.Open();
                        cmTheoHocLop.ExecuteNonQuery();
                        sqlConnection1.Close();
                      
                    }

                }
           
        }

        private void btClear_Click(object sender, EventArgs e)
        {
            myDS1.TheoHocLop.Clear();
        }

       

        
    }
}

Please help me!
Thanks!

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace QLHP
{
    public partial class FrmDKHP : Form
    {
        public FrmDKHP()
        {
            InitializeComponent();
        }

        private void FrmDKHP_Load(object sender, EventArgs e)
        {
            daNganhHoc.Fill(myDS1.NganhHoc);
        }
        private void btHienThi_Click(object sender, EventArgs e)
        {

            daLopHoc.SelectCommand.Parameters["@A"].Value = cbTenNganh.SelectedValue;
            daLopHoc.SelectCommand.Parameters["@B"].Value = cbHocKy.SelectedItem;
            //daLopHoc.SelectCommand.Parameters["@D"].Value = tbNam.Text;
            daLopHoc.Fill(myDS1.LopHoc);
            /*for (int i = 0; i < dataGridView1.Rows.Count - 1; i++)
            {
                dataGridView1.Rows[i].Cells["Chon"].Value = true;
            }*/
        }

        private void btThoat_Click(object sender, EventArgs e)
        {
            this.Hide();
            FrmPDT f2 = new FrmPDT();
            f2.ShowDialog();
            this.Close();
        }

        private void btThem_Click(object sender, EventArgs e)
        {
            
                for (int i = 0; i < dataGridView1.Rows.Count - 1; i++)
                {
                   
                 
                    if ((bool)(dataGridView1.Rows[i].Cells["Chon"]).Value == true)
                    {

                        cmTheoHocLop.Parameters["@X"].Value = tbMSSV.Text;
                        cmTheoHocLop.Parameters["@Y"].Value = dataGridView1.Rows[i].Cells["MaLop"].Value;
                        sqlConnection1.Open();
                        cmTheoHocLop.ExecuteNonQuery();
                        sqlConnection1.Close();
                      
                    }

                }
           
        }

        private void btClear_Click(object sender, EventArgs e)
        {
            myDS1.TheoHocLop.Clear();
        }

       

        
    }
}

Please help me!
Thanks!

You have the parentheses arount the wrong part in the casting.

if ((bool)(dataGridView1.Rows.Cells["Chon"]).Value == true)

Try this

if ((bool)(dataGridView1.Rows.Cells["Chon"].Value) == true)

I try it,it has the same error.But i did it successful!

if(dataGridView1.Rows[i].Cells["Chon"].Selected==true)

Thanks for ur post!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.