-3

I want to delete a row using OnRowDeleting on GridView.. I have tried Different codes from internet but nothing works for me.. Help me Please!! This is my code: ASPX file::

<script type="text/c#" runat="server">
protected void BtnUpload_Click(object sender, EventArgs e)
{
    if (Request.Files != null)
    {
        foreach (string file in Request.Files)
        {
            var uploadedFile = Request.Files[file];
            if (uploadedFile.ContentLength > 0)
            {
                var appData = Server.MapPath("~/");
                var fileName = Path.GetFileName(uploadedFile.FileName);
                uploadedFile.SaveAs(Path.Combine(appData, fileName));
            }
        }
    }
} //Some code here

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"  OnRowCommand="GridView1_RowCommand" OnRowDeleting="GridView1_RowDeleting"   CellPadding="4" ForeColor="#333333" GridLines="None" Width="155px">



        <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
        <Columns>

            <asp:TemplateField >
                <ItemTemplate>

     <asp:FileUpload ID="FileUpload1" runat="server" /> 
     </ItemTemplate>
                </asp:TemplateField>
            <asp:ButtonField CommandName="Uploader" ButtonType="Button" Text="Upload" HeaderText="Upload" />
            <asp:CommandField ShowDeleteButton="True" ButtonType="Button" HeaderText="Delete"  />
        </Columns>
        <EditRowStyle BackColor="#999999" />
        <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
        <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
        <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
        <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
        <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
        <SortedAscendingCellStyle BackColor="#E9E7E2" />
        <SortedAscendingHeaderStyle BackColor="#506C8C" />
        <SortedDescendingCellStyle BackColor="#FFFDF8" />
        <SortedDescendingHeaderStyle BackColor="#6F8DAE" />


       </asp:GridView>
      <asp:Button ID="Button1" runat="server" Text="Add New Row" OnClick="Button1_Click" />
     <asp:Panel ID="pnlInfo" runat="server">
     </asp:Panel>


       <asp:LinkButton ID="BtnUpload" runat="server"  Text="Upload" OnClick="BtnUpload_Click" />
        //some code here

and this is ASPX.CS

public partial class WebForm4 : System.Web.UI.Page
{
   DataTable dt;
  protected  void Page_Load(object sender, EventArgs e)
    {



    }

  protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs ea)
  {

      if(ea.CommandName=="Uploader")
      {
          //GridViewRow grv = (GridViewRow);
          int i = Convert.ToInt32(ea.CommandArgument);

          FileUpload fileUpload = GridView1.Rows[i].FindControl("FileUpload1") as FileUpload; 

          if (fileUpload.HasFile)
          {
              try
              {
                  string filename = Path.GetFileName(fileUpload.FileName);
                  fileUpload.SaveAs(Server.MapPath("~/") + filename);

              }
              catch (Exception ex)
              {
                  String se = ex.Message;
              }
          }
      }

  }






     protected virtual void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs ea)
  {
       //Suggest Code Here..

  }

     private void AddNewRow()
     {
          dt = new DataTable();

         dt.Columns.Add("sno");
         dt.Columns.Add("name");
         foreach (GridViewRow gvRow in GridView1.Rows)
         {
             DataRow dr = dt.NewRow();
             //dr["sno"] = ((Label)gvRow.FindControl("lblSno")).Text;
             //dr["name"] = ((Label)gvRow.FindControl("txtName")).Text;
             dt.Rows.Add(dr);
         }

         DataRow dr1 = dt.NewRow();
         dr1["sno"] = "";
         dr1["name"] = "";
         dt.Rows.Add(dr1);

         GridView1.DataSource = dt;
         GridView1.DataBind();

     }

     protected void Button1_Click(object sender, EventArgs e)
     {
         AddNewRow(); 
     }



     }
    }

Please Help...

Himanshu Bisht
  • 37
  • 1
  • 2
  • 7
  • 2
    Please describe what problem you are having. Are you getting some error for example or your line of code is not executed? Also look at the site here quite a lot of similar questions. For example: http://stackoverflow.com/questions/7226548/how-to-delete-row-in-gridview-using-rowdeleting-event – TarasB Oct 21 '14 at 20:30
  • Thanx for reply @TarasB I want to delete the row of gridview with this handler but it is not working.. I used the code:: protected virtual void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs ea) { int i = Convert.ToInt32(ea.RowIndex); GridView1.DeleteRow(i); } It gives StackOverFlowException was unhandled.. and thanx for suggestion i tried that before it doesnot work for me. – Himanshu Bisht Oct 21 '14 at 20:58
  • since you are getting StackOverflowException you are likely to be at the right forum :) – TarasB Oct 21 '14 at 21:32

1 Answers1

1

I'd recommend you to work with the bound data while adding/deleting rows, not with the rows directly since you've chosen grid view. Code is much simpler then. Below is an example - note that I initialize there DataTable in a static scope (which is not a perfect place) - store it where you need - in memory/session, or in view state - depends on your task there.

public partial class WebForm4 : System.Web.UI.Page
{

    private static DataTable MyDataTable = new DataTable();

    static WebForm4()
    {
        MyDataTable.Columns.Add("sno");
        MyDataTable.Columns.Add("name");
    }
    private void AddNewRow()
    {
        MyDataTable.Rows.Add(MyDataTable.NewRow());
        GridView1.DataSource = MyDataTable;
        GridView1.DataBind();
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        AddNewRow();
    }

    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        MyDataTable.Rows.RemoveAt(e.RowIndex);
        GridView1.DataSource = MyDataTable;
        GridView1.DataBind();
    }
}

Hope that helped.

TarasB
  • 2,407
  • 1
  • 24
  • 32
  • Thanx for the support @TarasB .. It works for me.. Problem was the DataTable Declaration and yes StackOverflowException is removed at StackOverflow :) :).. All thanx to you.. – Himanshu Bisht Oct 22 '14 at 04:38