Thursday 4 April 2013

Binding GridView Using C# in Asp.Net


Binding GridView Using C# in Asp.Net
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.

http://dotnetpools.com/images/ArticleImages/IMG-9531015461000000-678.png
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:

http://dotnetpools.com/images/ArticleImages/IMG-7657851431000000-88.png

Now select edit template, a window will open 

http://dotnetpools.com/images/ArticleImages/IMG-6046144721000000-931.png

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:

http://dotnetpools.com/images/ArticleImages/IMG-6127518321000000-933.png

Now for removing auto generated column we will select property window for grid view, and select autographed column false  as shown below:

http://dotnetpools.com/images/ArticleImages/IMG-7567794141000000-561.png

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>

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>

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();
            }
        }
    }
}

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:

http://dotnetpools.com/images/ArticleImages/IMG-9377409671000000-299.png
In this article i will show you how to bind a gridview which contain image using C# in asp.net. The code which i am going to show you is the simplest one which used for binding the gridview image.

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:

http://dotnetpools.com/images/ArticleImages/IMG-624934811000000-818.png

Now open your sql server and create a table in your data base . Here i have create a table named area for demo.

http://www.dotnetpools.com/images/ArticleImages/IMG-6309755801000000-59.png
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:

http://dotnetpools.com/images/ArticleImages/IMG-7657851431000000-88.png

Now select edit template, a window will open
 
http://www.dotnetpools.com/images/ArticleImages/IMG-528759781000000-658.png
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.
For image we will add
 TempleteField.

after adding all the fields click on ok button. After click on ok button your grid will look as shown below:

http://www.dotnetpools.com/images/ArticleImages/IMG-2632168601000000-238.png

Now for removing auto generated column we will select property window for grid view, and select
 autographed column false  as shown below:

http://dotnetpools.com/images/ArticleImages/IMG-7567794141000000-561.png

After this we will add image in image template.

http://www.dotnetpools.com/images/ArticleImages/IMG-1576313021000000-980.png

For binding image in grid view we have to add by using
 Eval("ImageUrl"). In beow screen show we have shown how to bind template using image.
http://www.dotnetpools.com/images/ArticleImages/IMG-1057759601000000-420.png

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:TemplateField HeaderText="Image">
                    <ItemTemplate>
                        <asp:Image ID="Image1" runat="server" Height="88px"
 
                            ImageUrl=`<%# Eval("ImageUrl") %>` Width="97px" />

                    </ItemTemplate>
                </asp:TemplateField>
                <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>

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();
            }
        }
    }
}

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:

http://www.dotnetpools.com/images/ArticleImages/IMG-1751673571000000-576.png


No comments:

Post a Comment