Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add new row data to gridview asp.net c#

I've made 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 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 = "[email protected]";
    lstCustomer.Add(new Customer(customer));

    customer.ID = 2;
    customer.Name = "Mokesh";
    customer.FatherName = "Rajnikant";
    customer.Email = "[email protected]";
    lstCustomer.Add(new Customer(customer));

    customer.ID = 3;
    customer.Name = "Bilal Ahmad";
    customer.FatherName = "Kashif";
    customer.Email = "[email protected]";
    lstCustomer.Add(new Customer(customer));

    customer.ID = 4;
    customer.Name = "Chin Shang";
    customer.FatherName = "Shang Woe";
    customer.Email = "[email protected]";
    lstCustomer.Add(new Customer(customer));

    return lstCustomer;
}

returning this list to bind with the grid. The code is :

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

My questions are :

  1. I added 4 textboxes and a button to an aspx page with the names: Id,Name,FatherName,Email When I click on the button, I want add the new values of the textboxes to gridview1 row. I want to add a row to the gridview dynamically.

  2. If I define an empty gridview, how can I add my textbox values to gridview rows? Is not equal method with question1 ?

like image 655
Ali Ahmadi Avatar asked Sep 08 '13 06:09

Ali Ahmadi


1 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 = "[email protected]";
        lstCustomer.Add(new Customer(customer));

        customer.ID = 2;
        customer.Name = "Mokesh";
        customer.FatherName = "Rajnikant";
        customer.Email = "[email protected]";
        lstCustomer.Add(new Customer(customer));

        customer.ID = 3;
        customer.Name = "Bilal Ahmad";
        customer.FatherName = "Kashif";
        customer.Email = "[email protected]";
        lstCustomer.Add(new Customer(customer));

        customer.ID = 4;
        customer.Name = "Chin Shang";
        customer.FatherName = "Shang Woe";
        customer.Email = "[email protected]";
        lstCustomer.Add(new Customer(customer));
        Session["dt"] = lstCustomer;

    }
}
like image 190
Samiey Mehdi Avatar answered Sep 23 '22 00:09

Samiey Mehdi