vijay

welcome Netizen

Share Your Knowledge.It is a way to achieve immortality

Tuesday, May 29, 2012

Bind data in gridview using List in asp.net


Introduction:
Here we discuss about to find the alternate way to bind the data in gridview or datalist or repeater control.
Usually we used the dataset or datatable to bind the data in gridview, but here new way of binding is using List type.
Here what we going to do create a new list with some data and bind the data in gridview. This process must help when using web service.
Because they mostly used class in web service, by creating list is the best way to bind in data control.

Initially we see the design view code..
<div>
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">
            <Columns>
                <asp:BoundField HeaderText="Name" DataField="name" />
                <asp:BoundField HeaderText="Rollno" DataField="roll" />
            </Columns>
        </asp:GridView>
    </div>

C# code:
First we create a one class by using the class store the data by get set method...
public class stud
    {
        public stud(string name,string roll)
        {
           _name=name;
            _rollno =roll;
        }
        private string _name;
        private string _rollno;

        public string name
    {
       get {return _name;}
         set { _name=value ;}
    }
        public string roll{
            get{return _rollno;}
            set{_rollno =value;}
        }
      
}

protected void Page_Load(object sender, EventArgs e)
        {
            List <stud > lst=new List<stud>() ;
            lst.Add(new stud("Raj", "01"));
            lst.Add(new stud("Kumar", "02"));
            lst.Add(new stud("Rahul", "03"));
             //*******Here we bind the list type in grid view*******/
             /**************www.dotnetcode.in***********************/
           GridView1.DataSource = lst;
           GridView1.DataBind();
        }

VB code:
Public Class customer
        Public Sub New(ByVal name As String, ByVal roll As String)
            _name = name
            _roll = roll
        End Sub
        Private _name As String
        Private _roll As String

        Public Property name() As String
            Get
                Return _name
            End Get
            Set(ByVal value As String)
                _name = value
            End Set
        End Property

        Public Property roll() As String
            Get
                Return _roll
            End Get
            Set(ByVal value As String)
                _roll = value
            End Set
        End Property
    End Class
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        loadgrd()
    End Sub
Sub loadgrd()
        fselect = "select * from phone"
        rtable = obj.ExecuteData(fselect)
        Dim stud As List(Of customer) = New List(Of customer)()
        stud.Add(New customer("Ram", "45"))
        stud.Add(New customer("Kavitha", "50"))
        stud.Add(New customer("Neha", "60"))
        GridView1.DataSource = stud
        GridView1.DataBind()
    End Sub

0 comments:

Post a Comment