1

I have created two TextBoxes to enter the FirstName and LastName of an employee in a web-based ASP.NET application using C# on Visual studio 2010. There is a Button. when I click on it, the values that I enter in the TextBoxes should be displayed in a Gridview without those values being stored in the database. I have already implemented it using DataTables as shown below. But now I want to implement it using ArrayList. How can I do that? Can you provide a sample code to execute the above mentioned functionality?

How can I tweak the code given below, to implement the functionality using ArrayList instead of DataTables?

**

protected void Page_Load(object sender, EventArgs e)
{
        DataTable dt = new DataTable();
        DataColumn dc1 = new DataColumn("FIRST NAME");
        DataColumn dc2 = new DataColumn("LAST NAME");
        dt.Columns.Add(dc1);
        dt.Columns.Add(dc2);
        DataRow dr1 = dt.NewRow();
        GridView1.DataSource = dt;
        GridView1.DataBind();
}
protected void Button1_Click(object sender, EventArgs e)
{
    DataRow dr1 = dt.NewRow();
    dr1[0] = TextBox1.Text;
    dr1[1] = TextBox2.Text;
    dt.Rows.Add(dr1); 
    GridView1.DataSource = dt;
    GridView1.DataBind();
}

**

1 Answer 1

3

I would recommend to use typed list List<T> instead of ArrayList it will save you from lot of type casting in your code. You can read more about advantages of using types list here. Make a class of lets say Employee with data members FirstName, LastName etc and make of list of Employees and bind it with GridView.

public class Employee
{
    public FirstName {get; set;}
    public LastName {get; set;}
    public Address {get; set;}
}

List<Employee> employees = List<Employee>();
employees.Add(new Employee{FirstName: "john", LastName: "Thunder", Address: "addr"});
gridView1.DataSource = employees;
gridView1.DataBind();
Sign up to request clarification or add additional context in comments.

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.