Wednesday, May 8, 2013

sample code for data bound in gridview asp.net

Here we describe data bound in grid view  with example :

Sample code as below

Default.aspx

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:GridView ID="gvboolywood" runat="server" Width="100%" ShowHeader="false" AllowPaging="true" OnPageIndexChanging="dopage"  AutoGenerateColumns="false" GridLines="None">
         <RowStyle BorderStyle="Dotted" BorderWidth="1px"/>
         <Columns>
         <asp:TemplateField>
         <ItemStyle CssClass="bordercss"/>
         <ItemTemplate>
         <a title='<%#Eval("Title") %>'  href=<%#getLink(Eval("postID").ToString())%>>
                <%#Eval("Title") %>
                </a>
         </ItemTemplate>
         </asp:TemplateField>
         </Columns>
         <PagerSettings Mode="Numeric" FirstPageText="Home" NextPageText="Next"/>
         <PagerStyle BackColor="Control"/>
         </asp:GridView>
    </div>
    </form>
</body>
</html>

 

Default.aspx.cs

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)
        {
            showBoolywood();
        }
    }
    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();
        ad.Fill(dS);

        if (dS != null && dS.Tables.Count > 0)
        {
            //rpPosts.DataSource = dS;
            //rpPosts.DataBind();
            if (dS.Tables.Count > 0)
            {
                gvboolywood.DataSource = dS.Tables[0];
                gvboolywood.DataBind();
            }
        }
       
    }

    protected void dopage(object sender, GridViewPageEventArgs e)
    {
        if (e.NewPageIndex > 0 && e.NewPageIndex < gvboolywood.PageCount)
        {
            gvboolywood.PageIndex = e.NewPageIndex;
        }
        else
        {
            gvboolywood.PageIndex = e.NewPageIndex;
        }
        showBoolywood();

    }
    public static string getLink(string str)
    {

        return "Songs.aspx?MID=" + str;
    }
}

 

Source code download :


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

0 comments:

Post a Comment