Friday, March 6, 2015

Create Temporary table dynamically and bind to Gridview in asp.net

Introduction: In this article today we will learn how to Create Temporary table dynamically and bind to Gridview in asp.net

Description:
Temporary table

Html Markup Of page:
<asp:GridView ID="grduser" runat="server" AutoGenerateColumns="false">
        <HeaderStyle Font-Bold="true"/>
         <Columns>
      <asp:BoundField DataField="UserId" HeaderText="Id" ItemStyle-Width="30" ItemStyle-HorizontalAlign="Center"/>
        <asp:BoundField DataField="UserName" HeaderText="UserName" ItemStyle-Width="150" ItemStyle-HorizontalAlign="Center"/>   
        <asp:imagefield alternatetext="no image" headertext="Profile Image" dataimageurlfield="Profileimage" ControlStyle-Width="120" ControlStyle-Height ="100"  />   
    </Columns>
        </asp:GridView>


C# Code:
using System.Data;

protected void Page_Load(object sender, EventArgs e)
    {
        if(!IsPostBack)
        {
            Bindgrid();
        }
    }
    public void Bindgrid()
    {
        DataTable dt = new DataTable();
        dt.Columns.Add("UserId", typeof(Int32));
        dt.Columns.Add("UserName", typeof(string));
        dt.Columns.Add("Profileimage", typeof(string));

        DataRow dr = dt.NewRow();
        dr["UserId"] = 1;
        dr["Profileimage"] = ResolveUrl("~/images/vs.jpg");
        dr["UserName"] = "Vijay";
        dt.Rows.Add(dr);

        DataRow dr1 = dt.NewRow();
        dr1["UserId"] =2;
        dr1["Profileimage"] = ResolveUrl("~/images/download.jpg");
        dr1["UserName"] = "Rohan";
        dt.Rows.Add(dr1);

        DataRow dr2 = dt.NewRow();
        dr2["UserId"] = 3;
        dr2["Profileimage"] = ResolveUrl("~/images/cr.jpg");
        dr2["UserName"] = "John";
        dt.Rows.Add(dr2);
        grduser.DataSource = dt;
        grduser.DataBind();      
    }

VB Code:
Imports System.Data

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            Bindgrid()
        End If
    End Sub
    Public Sub Bindgrid()
        Dim dt As New DataTable()
        dt.Columns.Add("UserId", GetType(Int32))
        dt.Columns.Add("UserName", GetType(String))
        dt.Columns.Add("Profileimage", GetType(String))

        Dim dr As DataRow = dt.NewRow()
        dr("UserId") = 1
        dr("Profileimage") = ResolveUrl("~/images/vs.jpg")
        dr("UserName") = "Vijay"
        dt.Rows.Add(dr)

        Dim dr1 As DataRow = dt.NewRow()
        dr1("UserId") = 2
        dr1("Profileimage") = ResolveUrl("~/images/download.jpg")
        dr1("UserName") = "Rohan"
        dt.Rows.Add(dr1)

        Dim dr2 As DataRow = dt.NewRow()
        dr2("UserId") = 3
        dr2("Profileimage") = ResolveUrl("~/images/cr.jpg")
        dr2("UserName") = "John"
        dt.Rows.Add(dr2)
        grduser.DataSource = dt
        grduser.DataBind()
    End Sub

  Is this article helpful for you?

If yes post your comment to appreciate my work and fell free to contact me. You can like me on Facebook, Google+, Linkedin and Twitter via hit on Follow us Button and also can get update follow by Email.

No comments:

Post a Comment