Click here to Skip to main content
15,885,309 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
This is .aspx code for gridview
ASP.NET
<asp:GridView ID="GridView2" runat="server" GridLines="None" AutoGenerateColumns="false" PageSize="1"
               onrowdatabound="GridView2_RowDataBound" align="center"
                OnPageIndexChanging="GridView2_PageIndexChanging" AllowPaging="true" >
               <PagerSettings FirstPageText="first" LastPageText="last" PageButtonCount="4" />
               <AlternatingRowStyle BackColor="White" />
               <RowStyle BackColor="#CCCCCC" />

           </asp:GridView>


This is my .cs code

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;

namespace tables
{
    public partial class Internship : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            
            if (!IsPostBack)
            {
                
                Recentinternship();
                
            }



        }

//for getting data in gridview

        protected void Recentinternship()
        {
            SqlConnection con = new SqlConnection("Data Source=jayraj-pc\\sqlexpress;Initial Catalog=Internship;Integrated Security=True;Pooling=False");
            con.Open();
            string str = "select B.CompanyLogo as ' ',A.Title,A.InternshipStartDate,A.ApplicationDeadline,A.Duration,A.InternshipCity,A.Category,A.Stipend,A.InternshipID,A.Degree,A.Branch,A.CompanyID,A.Title,A.CompanyName from Internship AS A INNER JOIN  Companies AS B ON A.CompanyID=B.CompanyID ";
            
            SqlDataAdapter da = new SqlDataAdapter(str, con);
            DataSet ds = new DataSet();
            da.Fill(ds, "0");

            

            GridView2.DataSource = ds.Tables["0"].ToString();
            GridView2.DataBind();
           
            con.Close();
        }
       
       

       

        protected void GridView2_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            string str = " Category :" + e.Row.Cells[6].Text + "<br/>Duration :" + e.Row.Cells[4].Text + "<br/>InternshipCity :" + e.Row.Cells[5].Text + "<br/>Stipend :" + e.Row.Cells[7].Text + "<br/>InternshipStartDate :" + e.Row.Cells[2].Text + "<br/>ApplicationDeadline :" + e.Row.Cells[3].Text + "<br/>Required Degree :" + e.Row.Cells[9].Text + "<br/>Required Branch :" + e.Row.Cells[10].Text;
            e.Row.Cells[1].Text = str;

            //e.Row.Cells[1].Visible = false;
            //e.Row.Cells[2].Visible = false;
            e.Row.Cells[3].Visible = false;
            e.Row.Cells[4].Visible = false;
            e.Row.Cells[5].Visible = false;
            e.Row.Cells[6].Visible = false;
            e.Row.Cells[7].Visible = false;
            e.Row.Cells[8].Visible = false;
            e.Row.Cells[9].Visible = false;
            e.Row.Cells[10].Visible = false;
            e.Row.Cells[11].Visible = false;
            e.Row.Cells[12].Visible = false;
            e.Row.Cells[13].Visible = false;


            if (GridView2.Rows.Count > 0)
            {
                GridView2.HeaderRow.Cells[0].Text = " ";
                GridView2.HeaderRow.Cells[1].Text = " ";
                GridView2.HeaderRow.Cells[2].Text = " ";

            }

            Image img = new Image();
            img.ID = "abc";
            img.ImageUrl = e.Row.Cells[0].Text;
            e.Row.Cells[0].Controls.Add(img);

            Button btn = new Button();
            //btn.Text = "preview";
            e.Row.Cells[2].Controls.Add(btn);
            btn.CausesValidation = false;
            btn.Text = "View and Apply";
            if (Session.Count > 0)
            {
               
                
                    btn.PostBackUrl = "Apply_internship.aspx?InternshipID=" + e.Row.Cells[8].Text + "&CompanyID=" + e.Row.Cells[11].Text + "&Title=" + e.Row.Cells[12].Text + "&CompanyName=" + e.Row.Cells[13].Text + "";
            }
            
            else
            {

                btn.PostBackUrl = "Apply_login.aspx";
            }

        }

        

        protected void GridView2_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GridView2.PageIndex = e.NewPageIndex;
            Recentinternship();
        }

       
       
    }
}
Posted
Updated 18-Apr-14 8:40am
v3
Comments
[no name] 18-Apr-14 10:52am    
And did you have some sort of a question or problem?
jayraj86 18-Apr-14 10:55am    
i'm getting this error "The data source does not support server-side data paging"
[no name] 18-Apr-14 11:06am    
Well why didn't you say so? http://www.google.com/search?q=The+data+source+does+not+support+server-side+data+paging

1 solution

use a datapager control..


C#
<div class="pager" style="margin-left: 20px; margin-top: 20px;">
                        <asp:DataPager ID="DataPager1" PagedControlID="GridView2" PageSize="10" runat="server"
                            Visible="true">
                            <Fields>
                                <asp:TemplatePagerField>
                                    <PagerTemplate>
                                        <span style="float: right; font-size: 10pt;">Page
                                            <asp:Label runat="server" ID="CurrentPageLabel" Text="<%# Container.TotalRowCount>0 ? (Container.StartRowIndex / Container.PageSize) + 1 : 0 %>" />
                                            of
                                            <asp:Label runat="server" ID="TotalPagesLabel" Text="<%# Math.Ceiling ((double)Container.TotalRowCount / Container.PageSize) %>" />
                                        </span>
                                    </PagerTemplate>
                                </asp:TemplatePagerField>
                                <asp:NextPreviousPagerField ButtonCssClass="command" FirstPageText="« First" PreviousPageText="‹ Prev"
                                    RenderDisabledButtonsAsLabels="true" ShowFirstPageButton="true" ShowPreviousPageButton="true"
                                    ShowLastPageButton="false" ShowNextPageButton="false" />
                                <asp:NumericPagerField ButtonCount="4" NumericButtonCssClass="command" CurrentPageLabelCssClass="current"
                                    NextPreviousButtonCssClass="command" />
                                <asp:NextPreviousPagerField ButtonCssClass="command" LastPageText="Last »" NextPageText="Next ›"
                                    RenderDisabledButtonsAsLabels="true" ShowFirstPageButton="false" ShowPreviousPageButton="false"
                                    ShowLastPageButton="true" ShowNextPageButton="true" />
                            </Fields>
                        </asp:DataPager>
                    </div>

<asp:GridView ID="GridView2" runat="server" GridLines="None" AutoGenerateColumns="false" PageSize="1"
                onrowdatabound="GridView2_RowDataBound" align="center"
                 OnPageIndexChanging="GridView2_PageIndexChanging" AllowPaging="true" >
                <AlternatingRowStyle BackColor="White" />
                <RowStyle BackColor="#CCCCCC" />

            </asp:GridView>
 
Share this answer
 
Comments
jayraj86 19-Apr-14 0:48am    
Error: Control 'GridView2' does not implement IPageableItemContainer.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900