将datagridivew复选框记录保存到数据库中 [英] saving the datagridivew check box record into the database

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

问题描述

设计如下;



课程代码组合框



Datagridview



在Datagridview中如下;

天1 2 3 4 

1复选框复选框复选框复选框
2复选框复选框复选框复选框
3复选框复选框复选框复选框
4复选框复选框复选框复选框复选框





在朗姆酒模式下,我选择课程,在datagridiview中选择复选框并保存。

然后我再选择另一个当然,在datagridveiw中选中复选框并保存。



我的保存代码如下;



< pre lang =c#> for int i = 0 ; i < DGv_Session.RowCount; i ++)
{
尝试
{
if (DGv_Session.Rows [i] .Cells [ 1 ]。值!= null && DGv_Session.Rows [i] .Cells [ 2 ]。值!= null && DGv_Session.Rows [i] .Cells [ 3 ]。值!= null && DGv_Session.Rows [i] .Cells [ 4 ]。值!= null
{
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 ]。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.To布尔值(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 (例外情况)
{
MessageBox.Show(ex.ToString(), 错误);
return ;
}
}
MessageBox.Show( 记录已成功插入 记录已插入,MessageBoxButtons.OK,MessageBoxIcon.Information);



当我保存在数据库中时,假设我将两门课程保存在数据库中。

只有一个课程保存在数据库中。

第二道菜没有保存在数据库中。



我上面的代码有什么问题?



请帮帮我。



问候,

Narasiman P.



注意它是windows applciation。

解决方案

  for  int  i =  0 ; i <  DGv_Session.RowCount; i ++)
{
try
{
if (DGv_Session.Rows [i] .Cells [ 1 ]。值!= null && DGv_Session.Rows [i] .Cells [ 2 ]。值!= null && DGv_Session.Rows [i] .Cells [ 3 ]。值!= null && DGv_Session.Rows [i] .Cells [ 4 ]。值!= null
{
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 ]。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.ToB oolean(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 (例外情况)
{
MessageBox.Show (ex.ToString(), 错误);
return ;
}
最后 {GFun.OleDbCon.Close();}
}
MessageBox.Show( 记录成功插入 记录已插入,MessageBoxButtons.OK,MessageBoxIcon.Information);


Design as follows;

Course Code Combobox

Datagridview

In Datagridview as follows;

Days   1          2           3        4

1     check box check box check box check box
2     check box check box check box check box
3     check box check box check box check box
4     check box check box check box check box



In the rum mode, I select the course and in the datagridiview select the checkbox and save.
Then I again choose another course and in the datagridveiw select the checkbox and save.

My save code as follows;

for (int i = 0; i < DGv_Session.RowCount; i++)
            {
                try
                {
                    if (DGv_Session.Rows[i].Cells[1].Value != null && DGv_Session.Rows[i].Cells[2].Value != null && DGv_Session.Rows[i].Cells[3].Value != null && DGv_Session.Rows[i].Cells[4].Value != null)
                    {
                   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;
                }
            }
                     MessageBox.Show("Record Inserted Successfully","Record Inserted",MessageBoxButtons.OK,MessageBoxIcon.Information);


When I save in the database, suppose I save the two course in the database.
Only one course save in the database.
The second course not saved in the database.

What is the problem in my above code?

Please help me.

Regards,
Narasiman P.

Note it is windows applciation.

解决方案

for (int i = 0; i < DGv_Session.RowCount; i++)
            {
                try
                {
                    if (DGv_Session.Rows[i].Cells[1].Value != null && DGv_Session.Rows[i].Cells[2].Value != null && DGv_Session.Rows[i].Cells[3].Value != null && DGv_Session.Rows[i].Cells[4].Value != null)
                    {
                   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;
                }
                finally{GFun.OleDbCon.Close();}
            }
                     MessageBox.Show("Record Inserted Successfully","Record Inserted",MessageBoxButtons.OK,MessageBoxIcon.Information);


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

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