C# GridView通过代码隐藏更新存储过程
有人能帮我吗。我已经设法使行编辑的功能发挥作用,现在正试图让行更新完成。我的gridview设置如下所示C# GridView通过代码隐藏更新存储过程,c#,asp.net,sql-server,visual-studio,C#,Asp.net,Sql Server,Visual Studio,有人能帮我吗。我已经设法使行编辑的功能发挥作用,现在正试图让行更新完成。我的gridview设置如下所示 <asp:GridView ID="CountryGridView" runat="server" AutoGenerateColumns="false" CssClass="mGrid" OnRowEditing="CountryGridView_RowEditing" OnRowCancelingEdit="CountryGridView_RowCancelingEdit" >
<asp:GridView ID="CountryGridView" runat="server" AutoGenerateColumns="false" CssClass="mGrid" OnRowEditing="CountryGridView_RowEditing" OnRowCancelingEdit="CountryGridView_RowCancelingEdit" >
<Columns>
<asp:BoundField ReadOnly="true" DataField="i_SK_Accom" HeaderText="i_SK_Accom" Visible="false" />
<asp:BoundField ReadOnly="true" DataField="Accom_Code" HeaderText="Accom Code" />
<asp:BoundField ReadOnly="true" DataField="Accom_Name" HeaderText="Accom Name" />
<asp:BoundField DataField="OP49_Required" HeaderText="OP49 Required?" />
<asp:BoundField DataField="Weekly" HeaderText="Weekly" />
<asp:BoundField DataField="Daily" HeaderText="Daily" />
<asp:CommandField ShowEditButton="true" ButtonType="Image" EditImageUrl="~/Images/Edit.gif" UpdateImageUrl="~/Images/save.png" CancelImageUrl="~/Images/cancel.png" ControlStyle-CssClass="ImageButton" />
</Columns>
</asp:GridView>
上面指定的字段与gridview所需的字段相同,但我正在努力将gridviewrRowEditing
值转换为变量/参数以传递给SQLCommand
感兴趣的身份证:
DataBinder - BindCountryGrid
DataReader - CountryGridSelectReader
最终解决了: 代码隐藏需要重新设置以下项:
protected void CountryGridView_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
Label Textbox_Accom = (Label)gv_CountryGridView.Rows[e.RowIndex].FindControl("lbl_Accom_Code");
Label Textbox_Accom_Name = (Label)gv_CountryGridView.Rows[e.RowIndex].FindControl("lbl_Accom_Name");
DropDownList Textbox_OP49 = (DropDownList)gv_CountryGridView.Rows[e.RowIndex].FindControl("ddl_OP49");
DropDownList Textbox_Weekly = (DropDownList)gv_CountryGridView.Rows[e.RowIndex].FindControl("ddl_Weekly");
DropDownList Textbox_Daily = (DropDownList)gv_CountryGridView.Rows[e.RowIndex].FindControl("ddl_Daily");
TextBox Textbox_FRRStartDate = (TextBox)gv_CountryGridView.Rows[e.RowIndex].FindControl("txt_FRRStartDate");
SqlCommand commEditConsultant = new SqlCommand("IFACE_JFA_ACCOM", ConnJFA);
commEditConsultant.CommandType = CommandType.StoredProcedure;
commEditConsultant.Parameters.Add("@Statement", SqlDbType.VarChar).Value = "AccomGridUpdate";
commEditConsultant.Parameters.Add("@Page", SqlDbType.VarChar).Value = "OP49";
commEditConsultant.Parameters.Add("@PC_Username", SqlDbType.VarChar).Value = HttpContext.Current.User.Identity.Name.ToUpper().ToString();
commEditConsultant.Parameters.Add("@Season_Name", SqlDbType.VarChar).Value = txt_Season.Text;
commEditConsultant.Parameters.Add("@Accom_Code", SqlDbType.VarChar).Value = Textbox_Accom.Text;
commEditConsultant.Parameters.Add("@i_FK_SeasonID", SqlDbType.VarChar).Value = Request["i_FK_SeasonID"].Trim().ToString();
commEditConsultant.Parameters.Add("@OP49_Required", SqlDbType.VarChar).Value = Textbox_OP49.Text;
commEditConsultant.Parameters.Add("@Weekly", SqlDbType.VarChar).Value = Textbox_Weekly.Text;
commEditConsultant.Parameters.Add("@Daily", SqlDbType.VarChar).Value = Textbox_Daily.Text;
commEditConsultant.Parameters.Add("@FRR_StartDate", SqlDbType.VarChar).Value = Textbox_FRRStartDate.Text;
ConnJFA.Open();
commEditConsultant.ExecuteNonQuery();
gv_CountryGridView.EditIndex = -1;
Error_Dashboard.Text = Textbox_Accom.Text + " - " + Textbox_Accom_Name.Text + " Updated Successfully!";
ConnJFA.Close();
BindCountryGrid();
}
protected void CountryGridView_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
Label Textbox_Accom = (Label)gv_CountryGridView.Rows[e.RowIndex].FindControl("lbl_Accom_Code");
Label Textbox_Accom_Name = (Label)gv_CountryGridView.Rows[e.RowIndex].FindControl("lbl_Accom_Name");
DropDownList Textbox_OP49 = (DropDownList)gv_CountryGridView.Rows[e.RowIndex].FindControl("ddl_OP49");
DropDownList Textbox_Weekly = (DropDownList)gv_CountryGridView.Rows[e.RowIndex].FindControl("ddl_Weekly");
DropDownList Textbox_Daily = (DropDownList)gv_CountryGridView.Rows[e.RowIndex].FindControl("ddl_Daily");
TextBox Textbox_FRRStartDate = (TextBox)gv_CountryGridView.Rows[e.RowIndex].FindControl("txt_FRRStartDate");
SqlCommand commEditConsultant = new SqlCommand("IFACE_JFA_ACCOM", ConnJFA);
commEditConsultant.CommandType = CommandType.StoredProcedure;
commEditConsultant.Parameters.Add("@Statement", SqlDbType.VarChar).Value = "AccomGridUpdate";
commEditConsultant.Parameters.Add("@Page", SqlDbType.VarChar).Value = "OP49";
commEditConsultant.Parameters.Add("@PC_Username", SqlDbType.VarChar).Value = HttpContext.Current.User.Identity.Name.ToUpper().ToString();
commEditConsultant.Parameters.Add("@Season_Name", SqlDbType.VarChar).Value = txt_Season.Text;
commEditConsultant.Parameters.Add("@Accom_Code", SqlDbType.VarChar).Value = Textbox_Accom.Text;
commEditConsultant.Parameters.Add("@i_FK_SeasonID", SqlDbType.VarChar).Value = Request["i_FK_SeasonID"].Trim().ToString();
commEditConsultant.Parameters.Add("@OP49_Required", SqlDbType.VarChar).Value = Textbox_OP49.Text;
commEditConsultant.Parameters.Add("@Weekly", SqlDbType.VarChar).Value = Textbox_Weekly.Text;
commEditConsultant.Parameters.Add("@Daily", SqlDbType.VarChar).Value = Textbox_Daily.Text;
commEditConsultant.Parameters.Add("@FRR_StartDate", SqlDbType.VarChar).Value = Textbox_FRRStartDate.Text;
ConnJFA.Open();
commEditConsultant.ExecuteNonQuery();
gv_CountryGridView.EditIndex = -1;
Error_Dashboard.Text = Textbox_Accom.Text + " - " + Textbox_Accom_Name.Text + " Updated Successfully!";
ConnJFA.Close();
BindCountryGrid();
}