How to bind gridview in asp.net

0
32

In this section, I am going to tell you how to bind gridview in ASP.Net C#.

If We have Data like below in your Database and you want to show in GridView.

SerialNameMotherNameFatherName
1SUMANVIMLA DEVILALLAN PRASAD
2PUNAM SINGHRITA DEVIKISHORI SINGH
3CHANDAJAIBOON NESHAMUHAMMAD LATIF
4KUSUM LATAPREMLATA SINGHOM PRASAD
5ANUPAMAMALATI DEVISUBASH CHANDRA

First of All, You have to add GridView in your aspx page.

Step 1 – Add GridView in aspx page in ASP.Net C#

For Example : <asp:GridView ID=”gvData” runat=”server”></asp:GridView>

Step 2 – Adding Labels to show data

For Example :

<asp:GridView ID="gvData" runat="server">
<Columns>
<asp:BoundField DataField="Serial" HeaderText="Serial Number" ItemStyle-Width="150px" />
<asp:BoundField DataField="Name" HeaderText="Student Name" />
<asp:BoundField DataField="MotherName" HeaderText="Mother Name" />
<asp:BoundField DataField="FatherName" HeaderText="Father Name" />
</Columns>
</asp:GridView>

You should know that in BoundField, there is a DataField and Header Text.

What is Data Field?

DataField is the Column Name in Table of your Database.

What is Header Text?

Header Text is the Header Name of your Data which you want to show in GridView.

Behind on Code C# Asp.Net

    MySqlConnection con = new MySqlConnection(ConfigurationManager.ConnectionStrings["con"].ConnectionString);

    private void PopulateData()
    {
        //Start Connection First
        if (con.State == ConnectionState.Closed)
        { con.Open(); }
        //Fetch Query here...
        MySqlDataAdapter da = new MySqlDataAdapter("select * from table_name order by id", con);
        DataTable dt = new DataTable();
        da.Fill(dt);
        con.Close();
        gvData.DataSource = dt;
        gvData.DataBind();
    }

Now, You will bind Data in Grid View.

Follow us for more information and contact us if you have any bussiness or personal query https://wa.me/919984177344

LEAVE A REPLY

Please enter your comment!
Please enter your name here