如何使用C#ASP.NET保存数据库中网格视图的已检查行值? [英] How to save checked row value of grid view in database using C# ASP.NET?

查看:68
本文介绍了如何使用C#ASP.NET保存数据库中网格视图的已检查行值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

protected void btnSave_Click(object sender, EventArgs e)
        {
            int id = 0;
            string strname = string.Empty;
            int Phone_Number = 0;
            string Address = string.Empty;
            for (int i = 0; i <grd_Data.Rows.Count; i++)
            {
                CheckBox chk = (CheckBox)grd_Data.Rows[i].Cells[1].FindControl("chkSelecct");
                if (chk.Checked)
                {
                  //  id = Convert.ToInt32(grd_Data.Rows[i].Cells[1].Text);
                    strname = grd_Data.Rows[i].Cells[1].Text;
                 //   Phone_Number = Convert.ToInt32(grd_Data.Rows[i].Cells[3].ToString());
                    Address = grd_Data.Rows[i].Cells[3].Text.ToString();

                    SqlConnection con = new SqlConnection(CS);
                    con.Open();
                    string insertquery="insert into tblCheck(name,phone,address) values(@name,@phone,@address)";
                    SqlCommand cmd = new SqlCommand(insertquery,con);
                    cmd.Parameters.AddWithValue("@name", strname);
                    cmd.Parameters.AddWithValue("@phone", Phone_Number);
                    cmd.Parameters.AddWithValue("@address", Address);
                    cmd.ExecuteNonQuery();
                    con.Close();
                    
                }
            }         





我的尝试:



数据正常,但值未插入数据库。



What I have tried:

Data is coming properly but value is not inserting to database.

推荐答案

for(int i = 0 ; i< datagrid1.rows.count-1; i ++)>

{

// Datagridview复选框列不为空

if (datagrid1.Rows [i] .Cells [0] .Value!= null)

{

//复选框是否已检查

bool a =(bool)datagrid1.Rows [i] .Cells [0] .Value;



//选中复选框然后执行此代码

if(a == true)

{

//你的编码在这里......

}

}

}
for(int i=0;i<datagrid1.rows.count-1;i++)>
{
//Datagridview Check box column is not empty
if(datagrid1.Rows[i].Cells[0].Value!=null)
{
//Checkbox checked or not
bool a=(bool)datagrid1.Rows[i].Cells[0].Value;

//Check box is checked then this code is execute
if(a==true)
{
// Your Coding here...
}
}
}


这篇关于如何使用C#ASP.NET保存数据库中网格视图的已检查行值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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