我在Gridview中只更新了两列。这是我的更新编码我在这个编码中得到错误.. [英] I Am Update Only Two Column In Gridview. This Is My Update Coding I Get Error In This Coding..

查看:83
本文介绍了我在Gridview中只更新了两列。这是我的更新编码我在这个编码中得到错误..的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

ASP设计页面:



ASP design page:

<asp:GridView ID="GridView2" runat="server" AlternatingRowStyle-BackColor="#C2D69B"

                            HeaderStyle-BackColor="green" AutoGenerateEditButton="True"

                            onrowcancelingedit="GridView2_RowCancelingEdit"

                            onrowediting="GridView2_RowEditing" onrowupdating="GridView2_RowUpdating">
                            <HeaderStyle BackColor="Green" />
                            <AlternatingRowStyle BackColor="#C2D69B" />

                        </asp:GridView>









table:







table:

SELECT TOP 1000 [BookingRefNo]
      ,[DateIn]
      ,[JobCode]
      ,[RequestedBy]
      ,[TargetDept]
      ,[DescriptionofJob]
      ,[NeedBy]
      ,[JobLocation]
      ,[ContactPerson]
      ,[ReqPriority]
      ,[AssignedPriority]
      ,[STATUS]
  FROM [FIMSONE_P2].[dbo].[TSafetyMaster]







代码页:




code page:

 protected void Page_Load(object sender, EventArgs e)
   {

      BindGridViewPageLoad();


   }

   private void BindGridViewPageLoad()
   {
       SqlConnection con1 = new SqlConnection(constr);
       con1.Open();
       SqlCommand cmd = new SqlCommand("select * from TSafetyMaster", con1);
       SqlDataAdapter da = new SqlDataAdapter(cmd);
       DataSet ds = new DataSet();
       da.Fill(ds);
       GridView2.DataSource = ds.Tables[0];
       GridView2.DataBind();


   }

protected void GridView2_RowEditing(object sender, GridViewEditEventArgs e)
   {
       GridView2.EditIndex = e.NewEditIndex;
       GridView2.DataBind();
   }

   protected void GridView2_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
   {
       GridView2.EditIndex = -1;
       GridView2.DataBind();
   }

   protected void GridView2_RowUpdating(object sender, GridViewUpdateEventArgs e)
   {


       GridViewRow row = (GridViewRow)GridView2.Rows[e.RowIndex];
       int BookingRefNo=int.Parse(GridView2.DataKeys[e.RowIndex].Value.ToString());
       //Label lblBookingRefNo = (Label)row.FindControl("BookingRefNo");
       TextBox txtAssignedPriority = (TextBox)row.FindControl("AssignedPriority");
       TextBox txtStatus = (TextBox)row.FindControl("Status");
       SqlConnection con1 = new SqlConnection(constr);
       SqlCommand cmd = new SqlCommand("Update TSafetyMaster set AssignedPriority=@AssignedPriority,Status=@Status where BookingRefNo=@BookingRefNo", con1);
       cmd.Parameters.Add("@BookingRefNo", SqlDbType.Int).Value = BookingRefNo;
       cmd.Parameters.Add("@AssignedPriority", SqlDbType.NVarChar).Value = txtAssignedPriority;
       cmd.Parameters.Add("@Status", SqlDbType.NVarChar).Value = txtStatus;
       cmd.ExecuteNonQuery();
       GridView2.EditIndex=-1;
       GridView2.DataBind();



   }

推荐答案

您需要设置数据Key Names属性为BookingRefNo。那么只有你才能获得数据密钥值

you need to set Data Key Names property as BookingRefNo. then only you can get the Data Key value
<asp:gridview id="GridView2" runat="server" datakeynames="BookingRefNo" xmlns:asp="#unknown"></asp:gridview>



现在低于行你将有BookingRefNo


now below line you will have BookingRefNo

int BookingRefNo=int.Parse(GridView2.DataKeys[e.RowIndex].Value.ToString());



当你获得价值时,尝试如下


when you get values, try like below

var DateIn =GridView2.Rows[e.RowIndex].Cells[1].Text;
var JobCode =GridView2.Rows[e.RowIndex].Cells[2].Text;
var RequestedBy=GridView2.Rows[e.RowIndex].Cells[3].Text;



PageLoad代码已更新


PageLoad code Updated

protected void Page_Load(object sender, EventArgs e)
   {
      if(!IsPostBack()) { 
          BindGridViewPageLoad();
      }
   }


这篇关于我在Gridview中只更新了两列。这是我的更新编码我在这个编码中得到错误..的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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