Click here to Skip to main content
15,903,856 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
I have dynamic gridview I need Edit,delete option for gridview as like static gridview. I worked but I am not getting the OP. Any one help me please.Its look like odd to post entire code but i have no choice
C#
public void BuildBranches()
{
    DataTable dtBranches = new DataTable();
    divBranches.Controls.Clear();
    objsupplyPL.username = Session["username"].ToString();
    dtBranches = objsupplyBAL.GetHOEnteredBranches(objsupplyPL);
    if (dtBranches.Rows.Count > 0)
    {
        for (int i = 0; i < dtBranches.Rows.Count; i++)
        {

            DataTable dtIndentDetails = new DataTable();
            string branches=dtBranches.Rows[i]["branch"].ToString();
            objsupplyPL.branch = branches;
            dtIndentDetails = objsupplyBAL.GetStock(objsupplyPL);

            Table tbldynamic = new Table();
            TableRow tr = new TableRow();
            TableCell tc = new TableCell();


            GridView gv = new GridView();
            gv.ID = "gv" + dtBranches.Rows[i]["branch"].ToString();
            gv.Width = 500;

            BoundField bfsno = new BoundField();
            bfsno.HeaderText = "S.No";
            bfsno.DataField = "sno";
            gv.Columns.Add(bfsno);

            BoundField bfpurchaseid = new BoundField();
            bfpurchaseid.HeaderText = "Productid";
            bfpurchaseid.DataField = "productid";
            gv.Columns.Add(bfpurchaseid);

            BoundField bfProductId = new BoundField();
            bfProductId.HeaderText = "Product Name";
            bfProductId.DataField = "productname";
            gv.Columns.Add(bfProductId);


            BoundField bfIssue = new BoundField();
            bfIssue.HeaderText = "Issue";
            gv.Columns.Add(bfIssue);

            BoundField bfgrno = new BoundField();
            bfgrno.HeaderText = "Transfer";
            gv.Columns.Add(bfgrno);


            gv.DataKeyNames = new string[]
            {
                "keys","branch"
            };


            gv.RowDataBound += new GridViewRowEventHandler(gv_RowDataBound);
            gv.RowCommand += new GridViewCommandEventHandler(gv_RowCommand);

            gv.AutoGenerateColumns = false;
            gv.EmptyDataText = "No Records Found";
            gv.DataSource = dtIndentDetails;
            gv.DataBind();
            divBranches.Controls.Add(tbldynamic);
            divBranches.Controls.Add(gv);

            LiteralControl @break1 = default(LiteralControl);
            @break1 = new LiteralControl("<br />");
            divBranches.Controls.Add(@break1);

        }


        }
    }

C#
protected void gv_RowDataBound(object sender, GridViewRowEventArgs e)
  {
      if (e.Row.RowType == DataControlRowType.DataRow)
      {
          TemplateField tfLink = new TemplateField();
          Button lnk = new Button();
          lnk.Text = "Issue";
          lnk.CommandName = "issue";


          lnk.Click += new EventHandler(MPIssue_Click);
          e.Row.Cells[4].Controls.Add(lnk);

          TemplateField tfTransfer = new TemplateField();
          Button btnTransfer = new Button();
          btnTransfer.Text = "Transfer";
          btnTransfer.CommandName = "transfer";

          btnTransfer.Click += new EventHandler(MPTransfer_Click);
          e.Row.Cells[5].Controls.Add(btnTransfer);
      }
  }

C#
protected void gv_RowCommand(object sender, GridViewCommandEventArgs e)
  {
       if (e.CommandName.ToUpper() == "ISSUE")
      {

          GridViewRow row = (GridViewRow)((Button)e.CommandSource).NamingContainer;

          lbl_Prod.Text = row.Cells[1].Text.ToString();
          lbl_Quant.Text = row.Cells[3].Text.ToString();

          lbl_Fprod.Text = row.Cells[1].Text.ToString();
          lbl_Fsquant.Text = row.Cells[3].Text.ToString();

          Button Btn = (Button)e.CommandSource;
          GridViewRow myRow = (GridViewRow)Btn.Parent.Parent;
          GridView myGrid = (GridView)sender;

          hd_sno1.Value = myGrid.DataKeys[myRow.RowIndex].Values[0].ToString(); //getting data
          hd_branch.Value = myGrid.DataKeys[myRow.RowIndex].Values[1].ToString();//getting
      }
  }


Up to here Its working fine
Posted
Updated 20-Aug-13 18:34pm
v3
Comments
CodeBlack 21-Aug-13 0:34am    
what is not working ? what error you are getting ?
Bhagavan Raju M 21-Aug-13 0:45am    
CommandField cf = new CommandField();
cf.ButtonType = ButtonType.Button;
cf.ShowCancelButton = true;
cf.ShowEditButton = true;


@CodeBlack
"The GridView 'ctl07' fired event RowEditing which wasn't handled."
CodeBlack 21-Aug-13 1:17am    
Have you added this command field in grid column ?

1 solution

Please have a look at below code. Last line is most important

C#
CommandField cf = new CommandField();
cf.ButtonType = ButtonType.Button;
cf.ShowCancelButton = true;
cf.ShowEditButton = true;
gv.Columns.Add(cf);
 
Share this answer
 
Comments
Bhagavan Raju M 21-Aug-13 1:29am    
@codeBlack i added before but no use.
gv.RowCancelingEdit += new GridViewCancelEditEventHandler(gv_RowCancelingEdit);
gv.RowEditing += new GridViewEditEventHandler(gv_RowEditing);
and i called their respective methods
CodeBlack 21-Aug-13 2:20am    
What issue you are facing now : "GridView Disappear when you click on edit button ? "

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900