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

ShankeyRank #972 · 2.1K reputationPosted Jan 23, 2011, 10:21 PM EST
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
ShankeyRank #972 · 2.1K reputationPosted Jan 23, 2011, 10:39 PM EST
Instead of following
datatable.rows(datatable.rows.count -1);
use following
dt.Rows[dt.Rows.Count-1];
ShankeyRank #972 · 2.1K reputationPosted Jan 23, 2011, 10:46 PM EST
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
Suthish NairRank #92 · 22.8K reputationPosted Jan 24, 2011, 1:02 AM EST
elnaz z13 reputationPosted Jan 24, 2011, 1:22 AM EST
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.