hi , plz help me ..............
how to count to total record in grid view like that "1
-
20
of
65" nd show in footer.
thanks
Loading

Kirtan PatelRank #292 · 8.6K reputationPosted Aug 12, 2009, 4:25 AM EDT
if you getting error i have recoded it try to Populate data as Following Code Here is way How you will do it
Here is Screen shot How it will Look in Web page in Code I have Done Commenting so that you can understand it Properly and if you dont i have also included Project Files So that You can Take a Reference :)
dont forget to mark "Do you like answer" it will give me some credits :)
Projects File Link is at Bottom of Post Take a Look :)
add name space
using System.Data;
protected void Page_Load(object sender, EventArgs e)
{
//Connecting to Database And Fill it In Grid
SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True;User Instance=True");
con.Open();
SqlCommand comm = new SqlCommand("select * From Users", con);
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter(comm);
da.Fill(ds, "vtable");
GridView1.DataSource = ds.Tables[0];
con.Close();
//Showing Numbers in Label
int iTotalRecords = ((DataTable)(GridView1.DataSource)).Rows.Count;
int iEndRecord = GridView1.PageSize * (GridView1.PageIndex + 1);
int iStartsRecods = iEndRecord - GridView1.PageSize;
if (iEndRecord > iTotalRecords)
{
iEndRecord = iTotalRecords;
}
if (iStartsRecods == 0)
{
iStartsRecods = 1;
}
if (iEndRecord == 0)
{
iEndRecord = iTotalRecords;
}
Label7.Text = iStartsRecods + " to " + iEndRecord.ToString() + " of " + iTotalRecords.ToString();
DataBind();
}
Download Project Files
Master BillaRank #2,229 · 630 reputationPosted Aug 12, 2009, 12:39 AM EDT
Add gridview in your page and make all the necessary setting to gridview in and follow the steps here
Note:my code render record in fast and easy
1 Create SP
CREATE PROCEDURE [usp_GetProducts] @startRowIndex int, @maximumRows int, @totalRows int OUTPUT AS DECLARE @first_id int, @startRow int SET @startRowIndex = (@startRowIndex - 1) * @maximumRows IF @startRowIndex = 0 SET @startRowIndex = 1 SET ROWCOUNT @startRowIndex SELECT @first_id = ProductID FROM Products ORDER BY ProductID PRINT @first_id SET ROWCOUNT @maximumRows SELECT ProductID, ProductName FROM Products WHERE ProductID >= @first_id ORDER BY ProductID SET ROWCOUNT 0 -- GEt the total rows SELECT @totalRows = COUNT(ProductID) FROM Products GO2.Then write cdoe in your code behind to get the data calculate total row count
and all
private void BindData() { string connectionString = "Server=localhost;" + "Database=Northwind;Trusted_Connection=true"; SqlConnection myConnection = new SqlConnection(connectionString); SqlCommand myCommand = new SqlCommand("usp_GetProducts", myConnection); myCommand.CommandType = CommandType.StoredProcedure; myCommand.Parameters.AddWithValue("@startRowIndex", currentPageNumber); myCommand.Parameters.AddWithValue("@maximumRows", PAGE_SIZE); myCommand.Parameters.Add("@totalRows", SqlDbType.Int, 4); myCommand.Parameters["@totalRows"].Direction = ParameterDirection.Output; SqlDataAdapter ad = new SqlDataAdapter(myCommand); DataSet ds = new DataSet(); ad.Fill(ds); gvProducts.DataSource = ds; gvProducts.DataBind(); // get the total rows double totalRows = (int)myCommand.Parameters["@totalRows"].Value; lblTotalPages.Text = CalculateTotalPages(totalRows).ToString(); lblCurrentPage.Text = currentPageNumber.ToString(); if (currentPageNumber == 1) { Btn_Previous.Enabled = false; if (Int32.Parse(lblTotalPages.Text) > 0) { Btn_Next.Enabled = true; } else Btn_Next.Enabled = false; } else { Btn_Previous.Enabled = true; if (currentPageNumber == Int32.Parse(lblTotalPages.Text)) Btn_Next.Enabled = false; else Btn_Next.Enabled = true; } }3.add a support method also
private int CalculateTotalPages(double totalRows) { int totalPages = (int) Math.Ceiling(totalRows / PAGE_SIZE); return totalPages;}
4.you can use the two button to navigate record other than gridview paze navigation
asp:Button ID="Btn_Previous" CommandName="Previous" runat="server" OnCommand="ChangePage" Text="Previous" /> asp:Button ID="Btn_Next" runat="server" CommandName="Next" OnCommand="ChangePage" Text="Next" /> 5.just make it work to button click adn get the data display protected void ChangePage(object sender, CommandEventArgs e) { switch (e.CommandName) { case "Previous": currentPageNumber = Int32.Parse(lblCurrentPage.Text) - 1; break; case "Next": currentPageNumber = Int32.Parse(lblCurrentPage.Text) + 1; break; } BindData(); }Above code is most efficient way to do the paging.
check more details here
thank you
Danatas GerviRank #4,152 · 361 reputationPosted Aug 5, 2009, 11:55 PM EDT
Kirtan PatelRank #292 · 8.6K reputationPosted Aug 6, 2009, 12:06 AM EDT
you can Show record Like
1 to 20 of 51
20 to 40 of 51
40 to 51 of 51
follow the below Code
Happy Coding :)
urmila kandari22 reputationPosted Aug 11, 2009, 11:36 PM EDT
thanks 4 help but this code is not working
pls explain how it work properly
thanks
urmila kandari22 reputationPosted Aug 16, 2009, 9:48 PM EDT
thanks for code
Kangeyan D2 reputationPosted Mar 2, 2011, 8:54 PM EST
Suthish NairRank #92 · 22.8K reputationPosted Mar 3, 2011, 6:53 AM EST