Search - Articles
DevASP.NET for ASP.NET, VB.NET, XML and C# (C-Sharp) Developers Tuesday, March 03, 2009
Dev Articles
Search Directory
ASP.NET
VB.Net
C-Sharp
SQL Server
 

Programmatically bind data to GridView using server controls in ASP.NET

Author: Alanna Kremer
Download Source Code : 1291_ControlsInGridView.zip

The purpose of this article is to explain that how you can find server controls in GridView and bind data to GridView using these controls programmatically.

 

  1. Open Visual Studio 2010
  2. File > New > Web Site
  3. Visual C# or Visual Basic > ASP.NET Empty Web Site > Click Ok
  4. Website > Add New Item > Web Form > Click Add
  5. Now Drag and Drop a “GridView” in Default aspx page from Data tab in Toolbox.

     
        <asp:GridView ID="GridView1" runat="server">
     
        </asp:GridView>

     
  6. Open Code Behind file and write below code. Include System.Data namespace.

    C#
     
    using System.Data;
     
    protected void Page_Load(object sender, EventArgs e)
    {
        GridView1.DataSource = GetData();
        GridView1.DataBind();
    }
     
    private DataTable GetData()
    {
        DataTable dt = new DataTable();
     
        dt.Columns.Add("ID", typeof(Int32));
        dt.Columns.Add("Name", typeof(string));
        dt.Columns.Add("Age", typeof(Int32));
        dt.Columns.Add("Country", typeof(string));
     
        dt.Rows.Add(1, "Shaun", 42, "USA");
        dt.Rows.Add(2, "Michael", 35, "Canada");
        dt.Rows.Add(3, "Wayne", 39, "UK");
        dt.Rows.Add(4, "Henry", 31, "France");
     
        return dt;
    }
     
    VB.NET
     
    Imports System.Data
     
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
        GridView1.DataSource = GetData()
        GridView1.DataBind()
    End Sub
     
    Private Function GetData() As DataTable
        Dim dt As New DataTable()
     
        dt.Columns.Add("ID", GetType(Int32))
        dt.Columns.Add("Name", GetType(String))
        dt.Columns.Add("Age", GetType(Int32))
        dt.Columns.Add("Country", GetType(String))
     
        dt.Rows.Add(1, "Shaun", 42, "USA")
        dt.Rows.Add(2, "Michael", 35, "Canada")
        dt.Rows.Add(3, "Wayne", 39, "UK")
        dt.Rows.Add(4, "Henry", 31, "France")
     
        Return dt
    End Function
     
     
    First create a sample data to bind with GridView as I have written a function that returns DataTable then I have bound this function to our GridView in Page Load event.
     
  7.  Write below code in Default.aspx page.


    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" OnRowDataBound="GridView1_RowDataBound">
                <Columns>
                    <asp:TemplateField>
                        <HeaderTemplate>
     
                            <table>
                                <tr>
                                    <td>
                                        <asp:Label ID="Label1" runat="server" Text="ID"></asp:Label>
                                    </td>
                                    <td>
                                        <asp:Label ID="Label2" runat="server" Text="Name"></asp:Label>
                                    </td>
                                    <td>
                                        <asp:Label ID="Label3" runat="server" Text="Age"></asp:Label>
                                    </td>
                                    <td>
                                        <asp:Label ID="Label4" runat="server" Text="Country"></asp:Label>
                                    </td>
                                </tr>
                            </table>
                        </HeaderTemplate>
                        <ItemTemplate>
                         <table>
                                <tr>
                                    <td>
                                        <asp:Label ID="lblID" runat="server" ></asp:Label>
                                    </td>
                                    <td>
                                        <asp:Label ID="lblName" runat="server" ></asp:Label>
                                    </td>
                                    <td>
                                        <asp:Label ID="lblAge" runat="server" ></asp:Label>
                                    </td>
                                    <td>
                                        <asp:Label ID="lblCountry" runat="server" ></asp:Label>
                                    </td>
                                </tr>
                            </table>
                        </ItemTemplate>
                    </asp:TemplateField>
                </Columns>
            </asp:GridView>
         


    I have added HeaderTemplate and ItemTemplate in TemplateField. Then I have added four Labels in both HeaderTemplate and ItemTemplate inside Table Rows.
     
  8. Write below RowDataBound event in code behind file.

    C#
     
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow) {
            Label lblID = (Label)e.Row.FindControl("lblID");
            Label lblName = (Label)e.Row.FindControl("lblName");
            Label lblAge = (Label)e.Row.FindControl("lblAge");
            Label lblCountry = (Label)e.Row.FindControl("lblCountry");
     
            lblID.Text = (DataBinder.Eval(e.Row.DataItem, "ID")).ToString();
            lblName.Text = (DataBinder.Eval(e.Row.DataItem, "Name")).ToString();
            lblAge.Text = (DataBinder.Eval(e.Row.DataItem, "Age")).ToString();
            lblCountry.Text = (DataBinder.Eval(e.Row.DataItem, "Country")).ToString();
        }
    }
     
    VB.NET
     
    Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs) Handles GridView1.RowDataBound
        If e.Row.RowType = DataControlRowType.DataRow Then
            Dim lblID As Label = CType(e.Row.FindControl("lblID"), Label)
            Dim lblName As Label = CType(e.Row.FindControl("lblName"), Label)
            Dim lblAge As Label = CType(e.Row.FindControl("lblAge"), Label)
            Dim lblCountry As Label = CType(e.Row.FindControl("lblCountry"), Label)
     
            lblID.Text = (DataBinder.Eval(e.Row.DataItem, "ID")).ToString()
            lblName.Text = (DataBinder.Eval(e.Row.DataItem, "Name")).ToString()
            lblAge.Text = (DataBinder.Eval(e.Row.DataItem, "Age")).ToString()
            lblCountry.Text = (DataBinder.Eval(e.Row.DataItem, "Country")).ToString()
        End If
    End Sub
     
    First find controls from GridView using FindControl() method. Then bind data values to these control by using Databinder.Eval() method.
     
  9. Press Ctrl+F5 and see result in browser. You can download complete source code.

 

   
Add Article Comment:
Name :
Email Address :
   
Comments :
 
   
<< Pass multiple command arguments on a button click in GridView in ASP.NET

Disclaimer - Privacy
© 2002-2017 DevASP.net