簡體   English   中英

使用checkboxes-string刪除datagridview中的多行未識別為有效的布爾值

[英]Deleting multiple rows in datagridview using checkboxes-string was not recognized as a valid boolean

請幫我,我在C#Winforms中卡在datagridview中,其中我的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)

對於此表,我使用了兩個按鈕,一個是viewdata,另一個是刪除我的視圖數據編碼部分,工作正常,如下所示:

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 (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;
            }
        }
  }
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);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM