我试过但是查询不能使用datagridview中的复选框进入DataBase [英] i tried but the query is not working using check box in datagridview into the DataBase

查看:52
本文介绍了我试过但是查询不能使用datagridview中的复选框进入DataBase的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

注意它是windows appplication。



保存按钮代码如下;



  for  int  i =  0 ;i<dgv_session.rowcount;i++)>  
{
if (Convert.ToBoolean(DGv_Session.Rows [i] .Cells [ 1 ]。Value)== true
{
sql = 插入到Tb_Session_Structure([Cmn_Minor_code],[Days] [SESSION1],[会话2],[SESSION3],[Session4]);

sql = sql + values(' + cb_Course_Code.Text + ',
'
+ DGv_Session.Rows [i] .Cells [ 0 ]。Value.ToString()+ ',
+ Convert.ToString(DGv_Session [ 1 ,i] .Value == true)+
+ Convert.ToString(DGv_Session [ 2 ,i] .Value == true)+
+ Convert.ToString(DGv_Session [ 3 ,i] .Value == true)+
+ Convert.ToString(DGv_Session [ 4 ,i] .Value == true)+ ;

GFun.Error = ;
GFun.InsertAccessData(sql);
if (GFun.Error.ToString()!=
{
MessageBox.Show(GFun.Error.ToString(), 错误);
return ;
}
GFun.OleDbCon.Close();
MessageBox.Show( 记录已成功插入);
}
}







运行模式屏幕如下;



课程代码(组合框)



天sess1 sess2 sess3 sess4

1已选中已选中未经检查未经检查



以上课程第1天sess1和sess2已经过检查并保存在数据库中。





但是在数据库记录中如下;



天sess1 sess2 sess3 sess4

1未经检查未选中unchecked unchecked



为什么sess1和sess2选中复选框未在数据库中检查。



什么是我上面的复选框中的问题检查了真的代码?



请帮帮我。



问候,

Narasiman P.





请注意它是windows appplication。

解决方案
您好,


可能与true相比较的值总是假的。所以,试试如下。

 sql = sql +   values(' + cb_Course_Code.Text +  ',
'
+ DGv_Session.Rows [i] .Cells [ 0 ]。Value.ToString()+ < span class =code-string>',
+(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()+ ;





希望它有效。




DataGridView值属性将返回包含的对象,因此如果已向单元格指定了布尔类型对象,则在询问时将返回该布尔对象,您不需要再将其转换回布尔值。



我假设您没有使用正确的类型对象进行数据绑定,即True / False属性键入为bool而不是字符串。但是,如果您使用了正确的类型,那就很好了。如果没有,在你的情况下,你更有可能将所有东西都用作字符串;那么你还必须检查你正在解析的字符串对象是否为空。



因此,在这种情况下,如果值不为null,则解析该对象的字符串,如果为null则设置为空:

   int  i =  0 ; i< dgv_session.rowcount; i ++)>  
{
if (DGv_Session.Rows [i] .Cells [ 1 ]。值!= null
{
if (Convert.ToBoolean(DGv_Session。行[i]。细胞[ 1 ]。值))
{
sql = 插入Tb_Session_Structure([Cmn_Minor_code],[Days],[Session1],[Session2],[Session3],[Session4]);

sql = sql + values(' + cb_Course_Code.Text + ','
+(DGv_Session.Rows [i] .Cells [ 0 ]。值!= null )?DGv_Session.Rows [i] .Cells [ 0 ]。Value.ToString(): + ',
+(DGv_Session [ 1 ,i] .Value!= null )?DGv_Session [ 1 ,i] .Value。 ToString(): +
+(DGv_Session [ 2 ,i] .Value!= null )? DGv_Session [ 2 ,i] .Value.ToString(): +
+(DGv_Session [ 3 ,i] .Value!= null )? DGv_Session [ 3 ,i] .Value.ToString(): +
+(DGv_Session [ 4 ,i] .Value!= null )? DGv_Session [ 4 ,i] .Value.ToString(): + ;

GFun.Error = ;
GFun.InsertAccessData(sql);
if (GFun.Error.ToString()!=
{
MessageBox.Show(GFun.Error.ToString(), 错误);
return ;
}
GFun.OleDbCon.Close();
MessageBox.Show( 记录已成功插入);
}
}
}





我希望这会有所帮助。



问候

Jegan


note it is windows appplication.

Save Button Code as follows;

for(int i =0;i<dgv_session.rowcount;i++)>
{
    if (Convert.ToBoolean(DGv_Session.Rows[i].Cells[1].Value) == true)
    {
        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.ToString(DGv_Session[1, i].Value == "true") + ",
" + Convert.ToString(DGv_Session[2, i].Value == "true") + ",
" + Convert.ToString(DGv_Session[3, i].Value == "true") + ",
" + Convert.ToString(DGv_Session[4, i].Value == "true") + ")";
        
        GFun.Error = "";
        GFun.InsertAccessData(sql);
        if (GFun.Error.ToString() != "")
        {
            MessageBox.Show(GFun.Error.ToString(), "Error");
            return;
        }
        GFun.OleDbCon.Close();
        MessageBox.Show("Record Inserted Successfully");
    }
}




Run Mode Screen As follows;

Course Code (combobox)

Days sess1 sess2 sess3 sess4
1 checked checked unchecked unchecked

in the above for the course day 1 sess1 and sess2 is checked and save in the database.


But In database records as follows;

Days sess1 sess2 sess3 sess4
1 unchecked unchecked unchecked unchecked

why the sess1 and sess2 checked checkbox are not checked in the database.

what is the problem in my above checkbox checked true code?

Please help me.

regards,
Narasiman P.


note it is windows appplication.

解决方案

Hi,

may the value you are comparing with "true" is always giving false. so, try like below.

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() + ")";



hope it works.


Hi,
The DataGridView Value property will return the object that is contains so if have assigned a Boolean type object to a cell, it will return that Boolean object when asked, you wouldn''t need to convert it back to Boolean again.

I assume that you not used proper typed objects for the data binding i.e. where a "True/False" property typed as bool not string. However, if you have used correct types it is good. If not, it is more likely in your case, you would be using everything as string; then you must also check that the string object you are parsing around is not null.

So, in this case if the value is not null parse the string of that object, if it is null then set as empty:

for(int i =0;i<dgv_session.rowcount;i++)>
{
    if (DGv_Session.Rows[i].Cells[1].Value != null)
    {
        if (Convert.ToBoolean(DGv_Session.Rows[i].Cells[1].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 != null) ? DGv_Session.Rows[i].Cells[0].Value.ToString() : "" + "',"
                + (DGv_Session[1, i].Value != null) ? DGv_Session[1, i].Value.ToString() : "" + ","
                + (DGv_Session[2, i].Value != null) ? DGv_Session[2, i].Value.ToString() : ""  + ","
                + (DGv_Session[3, i].Value != null) ? DGv_Session[3, i].Value.ToString() : ""  + ","
                + (DGv_Session[4, i].Value != null) ? DGv_Session[4, i].Value.ToString() : "" + ")";

            GFun.Error = "";
            GFun.InsertAccessData(sql);
            if (GFun.Error.ToString() != "")
            {
                MessageBox.Show(GFun.Error.ToString(), "Error");
                return;
            }
            GFun.OleDbCon.Close();
            MessageBox.Show("Record Inserted Successfully");
        }
    }
}



I hope this helps.

Regards
Jegan


这篇关于我试过但是查询不能使用datagridview中的复选框进入DataBase的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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