Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
514 views
in Technique[技术] by (71.8m points)

c# - How to delete row in gridview using rowdeleting event?

This is my .cs code :

protected void Gridview1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
 Gridview1.DeleteRow(e.RowIndex);
 Gridview1.DataBind();
}

and this is markup,

<asp:gridview ID="Gridview1" runat="server" ShowFooter="true" 
                                                AutoGenerateColumns="false" OnRowDeleting="Gridview1_RowDeleting">
                    <Columns>
                    <asp:BoundField DataField="RowNumber" HeaderText="Row Number" />
                    <asp:TemplateField HeaderText="Column Name">
                        <ItemTemplate>
                            <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
                        </ItemTemplate>
                    </asp:TemplateField>

                 <%-- <asp:TemplateField HeaderText="Header 2">
                        <ItemTemplate>
                            <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
                        </ItemTemplate>
                    </asp:TemplateField>--%>
                    <asp:TemplateField HeaderText="Data Type">
                        <ItemTemplate>
                              <asp:DropDownList ID="ddldatatype" runat="server">
                              <asp:ListItem>varchar</asp:ListItem>
                              <asp:ListItem>int</asp:ListItem>
                              <asp:ListItem>numeric</asp:ListItem>
                              <asp:ListItem>uniqueidentifier</asp:ListItem>
                              <asp:ListItem>char</asp:ListItem>
                              </asp:DropDownList>
                        </ItemTemplate>
                        <FooterStyle HorizontalAlign="Right" />
                        <FooterTemplate>
                        <asp:Button ID="ButtonAdd" runat="server" Text="Add New Row" OnClick="ButtonAdd_Click"/>
                         <asp:Button ID="ButtonDel" runat="server" Text="Delete Row" OnClick="ButtonDel_Click" />
                         <input type="hidden" runat="server" value="0" id="hiddencount" />
                        </FooterTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField>
                        <ItemTemplate>
                            <asp:LinkButton ID="lnkdelete" runat="server" CommandName="Delete" >Delete</asp:LinkButton>
                        </ItemTemplate>
                    </asp:TemplateField>
                    </Columns>
            </asp:gridview>

Please sugegest me. I have done this much.. but still not deleting row...

 protected void Gridview1_RowDeleting(object sender, GridViewDeleteEventArgs e)
         {


             //Gridview1.DeleteRow((int)Gridview1.DataKeys[e.RowIndex].Value);
             //Gridview1.DeleteRow(e.RowIndex);
             //Gridview1.DataBind();
             foreach(DataRow dr in dt.Rows)
             {
                 dt.Rows.Remove(dr);
                 dt.Rows[e.RowIndex].Delete();

             }
             Gridview1.DeleteRow(e.RowIndex);
            // dt = (DataTable)Gridview1.DataSource;
             Gridview1.DataSource = dt;
             Gridview1.DataBind();
       }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
    GridViewRow row = (GridViewRow)GridView1.Rows[e.RowIndex];
    SqlCommand cmd = new SqlCommand("Delete From userTable (userName,age,birthPLace)");
    GridView1.DataBind();
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...