C# 在Gridview上编辑和删除
如何使用OnRowEditing和RowDataBound事件在GridView上单击编辑时根据Id列值(隐藏在GridView中)显示下拉列表和绑定下拉列表数据。此下拉列表不应显示在GridView加载上。由于我想选择Column1的值并将其传递给数据库以获得下拉数据,所以我在单击编辑时使用下面的代码C# 在Gridview上编辑和删除,c#,asp.net,C#,Asp.net,如何使用OnRowEditing和RowDataBound事件在GridView上单击编辑时根据Id列值(隐藏在GridView中)显示下拉列表和绑定下拉列表数据。此下拉列表不应显示在GridView加载上。由于我想选择Column1的值并将其传递给数据库以获得下拉数据,所以我在单击编辑时使用下面的代码 protected void RowDataBound (object sender, GridViewRowEventArgs e) { if (e.R
protected void RowDataBound (object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
if ((e.Row.RowState & DataControlRowState.Edit) > 0)
{
DropDownList DDL =
(DropDownList)e.Row.FindControl("DropDownList2");
int editRowIndex = e.NewEditIndex;
GridViewRow row = GridView1.Rows[editRowIndex];
string Tname = row.Cells[2].Text;// To get value at colomn 2
SqlConnection cn = new SqlConnection(cs);
cn.Open();
SqlCommand cd = new SqlCommand();
DataTable dt = cn.GetData("Select TrainingDate from TrainingDetail where TrainingName = Tname");
cd.CommandType = CommandType.StoredProcedure;
cd.Parameters.Add("@Tname", SqlDbType.NVarChar).Value = Tname;
cd.ExecuteNonQuery();
cn.Close();
}
}
protected void OnRowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.DataBind();
}
<asp:GridView ID="GridView1" runat="server" AutoGenerateDeleteButton="True" AutoGenerateEditButton="True" AutoGenerateColumns="False" OnSelectedIndexChanged="GridView1_SelectedIndexChanged" OnRowEditing="OnRowEditing" OnRowDeleting="OnRowDeleting" OnRowDataBound ="RowDataBound">
<Columns>
<asp:BoundField DataField ="Id" HeaderText ="Training Id" Visible="False" InsertVisible ="false" ReadOnly="true" />
<asp:BoundField ReadOnly="true" DataField ="TrainingName" HeaderText ="Training Name" />
<asp:TemplateField HeaderText="Training Date">
<EditItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Eval("TrainingDate", "{0:dd/MM/yyyy}") %>'></asp:Label>
</EditItemTemplate>
<ItemTemplate>
<asp:DropDownList ID="DropDownList2" runat="server" DataTextField ="TrainingDate" Visible ="false" AppendDataBoundItems="true">
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField ReadOnly="true" DataField ="TrainingType" HeaderText ="Training Type" />
<asp:BoundField ReadOnly="true" DataField ="TrainingDuration" HeaderText ="Training Duration(hours)" />
<asp:BoundField ReadOnly="true" DataField ="StartTrainingTime" HeaderText ="Start Training Time" />
<asp:BoundField ReadOnly="true" DataField ="EndTrainingTime" HeaderText ="End Training Time" />
</Columns>
</asp:GridView>
受保护的void行数据绑定(对象发送方,GridViewRowEventArgs e)
{
如果(e.Row.RowType==DataControlRowType.DataRow)
{
如果((e.Row.RowState和DataControlRowState.Edit)>0)
{
DropDownList DDL=
(DropDownList)e.Row.FindControl(“DropDownList2”);
int editRowIndex=e.NewEditIndex;
GridViewRow row=GridView1.Rows[editRowIndex];
字符串Tname=row.Cells[2].Text;//获取第2列的值
SqlConnection cn=新的SqlConnection(cs);
cn.Open();
SqlCommand cd=newsqlcommand();
DataTable dt=cn.GetData(“从TrainingDetail中选择TrainingDate,其中TrainingName=Tname”);
cd.CommandType=CommandType.storedProcess;
Add(“@Tname”,SqlDbType.NVarChar).Value=Tname;
cd.ExecuteNonQuery();
cn.Close();
}
}
受保护的void OnRowEditing(对象发送方,GridViewEditEventArgs e)
{
GridView1.DataBind();
}
更详细地介绍您想要实现的用例会更有用。还可以分享一些您已经编写的代码。看看。它涵盖了GridView编辑和更新的所有基础知识。