如何在SQL Server中更新网格视图中选定的行 [英] How to Update the Rows Selected in the Grid View In the SQL Server

查看:55
本文介绍了如何在SQL Server中更新网格视图中选定的行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我是C#的新手,在我的项目中我有一个页面,在网格视图中显示已注册的批准系统我在网格视图中有一个复选框模板字段供选择我想用更新的以下代码尝试使用以下代码进行更新但是它只使用了选择至少一行的Else部分,即使我选择了一个复选框我的代码是,



Hi,
I am New to C#, In my Project I have a Page that displays the Registered Systems for Approval in the Grid View I have a Check box Template Field in the Grid View for Selection and Button outside the Gridview for the selected row to be Updated I had tried with the Following code for Updation but it excecutes only the Else part that "Select atleast one Row" even if I select a Check Box My Code is,

protected void Button1_Click(object sender, EventArgs e)
       {

           for (int i = 0; i < GridView1.Rows.Count; i++)
           {

               CheckBox cb = (CheckBox)GridView1.Rows[i].Cells[0].FindControl("chkSelect");

               if (cb != null && cb.Checked)
               {
                   using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["EvalCon"].ConnectionString))
                   {
                       string sqlqry = "Update Sysinfo Set Appflag='W',PApprovedate=getdate() where Mac_id=' " + GridView1.Rows[i].Cells[0].ToString()+ "'";
                       try
                       {
                           using (SqlCommand cmd = new SqlCommand(sqlqry, conn))
                           {
                               cmd.CommandType = CommandType.StoredProcedure;
                               conn.Open();
                               cmd.ExecuteNonQuery();
                               lblMessage.Visible = true;
                               lblMessage.Text = "Selected System(s) has been Approved Successfully...";
                           }
                       }
                       catch (SqlException ex)
                       {
                           if (ex.Message.Contains("SQL"))
                           {
                               lblMessage.Visible = true;
                               lblMessage.ForeColor = System.Drawing.Color.Red;
                               lblMessage.Text = "Error Occured Continue Kindly Contact Administartor!!!";
                           }
                       }
                       finally
                       {
                           conn.Close();
                           conn.Dispose();
                       }
                   }
               }
               else
               {
                   lblMessage.Visible = true;
                   lblMessage.ForeColor = System.Drawing.Color.Red;
                   lblMessage.Text = "Select Atleast One Row!!!";

               }

           }

       }





任何人都可以通过示例编码帮我解决....



Can Anybody help me out with Example Coding....

推荐答案

尝试以下代码获取复选框值



Try below code to get checkbox value

CheckBox cb = (CheckBox)GridView1.Rows[i].FindControl("chkSelect");





现在获取id的值使用 DataKeys 来自GridView。

DataKeyNames 网格的属性设置为Id的PropertyName,你可以获得它的值,如下所示





Now to get value of id use DataKeys from GridView.
Set DataKeyNames property of grid to PropertyName of Id and you can get value of it ineach row as below

GridView1.DataKeys[i].Value


这篇关于如何在SQL Server中更新网格视图中选定的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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