7

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 ?

0

4 Answers 4

6

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;

    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

When I click add button, first list cleared and new row data added. How can I store first list?
2

You will need to persist your collection during postback. Depending on how big your collection is you can consider keeping it in viewstate, session, etc.

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

So when the user fills the textboxes with data and initiates a postback, you can create a new customer object and add it to the existing collection. Then rebind the data to 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 you will need to add the [Serializable] attribute to your Customer class.

2 Comments

When I click add button, first list cleared and new row data added. How can I store first list?
ViewState["Customers"] = lstCustomer; will store the collection in the viewstate. then when adding new data, you first pull the collection from the Viewstate and then add the new customer to it (second code block)
0

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

Comments

0

So, I'd do it like this. Firstly I'd extend the scope of lstCustomer so that it persists through postbacks.

Then in the button event handler, I'd 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();

This also helps if you want to let the user perform some things on the data (or you want to persist to the database or something).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.