更新时间:2023-02-15 13:39:26
如果复选框已选中,则只应调用插入,即为True ...DataGridViewCheckBoxCell ch1 = new DataGridViewCheckBoxCell();
ch1 =(DataGridViewCheckBoxCell)DGv_Session.Rows [i] .Cells [1];
if(ch1.Value!= null&& bool.Parse(ch1.Value.ToString())== true)
...
我个人会添加一个小帮手功能private bool IsTrue(DataGridViewCell cb)
{
DataGridViewCheckBoxCell tcb =(DataGridViewCheckBoxCell)cb;
bool bRet = false ;
if (tcb.Value!= null && bool .Parse(tcb.Value.ToString())== true )
bRet = 真跨度>;
return bRet;
}}
这将使你的相当大的if语句更容易阅读...if(IsTrue(DGv_Session [1,i])&& IsTrue(DGv_Session [2,i])&& IsTrue(DGv_Session [3,i])&& IsTrue(DGv_Session [4, I]))
validating checkbox which are not checked in the datagridview those record should not be save in the database.
Save code as follows;
for (int i = 0; i < DGv_Session.RowCount; i++)
{
try
{
if (DGv_Session[1, i].Value != null && DGv_Session[1, i].Value != "" && DGv_Session[2, i].Value != null && DGv_Session[2, i].Value != "" && DGv_Session[3, i].Value != null && DGv_Session[3, i].Value != "" && DGv_Session[4, i].Value != null && DGv_Session[4, i].Value != "")
{
sql = "insert into Tb_Session_Structure ([Cmn_Minor_code],[Days],[Session1],[Session2],[Session3],[Session4])";
sql = sql + " values('" + cb_Course_Code.Text + "',
'" + DGv_Session.Rows[i].Cells[0].Value.ToString() + "',
" + (Convert.ToBoolean(DGv_Session[1, i].Value) == true).ToString() + ",
" + (Convert.ToBoolean(DGv_Session[2, i].Value) == true).ToString() + ",
" + (Convert.ToBoolean(DGv_Session[3, i].Value) == true).ToString() + ",
" + (Convert.ToBoolean(DGv_Session[4, i].Value) == true).ToString() + ")";
GFun.Error = "";
GFun.InsertAccessData(sql);
if (GFun.Error.ToString() != "")
{
MessageBox.Show(GFun.Error.ToString(), "Error");
return;
}
GFun.OleDbCon.Close();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString(), "Error");
return;
}
}
I am validating check box which are not checked in the datagridview that value not to be saved in the database.
Days 1 2
1 checkboxunchecked checkboxunchecked
2 checkboxchecked checkboxchecked
after enters the above records into the database and click the save button.
In the database records show as follows;
Days 1 2
1 checkboxunchecked checkboxunchecked
2 checkboxchecked checkboxchecked
but i want which checkbox which which are not checked in the datagridview do not be saved in the database.
I want the final output as follows after made entries and click the save button records in the database as follows;
Final ouput as follows;
Days 1 2
2 checkboxchecked checkboxchecked
You should only call the insert if the checkbox is checked i.e. is True ...DataGridViewCheckBoxCell ch1 = new DataGridViewCheckBoxCell(); ch1 = (DataGridViewCheckBoxCell)DGv_Session.Rows[i].Cells[1]; if(ch1.Value != null && bool.Parse(ch1.Value.ToString()) == true) ...
Personally I would add a little helper functionprivate bool IsTrue(DataGridViewCell cb) { DataGridViewCheckBoxCell tcb = (DataGridViewCheckBoxCell)cb; bool bRet = false; if (tcb.Value != null && bool.Parse(tcb.Value.ToString()) == true) bRet = true; return bRet; }}
which would make your rather large if statement easier to read...if (IsTrue(DGv_Session[1, i]) && IsTrue(DGv_Session[2, i]) && IsTrue(DGv_Session[3, i]) && IsTrue(DGv_Session[4, i]))