Question

I'm getting only 1 record using this code, but i want to display multiple records on page. I have 3 columns to display on page which are: id,name and lastname. How can I get this done?

Code Behind:

protected List<Class1> GetClass1()
{
    Class1 uinfo = new Class1();
    uinfo.ID = Convert.ToInt16(TextBox1.Text);
    uinfo.Name = TextBox2.Text;
    uinfo.LastName = TextBox3.Text;
    data.Add(uinfo);
    return data;   
}

protected void BindUserDetails()
{
    data = GetClass1();
    GridView1.DataSource = data;
    GridView1.DataBind();
}

Class file:

public class Class1
{
    Int16 id;
    string name = string.Empty;
    string lastname = string.Empty;

    public Int16 ID
    {
        get { return id; }
        set { id = value; }
    }

    public string Name
    {
        get { return name; }
        set { name = value; }
    }

    public string LastName
    {
        get { return lastname; }
        set { lastname = value; }
    }
}
Was it helpful?

Solution

I got one solution for you. This is just a sample implementation. Please modify according your requirement.

The Code :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;

namespace BindingListSample
{
    public partial class _Default : System.Web.UI.Page
    {
        static List<Employee> bindingL = new List<Employee>();
        protected void Btn_Click(object sender, EventArgs e)
        {
            bindingL.Add(new Employee { Name = TxtName.Text });
            GrvSample.DataSource = bindingL;
            GrvSample.DataBind();
        }
    }
    public class Employee
    {
        public string Name { get; set; }
    }
}

The problem is that you need to use static with your list. When you use static you can store the values that you have inserted until the application is closed. Refer Static Keyword for more explanation.

This is just my way to tackle this situation.

Hope This Helps

OTHER TIPS

 `List<Class1> data = new List<Class1>();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {

        }
        if (Session["test"] != null)
        {
            data = Session["test"] as List<Class1>;
        }
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        Class1 cl = new Class1 { ID = Convert.ToInt16(TextBox1.Text), Name = TextBox2.Text, LastName = TextBox3.Text };
        data.Add(cl);            
        Session["test"] = data;

        dataBind();
    }

    protected void dataBind()
    {
        GridView1.DataSource = data;
        GridView1.DataBind();
    }
}

}`

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top