In this article i will show you how to bind a gridview using C# in asp.net. The code which i am going to show you is the simplest one which used for binding the gridview.
So here we go.
First you need to create a project . After creating a project add a blank page. In this blank page add a grid view control from your toolbox as shown below:
Now open your sql server and create a table in your data base . Here i have create a table named area for demo.
now save you table and minimize your sql server window.
Now again come to Visual studio. Open your webpage which you have created earlier, and at right top corner of you grid select the tip as shown below:
Now select edit template, a window will open
Now select bound field and click on add define HeaderText and DataFid. In header text we will add the header text for the fiend. In DataField we will add the table field Name which we are going to find.
after adding all the fields click on ok button. After click on ok button your grid will look as shown below:
Now for removing auto generated column we will select property window for grid view, and select autographed column false as shown below:
After doing all this go the your source code view.you will get your html page code:
<asp:GridView runat="server" AutoGenerateColumns="False" ID="griddemo">
<Columns>
<asp:BoundField DataField="AreaID" HeaderText="Area ID" />
<asp:BoundField DataField="AreaName" HeaderText="Area Name" />
<asp:CheckBoxField DataField="IsDeleted" HeaderText="Is Deleted" />
</Columns>
</asp:GridView>
<Columns>
<asp:BoundField DataField="AreaID" HeaderText="Area ID" />
<asp:BoundField DataField="AreaName" HeaderText="Area Name" />
<asp:CheckBoxField DataField="IsDeleted" HeaderText="Is Deleted" />
</Columns>
</asp:GridView>
Now open you web.config file and define connection string as shown below:
<connectionStrings>
<add name="ConnectionString1" connectionString="Data Source=Servername;Initial Catalog=DBname;udi=userid;pwd=password;Integrated Security=True"
providerName="System.Data.SqlClient" />
</connectionStrings>
<add name="ConnectionString1" connectionString="Data Source=Servername;Initial Catalog=DBname;udi=userid;pwd=password;Integrated Security=True"
providerName="System.Data.SqlClient" />
</connectionStrings>
Now open your web page and open your .cs page. and add the following code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
namespace DataGridBinding
{
public partial class About : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
DataTable dt = new DataTable();
SqlConnection objcon = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString1"].ToString());
SqlDataAdapter objda = new SqlDataAdapter("select * from AreaTable", objcon);
objda.Fill(dt);
if (dt.Rows.Count > 0)
{
griddemo.DataSource = dt;
griddemo.DataBind();
}
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
namespace DataGridBinding
{
public partial class About : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
DataTable dt = new DataTable();
SqlConnection objcon = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString1"].ToString());
SqlDataAdapter objda = new SqlDataAdapter("select * from AreaTable", objcon);
objda.Fill(dt);
if (dt.Rows.Count > 0)
{
griddemo.DataSource = dt;
griddemo.DataBind();
}
}
}
}
Above code description is as follows:
1. First we have define datatable.
2. Now define connectionstring
3. Define sqldataadaptor with sql query
4. Now fill data table
5. Check weather we have got any record from data base or not. if count is greater the 0 then we will bind the grid.
So here is the final output:
No comments:
Post a Comment