I have to create grid programically. For this purpose I have used below lines of code.

[Serializable]
public class feilds
{
    public string DocumentId { get; set; }
    public string Document { get; set; }
    public string FileName { get; set; }
    public byte[] FileContent { get; set; }
    public string URL { get; set; }
    public bool IsActive { get; set; }
}   

protected override void Page_Load(object sender, EventArgs e) { BindGrid(); }

protected void grdDummy_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "Delete")
        {
            string docid = e.CommandArgument.ToString();
            //find field by documentId and remove it from list
            data.Remove(data.Find(o => o.DocumentId == docid));

            ViewState["_data"] = data;

            //rebind grid
            grdDummy.DataSource = data;
            grdDummy.DataBind();

        }
    }

  protected void btnUpload_Click(object sender, EventArgs e)
    {
      feilds s = new feilds();
      string guid = Guid.NewGuid().ToString();
      s.DocumentId = guid;
      if (fiFile.PostedFile != null)
      {
          string FileName = fiFile.PostedFile.FileName;
          if (FileName != "")
          {
              FileName = FileName.Replace("\\", "/");
              FileName = FileName.Substring(FileName.LastIndexOf("/") + 1);
              s.FileName= FileName;
              s.FileContent = WebUtility.ReadFileContents(fiFile);
              s.Document = FileName;
              s.URL = DBNull.Value.ToString();
          }
      }
      else
      {
          s.FileName = DBNull.Value.ToString();
          s.FileContent = StringToByteArray(DBNull.Value.ToString());
          if (!txtUrl.Text.Contains("http://"))
          {
              s.URL = "http://" + txtUrl.Text;
              s.Document = "http://" + txtUrl.Text;
          }
          else
          {
          s.URL= txtUrl.Text;
          s.Document = txtUrl.Text;
          }
        }
       s.IsActive = Convert.ToBoolean(rdlIsactive.SelectedItem.Value);
       data.Add(s);
       ViewState["_data"] = data;
       grdDummy.DataSource = data;
       grdDummy.DataBind();

    }
    List<feilds> data = null;
    private void BindGrid()
    {
        data = ViewState["_data"] as List<feilds>;
        if (data == null)
            data = new List<feilds>();
    }

In aspx file

           <asp:UpdatePanel ...>
           ...

            <asp:GridView ID="grdDummy" OnRowCommand=" grdDummy_RowCommand" runat="server">
                  <Columns>
                 <asp:TemplateField>
                 <ItemTemplate>
                 <asp:LinkButton ID="lnkDelete" runat="server" CssClass="colorlnkbtndelete" style="float:right"  CommandName="Delete" CommandArgument='<%# DataBinder.Eval (Container.DataItem, "DocumentId") %>'><i class="icon-trash"></i></asp:LinkButton>
              </ItemTemplate>
              </asp:TemplateField>
              </Columns>      
               </asp:GridView>
              ...
              ...
             </asp:UpdatePanel>

Now the issue is in row command event of the grid. It's not deleting the selected row. I want to delete currently selected item from the grid and also from view state and then refresh the grid. Please help!!!

Related posts

Recent Viewed