简单啊!
DATAGRID有个属性EditItemIndex表示那行可以再用
DataKeyField可以设置主健字段名,如
mygrid.DataKeyField[(int)EditItemIndex]

解决方案 »

  1.   

    SORRY看错是要行数!
    那就来个DataReader
    SELECT语句就是seledt count(*) from 你的表,

    this.sqlCommand1.CommandText="select count(*) as iCount from dbo.[user] WHERE (login ='" +login_name+"')";
    this.sqlCommand1.Connection = this.sqlConnection1;
    SqlDataReader reg1=this.sqlCommand1 .ExecuteReader();
    reg1.Read ();
    int Count=reg1("iCount")
      

  2.   

    有几种方法,最简单的是用一个sql语句形如
    sqlCommand1.CommandText = "SELECT count(*) FROM table ";
    然后再用一个datareader读取即可,代码片断如下:
            this.sqlConnection1.Open();
    System.Data.SqlClient .SqlDataReader rd = this.sqlCommand1.ExecuteReader();


    while (rd.Read())
    {
    int count = rd.GetSqlInt32(0).Value;//这就是你要的表的总行数了!

    }
    rd.Close();
    this.sqlConnection1 .Close();
      

  3.   

    netwalking(不懂就问),我把你的代码输成下面这样:<%@ Page Language="C#" %>
    <%@ import Namespace="System.Data" %>
    <%@ import Namespace="System.Data.SqlClient" %>
    <script runat="server">    void Page_Load(object sender, EventArgs e) {
         
            if (!Page.IsPostBack) {
        
                // Databind the data grid on the first request only
                // (on postback, rebind only in paging command)
        
                BindGrid();
            }
        }
        
        void DataGrid_Page(object sender, DataGridPageChangedEventArgs e) {
        
            DataGrid1.CurrentPageIndex = e.NewPageIndex;
            
          
            BindGrid();
        }
        
        void BindGrid() {
        
            // TODO: update the ConnectionString and CommandText values for your application
            string ConnectionString = "server=(local)\\netsdk;database=pubs;trusted_connection=true";
            string CommandText = "select au_lname, au_fname, address, city, state from Authors order by au_lname";
        
            SqlConnection myConnection = new SqlConnection(ConnectionString);
            SqlDataAdapter myCommand = new SqlDataAdapter(CommandText, myConnection);
        
            DataSet ds = new DataSet();
            myCommand.Fill(ds);
        
            DataGrid1.DataSource = ds;
            DataGrid1.DataBind();
            
          BindGrid1();
             
        }
         void BindGrid1() {
        SqlConnection myConnection;
          myConnection = new SqlConnection("server=(local)\\NetSDK;database=pubs;Trusted_Connection=yes");        string CommandText = "select count(*) as iCount from Authors";
    SqlCommand myCommand = new SqlCommand(CommandText, myConnection);
    SqlDataReader reg1=myCommand.ExecuteReader();
    reg1.Read ();
    int Count=reg1("iCount");
            
        
          
             Message.InnerHtml = "content: " + count;
        }</script>
    <html>
    <head>
    </head>
    <body style="FONT-FAMILY: arial">
        <h2>Data Report with Paging 
        </h2>
        <hr size="1" />
        <form runat="server">
            <asp:datagrid id="DataGrid1" runat="server" width="80%" CellSpacing="1" GridLines="None" CellPadding="3" BackColor="White" ForeColor="Black" OnPageIndexChanged="DataGrid_Page" PageSize="6" AllowPaging="true">
                <HeaderStyle font-bold="True" forecolor="white" backcolor="#4A3C8C"></HeaderStyle>
                <PagerStyle horizontalalign="Right" backcolor="#C6C3C6" mode="NumericPages"></PagerStyle>
                <ItemStyle backcolor="#DEDFDE"></ItemStyle>
            </asp:datagrid>
        </form>
        <div style="font: 10pt verdana;padding:0,15,15,15" id="Message" runat="server"/>
    </body>
    </html>但是,它打出以下信息:
    编译器错误信息: CS0118: “reg1”表示“变量”,此处应为“方法”请问为什么这样?请赐教!
      

  4.   

    返回DataGrid的所在行的行号可以用两种方法解决(当然还有其他的方法了):
    1.可以设DataKeyField,这是DataGrid本身的属性(可以在它的属性中找到),然后把它绑定数据的主键.事例如下:
    --------------------------------------------------------------
    <asp:datagrid id="dgStoryTitleDelete" runat="server" DataKeyField="id" CellPadding="2" width="90%" PageSize="20" AllowPaging="true">这里的DataKeyField绑定的id是从你的数据库取出的主键.
    --------------------------------------------------------------
    在后台如何的获取,如下:
    string id=dgStoryTitleDelete.DataKeys[(int)e.Item.ItemIndex].ToString();
    这里就可以获取到该纪录在数据库的id号了.
    --------------------------------------------------------------
    在你的程序里面
    (前台)
    string CommandText = "select 主键, au_lname, au_fname, address, city, state from Authors order by au_lname"; <asp:datagrid id="DataGrid1" runat="server" width="80%" CellSpacing="1" GridLines="None" CellPadding="3" BackColor="White" ForeColor="Black" OnPageIndexChanged="DataGrid_Page" PageSize="6" AllowPaging="true" DataKeyField="主键" > 
    (后台)你还需要作一个事件返回到后台获取主键值.
    2.第二个方法就是每行绑定一个label把它设为不可见,然后把数据库数据所在行的主键值赋给这个label的text值,后台中把对应的label值取出即是该数据所在的行号.
    可参考帖子:
    http://expert.csdn.net/Expert/topic/1126/1126557.xml?temp=.3290064
    哦,我看到你的帖子了,不过分是退不了的,我只能帮你多回答几个问题了:)
      

  5.   

    如果是
    int Count=reg1("iCount");
    这句话出错的话,换成这样试试?
    int count = rd.GetSqlInt32(0).Value
      

  6.   

    对了你用SqlDataReader
    要using System.Data.SqlClient
      

  7.   

    romeoluo(安德的游戏),改成int count = rd.GetSqlInt32(0).Value之后,就弹出:ExecuteReader 需要打开的并且可用的连接。该连接的当前状态是 Closed。 netwalking(不懂就问),我的c#知识有限不知道using System.Data.SqlClient应该放在aspx页的什么地方,请指点!
    yirenboy(一人),对不起,我要的是一个表的行总数:)。
      

  8.   

    在SqlConnection myConnection = new SqlConnection(ConnectionString); 
    后面加入
    myConnection.open();
    对于using System.Data.SqlClient你已经引用过了。
    就是<%@ import Namespace="System.Data.SqlClient" %> 这个东西。
    获得datagrid的总行数:
    int i=dDataGrid1.items.count;
    如果获得ds中的记录即你从数据库中查询出来并fill到ds中的可用
    ds.Tables[0].Rows.Count;多试试就行了 :)