[英]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.