Wednesday, May 8, 2013

sample code for data bound in gridview

Here we describe data bound in grid view  with example :
Sample code as below
<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">
<html xmlns="">
<head runat="server">
    <title>Untitled Page</title>
    <form id="form1" runat="server">
    <asp:GridView ID="gvboolywood" runat="server" Width="100%" ShowHeader="false" AllowPaging="true" OnPageIndexChanging="dopage"  AutoGenerateColumns="false" GridLines="None">
         <RowStyle BorderStyle="Dotted" BorderWidth="1px"/>
         <ItemStyle CssClass="bordercss"/>
         <a title='<%#Eval("Title") %>'  href=<%#getLink(Eval("postID").ToString())%>>
                <%#Eval("Title") %>
         <PagerSettings Mode="Numeric" FirstPageText="Home" NextPageText="Next"/>
         <PagerStyle BackColor="Control"/>

using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page
    SqlConnection con=new SqlConnection(ConfigurationManager.ConnectionStrings["connection"].ConnectionString);
    protected void Page_Load(object sender, EventArgs e)
        if (!IsPostBack)
    public void showBoolywood()
        string query = "select PostID,Title from posts";
        SqlCommand cmd = new SqlCommand(query, con);
        SqlDataAdapter ad = new SqlDataAdapter(cmd);
        DataSet dS = new DataSet();
        if (dS != null && dS.Tables.Count > 0)
            //rpPosts.DataSource = dS;
            if (dS.Tables.Count > 0)
                gvboolywood.DataSource = dS.Tables[0];
    protected void dopage(object sender, GridViewPageEventArgs e)
        if (e.NewPageIndex > 0 && e.NewPageIndex < gvboolywood.PageCount)
            gvboolywood.PageIndex = e.NewPageIndex;
            gvboolywood.PageIndex = e.NewPageIndex;
    public static string getLink(string str)
        return "Songs.aspx?MID=" + str;

Source code download : Download Source

If you are searching life partner. your searching end with now offer free matrimonial website which offer free message, free chat, free view contact information. so register here : Free matrimonial website