Can I display the last row of a table(in Sql 2005) in a gridview?
When i write this code: datatable.rows(datatable.rows.count -1);
my program has a error :
Error:Non-invocable member 'System.Data.DataTable.Rows' cannot be used like a method.
pls help
thanx
Loading
elnaz zPosted Jan 24, 2011, 6:22 AM
i found answer for my question
select top 1 * from tbl order by id desc
or
datatable dt=new datatable;
DataRow dr = dt.Rows[dt.Rows.Count-1];
but i think answer 1 is better.
Suthish NairPosted Jan 24, 2011, 6:02 AM
ShankeyPosted Jan 24, 2011, 3:46 AM
again i tried
following is your .cs file 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;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
DataTable dt = new DataTable();
dt.Columns.Add(new DataColumn("Total",typeof(string)));
dt.Rows.Add("5");
dt.Rows.Add("6");
dt.Rows.Add("7");
dt.Rows.Add("8");
DataRow dr = dt.Rows[dt.Rows.Count-1];
DataTable dttemp = new DataTable();
dttemp.Columns.Add(new DataColumn("Total", typeof(string)));
dttemp.ImportRow(dr);
gv.DataSource = dttemp;
gv.DataBind();
}
}
following is your .aspx code
<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
CodeFile="Default.aspx.cs" Inherits="_Default" %>
Welcome to ASP.NET!
To learn more about ASP.NET visit www.asp.net.
You can also find title="MSDN ASP.NET Docs">documentation on ASP.NET at MSDN.
hope it will help you
ShankeyPosted Jan 24, 2011, 3:39 AM
Instead of following
datatable.rows(datatable.rows.count -1);
use following
dt.Rows[dt.Rows.Count-1];
ShankeyPosted Jan 24, 2011, 3:21 AM
You can assign like shown below
DataRow dr = datatable.rows(datatable.rows.count -1);
now add above dr to your new datatable
and then bind it to your gridview