Home > ASP.NET > bind gridview with datatable in asp.net c#



bind gridview with datatable in asp.net c#

In this article, we have described how to bind gridview with datatable in asp.net c #.


In this example we have described how to bind gridview with datatable in asp.net C# with Mysql .

 

About Gridview

Gridview is a control in asp.net.

Gridview is a very popular and useful control in C#. We can specify custom columns and styles.

Gridview automatically binds to and displays data from a data source control.

 

Required Namespaces:

you will need to use the following namespaces.

using System.Data;

using MySql.Data.MySqlClient;

using System.Configuration;

 

aspx page

We have used the gridview code below. You can have a look at it and learn.

<html xmlns="">

<head runat="server">

   <title>HOW TO USE DATATABLE IN ASP.NET</title>

</head>

<body>

   <form id="form1" runat="server">

       <asp:GridView ID="gvUsers" calss="table table-bordered responsive-table" AutoGenerateColumns="true" runat="server">

       </asp:GridView>

   </form>

</body>

</html>

 

C#

We have explained below C# code and added the examples of some user information in this article .You can have a look at it and learn.

string mysqlcomnnection = Convert.ToString(ConfigurationManager.AppSettings["connectioninfo"]);

protected void Page_Load(object sender, EventArgs e)

{

   if (!IsPostBack)

   {

       BindGridview();

   }

}

private void BindGridview()

{

   MySqlConnection con = new MySqlConnection(mysqlcomnnection);

   string CommandText = "Select userid as UserID, firstname as FirstName,lastname as LastName,logon as LoginId from users";

   MySqlCommand cmd = new MySqlCommand(CommandText, con);

   MySqlDataAdapter sda = new MySqlDataAdapter(cmd);

   DataTable dt = new DataTable();

   sda.Fill(dt);

   gvUsers.DataSource = dt;

   gvUsers.DataBind();

}




Note: All contents are copyright of their authors.



Comments



Leave a Reply

Comment:*
Email ID:*


Name:*