简单啊!
DATAGRID有个属性EditItemIndex表示那行可以再用
DataKeyField可以设置主健字段名,如
mygrid.DataKeyField[(int)EditItemIndex]
DATAGRID有个属性EditItemIndex表示那行可以再用
DataKeyField可以设置主健字段名,如
mygrid.DataKeyField[(int)EditItemIndex]
解决方案 »
- 网页中嵌入winform,当页面刷新的时候,如何释放内存,重新加载winform?
- 能不能在页面实现大转盘抽奖的效果?不用Flash!
- 数据库密码存在什么地方最安全????
- 高手进来看看,奇怪的问题
- 如何在gridview中动态创建一个checkboxlist控件?
- 写一个数据访问类的问题!!
- 如何获得鼠标的坐标,并将该坐标存到数据库中?请教高人指点。
- test.htm文件里include 了一个top.aspx文件,显示网页的头部.为什么不显示呢,而把top.aspx里的代码直接写到test.htm里就能够显示了.
- EasyUI Dialog中有一个iframe,iframe里面的按钮如何关闭外面的Dialog
- 关于递归取值问题
- 让select和dropdownlist的初始话问题
- session 用法请教!
那就来个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")
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();
<%@ 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”表示“变量”,此处应为“方法”请问为什么这样?请赐教!
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
哦,我看到你的帖子了,不过分是退不了的,我只能帮你多回答几个问题了:)
int Count=reg1("iCount");
这句话出错的话,换成这样试试?
int count = rd.GetSqlInt32(0).Value
要using System.Data.SqlClient
yirenboy(一人),对不起,我要的是一个表的行总数:)。
后面加入
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;多试试就行了 :)