在Gridview上编辑和删除

时间:2018-04-25 03:46:48

标签: c# asp.net

如何在Edit上显示DropDown并根据Id列值(在GridView中隐藏)绑定DropDown数据单击Gridview使用OnRowEditing和RowDataBound事件。并且不应在GridView Load上显示此DropDown。因为我想选择colomn1的值并将其传递给数据库以获取DropDown数据,我在Edit CLick上使用下面的代码。

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>

0 个答案:

没有答案