使用复选框字符串删除数据网格视图中的多行未被识别为有效的布尔值

本文关键字:识别 布尔值 有效 字符串 复选框 删除 数据 视图 网格 数据网 | 更新日期: 2023-09-27 18:32:35

请帮帮我,我卡在 C# winForms 中的数据网格视图中,我的 SQL 表如下所示

empcode-varchar(50)
fullname-varchar(50)
month-date
branch-varchar(50)
designation-varchar(50)
id-varchar(50)
accountno-nvarchar(50)
paymenttype-nvarchar(50)
basicsal-int
ca-int
hra-int
sa-int
totalsalary-int
allowanceid-int(IDENTITY COLUMN)
remark-nvarchar(50)

对于这个表,我取了两个按钮,一个是查看数据,另一个是删除我的视图数据编码部分工作正常,如下所示:-

SqlDataAdapter da = new SqlDataAdapter("select * from allowance", cn);
            dt = new System.Data.DataTable();
            da.Fill(dt);
            dg2.Rows.Clear();
            foreach (DataRow item in dt.Rows)
            {
                int n = dg2.Rows.Add();
                dg2.Rows[n].Cells[0].Value = false;
                dg2.Rows[n].Cells[1].Value = item["empcode"].ToString();
                dg2.Rows[n].Cells[2].Value = item["fullname"].ToString();
                dg2.Rows[n].Cells[3].Value = item["month"].ToString();
                dg2.Rows[n].Cells[4].Value = item["branch"].ToString();
                dg2.Rows[n].Cells[5].Value = item["designation"].ToString();
                dg2.Rows[n].Cells[6].Value = item["id"].ToString();
                dg2.Rows[n].Cells[7].Value = item["accountno"].ToString();
                dg2.Rows[n].Cells[8].Value = item["paymenttype"].ToString();
                dg2.Rows[n].Cells[9].Value = item["basicsal"].ToString();
                dg2.Rows[n].Cells[10].Value = item["ca"].ToString();
                dg2.Rows[n].Cells[11].Value = item["hra"].ToString();
                dg2.Rows[n].Cells[12].Value = item["sa"].ToString();
                dg2.Rows[n].Cells[13].Value = item["totalsalary"].ToString();
                dg2.Rows[n].Cells[14].Value = item["allowanceid"].ToString();
                dg2.Rows[n].Cells[15].Value = item["remark"].ToString();
}

另一个按钮"删除"代码看起来像这样:-

foreach (DataGridViewRow itemRow in dg2.Rows)
                {
                    if (bool.Parse(itemRow.Cells[14].Value.ToString()))
                    {
        da = new SqlDataAdapter("DELETE FROM allowance WHERE allowanceid = '" + itemRow.Cells[14].Value.ToString() + "'", cn);
                        DataTable bb = new DataTable();
                        da.Fill(bb);
                        }
                    }
                    MessageBox.Show("SuccessFully DELETED.....!");

使用复选框字符串删除数据网格视图中的多行未被识别为有效的布尔值

foreach (DataGridViewRow itemRow in dg2.Rows)
                    {
                        if (!itemRow.IsNewRow)
                        {
                            if ((bool)itemRow.Cells[0].EditedFormattedValue)
                            {
 da = new SqlDataAdapter("DELETE FROM allowance WHERE allowanceid = " + Convert.ToInt32(itemRow.Cells[14].Value) + "", cn);
                                DataTable bb = new DataTable();
                                da.Fill(bb);

试试这个

foreach (DataGridViewRow itemRow in dg2.Rows)
                {
                    if (bool.Parse(Convert.Tostring(itemRow.Cells[14].Value)))
                    {
        da = new SqlDataAdapter("DELETE FROM allowance WHERE allowanceid = '" + Convert.Tostring(itemRow.Cells[14].Value) + "'", cn);
                        DataTable bb = new DataTable();
                        da.Fill(bb);
                        }
                    }
                    MessageBox.Show("SuccessFully DELETED.....!");

在 DaataError 事件中连接此代码

 private void DGData_DataError(object sender, DataGridViewDataErrorEventArgs anError)
        {
            if ((anError.Exception) is ConstraintException)
            {
                DataGridView grd1 = (DataGridView)sender;
                grd1.Rows[anError.RowIndex].ErrorText = "an error";
                grd1.Rows[anError.RowIndex].Cells[anError.ColumnIndex].ErrorText = "an error";
                anError.ThrowException = false;
            }
        }
  }