Simple GridView Data Binding in ASP.NET

I am going to write a blog on "Gridview Code". This is my first blog on Simple Gridview Data Binding using c#.

Source Code

<div>
    <asp:GridView ID="grdCompany" runat="server">
    </asp:GridView>

</div>

C# Code

protected void Page_Load(object sender, EventArgs e)

{
    if (!IsPostBack)

        bindGridview();

}
 

public DataTable getDataTable()

{

   DataTable dt = new DataTable();

    try

    {

        dt = new DataTable();

        dt.Columns.Add("id", typeof(int));

        dt.Columns.Add("company", typeof(string));

        dt.Columns.Add("address", typeof(string));

        dt.Columns.Add("pincode", typeof(string));
 
        dt.Rows.Add(1, "Capgemini", "Vikhroli East, Mumbai", "400 079");

        dt.Rows.Add(2, "TCS", "Tech Park, Bangalore", "560066");

        dt.Rows.Add(3, "Wipro IT", "Guindy, Chennai", "600032");

    }

    catch { throw; }

    return dt;

}
 

public void bindGridview()

{
    grdCompany.DataSource = getDataTable();

    grdCompany.DataBind();

}

Code Output

image1.jpg

Ebook Download
View all
css
Read by 0 people
Download Now!
Learn
View all