Add new row data in gridview asp.net c #

I created a class with this code:

public class Customer { public Customer() { } public Customer(Customer cust) { ID = cust.ID; Name = cust.Name; FatherName = cust.FatherName; Email = cust.Email; } public int ID { get; set; } public string Name { get; set; } public string FatherName { get; set; } public string Email { get; set; } } 

and created this function to load a list with some data:

 public List<Customer> Generate_Data() { List<Customer> lstCustomer = new List<Customer>(); Customer customer = new Customer(); customer.ID = 1; customer.Name = "John Cena"; customer.FatherName = "John"; customer.Email = " cena@gmail.com "; lstCustomer.Add(new Customer(customer)); customer.ID = 2; customer.Name = "Mokesh"; customer.FatherName = "Rajnikant"; customer.Email = " mokesh@gmail.com "; lstCustomer.Add(new Customer(customer)); customer.ID = 3; customer.Name = "Bilal Ahmad"; customer.FatherName = "Kashif"; customer.Email = " bilal@gmail.com "; lstCustomer.Add(new Customer(customer)); customer.ID = 4; customer.Name = "Chin Shang"; customer.FatherName = "Shang Woe"; customer.Email = " chinshang@gmail.com "; lstCustomer.Add(new Customer(customer)); return lstCustomer; } 

returns this list to snap to the grid. The code:

 List<Customer> lstCustomer = new List<Customer>(); lstCustomer = Generate_Data(); GridView1.DataSource = lstCustomer; GridView1.DataBind(); 

My questions:

  • I added 4 text fields and a button to the aspx page with the names: Id,Name,FatherName,Email When I click on the button, I want to add new text field values ​​to the gridview1 line. I want to add a row in gridview dynamically.

  • If I define an empty gridview, how can I add my text field values ​​to the gridview rows? Is the method equal to question1?

+7
c # gridview
source share
4 answers

Aspx:

 <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><br /> <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox><br /> <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox><br /> <asp:TextBox ID="TextBox4" runat="server"></asp:TextBox><br /> <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" /> <asp:GridView ID="GridView1" runat="server"/> 

Code behind:

 public class Customer { public Customer() { } public Customer(Customer cust) { ID = cust.ID; Name = cust.Name; FatherName = cust.FatherName; Email = cust.Email; } public int ID { get; set; } public string Name { get; set; } public string FatherName { get; set; } public string Email { get; set; } } protected void Button1_Click(object sender, EventArgs e) { List<Customer> lstCustomer = new List<Customer>(); if (Session["dt"] != null) { lstCustomer = (List<Customer>)Session["dt"]; } Customer customer = new Customer(); customer.ID = int.Parse(TextBox1.Text); customer.Name = TextBox2.Text; customer.FatherName = TextBox2.Text; customer.Email = TextBox2.Text; lstCustomer.Add(new Customer(customer)); GridView1.DataSource = lstCustomer; GridView1.DataBind(); Session["dt"] = lstCustomer; } 

Updated!

 protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { List<Customer> lstCustomer = new List<Customer>(); Customer customer = new Customer(); customer.ID = 1; customer.Name = "John Cena"; customer.FatherName = "John"; customer.Email = " cena@gmail.com "; lstCustomer.Add(new Customer(customer)); customer.ID = 2; customer.Name = "Mokesh"; customer.FatherName = "Rajnikant"; customer.Email = " mokesh@gmail.com "; lstCustomer.Add(new Customer(customer)); customer.ID = 3; customer.Name = "Bilal Ahmad"; customer.FatherName = "Kashif"; customer.Email = " bilal@gmail.com "; lstCustomer.Add(new Customer(customer)); customer.ID = 4; customer.Name = "Chin Shang"; customer.FatherName = "Shang Woe"; customer.Email = " chinshang@gmail.com "; lstCustomer.Add(new Customer(customer)); Session["dt"] = lstCustomer; } } 
+5
source share

You will need to save your collection during postback. Depending on how large your collection is, you might consider storing it in viewstate, session, etc.

 List<Customer> lstCustomer = new List<Customer>(); lstCustomer = Generate_Data(); ViewState["Customers"] = lstCustomer; GridView1.DataSource = lstCustomer; GridView1.DataBind(); 

Therefore, when a user fills in text fields with data and initiates a postback, you can create a new client object and add it to an existing collection. Then reinstall the data into the grid.

 Customer customer = new Customer(){ID=int.Parse(TextBox1.Text), Name = TextBox2.Text, FatherName = TextBox2.Text, Email = TextBox2.Text } var lstCustomer = ViewState["Customers"] as List<Customers>; lstCustomer.Add(customer); 

Note that you need to add the [Serializable] attribute to the Customer class.

+2
source share

You can simply add AutoGenerateInsertButton="True" to the <asp:GridView> .

0
source share

So, I would do it like this. First, I would expand the scope of lstCustomer so that it is saved using postbacks.

Then in the button event handler I would write something like this

 //Here we create the object Customer customer = new Customer(); customer.foo = txtFoo.Text; customer.bar = txtBar.Text; //.... //Then we add it to the list lstCustomer.Add(customer); //and databind on it again GridView1.DataSource = lstCustomer; GridView1.DataBind(); 

It also helps if you want the user to do some things in the data (or you want to save the database or something else).

0
source share

All Articles