在Gridview中删除一行

时间:2014-10-21 20:10:59

标签: c# asp.net

我想在GridView上使用OnRowDeleting删除一行.. 我尝试过来自互联网的不同代码,但对我来说没什么用。 请帮帮我!! 这是我的代码: ASPX文件::

<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

这是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(); 
     }



     }
    }

请帮助......

1 个答案:

答案 0 :(得分:0)

我建议您在添加/删除行时使用绑定数据,而不是直接使用行,因为您选择了网格视图。代码要简单得多。 下面是一个例子 - 请注意我在静态范围内初始化DataTable(这不是一个完美的地方) - 将它存储在你需要的地方 - 在内存/会话中,或在视图状态 - 取决于你的任务。

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();
    }
}

希望有所帮助。