Forums - C# Corner

Forum guidelines
Priyanka Singh

Priyanka Singh

  • 1.3k
  • 495
  • 11.4k

Create Row in gridview.

May 6 2019 6:53 AM
I want to create a row in gridview as per specified requirement.
 
Here I use this code to create a row,but it creates in all the fields
  1. protected void gvEmployee_RowCreated(object sender, GridViewRowEventArgs e)  
  2.        {  
  3.            if (e.Row.RowType == DataControlRowType.Footer)  
  4.            {  
  5.                for (int i = 0; i < e.Row.Cells.Count; i++)  
  6.                {  
  7.                    TextBox tb = new TextBox();  
  8.                    tb.ID = string.Format("txtEntry{0}", i);  
  9.                    e.Row.Cells[i].Controls.Add(tb);  
  10.                    if (i == e.Row.Cells.Count - 1)  
  11.                    {  
  12.                        Button btn = new Button();  
  13.                        btn.ID = "btnAddNew";  
  14.                        btn.Text = "Add Text";  
  15.                        btn.Click += btnAddNew_Click;  
  16.                        e.Row.Cells[i].Controls.Add(btn);  
  17.   
  18.                    }  
  19.   
  20.                }  
  21.            }  
  22.        }  
  23.        protected void btnAddNew_Click(object sender, EventArgs e)  
  24.        {  
  25.            Button btn = (Button)sender;  
  26.            GridViewRow row = (GridViewRow)btn.NamingContainer;  
  27.            if (row != null)  
  28.            {  
  29.                if (InsertNewEmployee(row))  
  30.                    ClientScript.RegisterClientScriptBlock(typeof(Page), "ALERT""alert('New Record Added.');"true);  
  31.                else  
  32.                    ClientScript.RegisterClientScriptBlock(typeof(Page), "ALERT""alert('All field cannot be empty');"true);  
  33.            }  
  34.            BindGridView(GetAllEmployee());  
  35.   
  36.        } 
 
 I want to create row only in 4 fields first name to position only. Please help me with the appropriate code.
 

Answers (5)