在datagridview复选框值null不保存在数据库中 [英] in datagridview checkbox value null not be saved in the database

查看:93
本文介绍了在datagridview复选框值null不保存在数据库中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

验证未在datagridview中检查的复选框,这些记录不应保存在数据库中。





保存代码如下;



  for  int  i =  0 ; i <  DGv_Session.RowCount; i ++)
{
尝试
{
if (DGv_Session [ 1 ,i] .Value!= null &&& DGv_Session [ 1 ,i] .Value!= &&& DGv_Session [< span class =code-digit> 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 = 插入到Tb_Session_Structure([Cmn_Minor_code],[天],[SESSION1],[会话2],[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(), 错误);
return ;
}
GFun.OleDbCon.Close();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString( ), 错误);
return ;
}
}





我正在验证未在datagridview中检查过的复选框值不能保存在数据库中。



第1天2

1 checkboxunchecked checkboxunchecked

2 checkboxchecked checkboxchecked



将上述记录输入数据库并单击保存按钮。



在数据库记录中显示如下;



第1天2

1 checkboxunchecked checkboxunchecked

2 checkboxchecked checkboxchecked



但是我想在datagridview中未选中哪个复选框不会保存在数据库中。



我想要输入后的最终输出如下,单击数据库中的保存按钮记录,如下所示;





最终输出如下;

第1天2

2复选框已选中复选框

解决方案

如果复选框已选中,则只应调用插入,即为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 function

private 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]))


这篇关于在datagridview复选框值null不保存在数据库中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆