Wednesday 19 December 2012

Beginning .Net : Bind Strings List to a Grid View with C# Examples and VB.Net Examples

You can bind list of strings to gird view and display very uniform manner in a very easy way. List may be a generic list of strings. Generic List class is available in "System.Collections.Generic" namespace. We are using "DataSource" property of Grid view to set data source and "DataBind" method to bind data.

Here is example for this.

In this example we take one Generic String list and add some string item into that , After that we take one grid view and set "DataSource" property of grid view to String List object after that call "DataBind()" method to bind string data. You can see in output that string data is display in grid view.

ASPX Code :
    <div>
    
        <asp:GridView ID="gvFruits" runat="server">
        </asp:GridView>
    
    </div>

C# Examples :
    protected void Page_Load(object sender, EventArgs e)
    {
        List<string> strList = new List<string>();
        strList.Add("Apple");
        strList.Add("Banana");
        strList.Add("Mango");
        strList.Add("Orange");
        gvFruits.DataSource = strList;
        gvFruits.DataBind();
    }

VB.Net Examples :
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim strList As New List(Of String)
        strList.Add("Apple")
        strList.Add("Banana")
        strList.Add("Mango")
        strList.Add("Orange")
        gvFruits.DataSource = strList
        gvFruits.DataBind()

    End Sub

Output :
Bind Strings List to a Grid View








This article is very useful for Beginning .Net.

Note : Give Us your valuable feedback in comments. Give your suggestions in this article so we can update our articles accordingly that.



1 comment: