删除gridview中的行

时间:2014-01-15 07:35:40

标签: c# asp.net gridview

我目前正在尝试删除存储在内存中的网格视图中的记录

有点背景,网格当前根据Asp.net fileUpload控件上的选定文件填充。 因此,当用户从文件上载中选择文件并单击添加文件时,文件将添加到网格中。文件存储在物理位置或数据库中。它目前存储在内存中。

    <asp:FileUpload ID="file_upload" runat="server" AllowMultiple="true" maxLength="10" accept="text/plain" Enabled="false"/>


    <asp:GridView ID="GridFiles" runat="server" AutoGenerateDeleteButton="True" Height="122px" ShowFooter="True" Style="left: 173px; 
                    top: 84px" Width="532px" BackColor="White" BorderColor="#336666" BorderStyle="Double" BorderWidth="3px" CellPadding="4" OnRowDeleting="GridFiles_RowDeleting">
                    <AlternatingRowStyle BorderColor="#FFC0C0" />
                    <FooterStyle BackColor="White" ForeColor="#333333" />
                    <RowStyle BackColor="White" BorderStyle="Dotted" ForeColor="#333333" />
                    <SelectedRowStyle BackColor="#339966" Font-Bold="True" ForeColor="White" />
                    <PagerStyle BackColor="#336666" ForeColor="White" HorizontalAlign="Center" />
                    <HeaderStyle BackColor="#336666" Font-Bold="True" ForeColor="White" />
                    <emptydatatemplate>
                           <center> No Files have been selected for upload.</center> 
                    </emptydatatemplate>
            </asp:GridView>

 <asp:Button ID="btnAdd" runat="server" Text="Add Files" onclick="btnAdd_Click" CssClass="submit"/>


.aspx.cs

 protected void btnAdd_Click(object sender, EventArgs e)
        {

            DataTable dt;
            DataRow dr = null;
            FileInfo fileObj = new FileInfo(file_upload.PostedFile.FileName);
            //string FileName = file_upload.PostedFile.FileName;
            HttpFileCollection uploadedFiles = Request.Files;
            for (int i = 0; i < uploadedFiles.Count; i++)
            {
                HttpPostedFile userPostedFile = uploadedFiles[i];
                string FileName = userPostedFile.FileName;
                try
                {

                    if (GridFiles.Rows.Count > 0)
                    {
                        int count = GridFiles.Rows.Count;
                        dt = new DataTable();
                        DataColumn dcFileName = new DataColumn("File Name", typeof(string));
                        dt.Columns.Add(dcFileName);

                        for (int j = 0; j < count; j++)
                        {
                            if (GridFiles.Rows[j].Cells[1].Text == FileName)
                            {
                                lblMessage.Text = "File already in the list";
                                break;
                            }
                        }
                        for (int k = 0; k < count; k++)
                        {
                            dr = dt.NewRow();
                            dr["File Name"] = GridFiles.Rows[k].Cells[1].Text;
                            dt.Rows.Add(dr);
                        }

                        dr = dt.NewRow();
                        dr["File Name"] = FileName;
                        dt.Rows.Add(dr);
                        GridFiles.DataSource = dt;
                        GridFiles.DataBind();
                    }
                    else
                    {
                        dt = new DataTable();

                        DataColumn dcFileName = new DataColumn("File Name", typeof(string));
                        dt.Columns.Add(dcFileName);
                        dr = dt.NewRow();
                        dr["File Name"] = FileName;
                        dt.Rows.Add(dr);
                        GridFiles.DataSource = dt;
                        GridFiles.DataBind();
                    }

                }



                catch (Exception ex)
                {
                    lblMessage.Text = "Error: <br>" + ex.Message;
                }

  protected void GridFiles_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {


            //TODO delete 

        }

1 个答案:

答案 0 :(得分:0)

您需要将DataKeyNames值添加到gridview和数据表中包含您的信息的相应列,并使用一些ID填充它以标识每一行。然后在每行放置一个删除按钮的click事件中尝试此代码

GridViewRow gvRow = (GridViewRow)((Button)sender).Parent.Parent;
int ID = Convert.ToInt32(grdView.DataKeys[gvRow.RowIndex]["ID"]);

DataTable grdContent = (DataTable)ViewState["grdContent"];

foreach (DataRow dr in grdContent.Rows)
{
    if (dr["ID"].ToString() == ID.ToString())
    {
        grdContent.Rows.Remove(dr);
        grdContent.AcceptChanges();
        break;
    }
}

grdView.DataSource = grdContent;
grdView.DataBind();