Gridview datakeys

本程序错误提示:“GridViewName.DataKeys[SelectedIndex].Value.Tostring();索引超出范围。必须为非负值并小于集合大小。.... 本程序错误提示:“GridViewName.DataKeys[SelectedIndex].Value.Tostring(); 索引超出范围。必须为非负值并小于集合大小。..."查看microsoft visual studio 2005 帮助文档的例子:<%@ Page language="C#" %><script runat="server"> void CustomersGridView_SelectedIndexChanged(Object sender, EventArgs e) { // Determine the index of the selected row. int index = CustomersGridView.SelectedIndex; // Display the primary key value of the selected row. Message.Text = "The primary key value of the selected row is " + CustomersGridView.DataKeys[index].Value.ToString() + "."; }</script><html> <body> <form runat="server"> <h3>GridView DataKeys Example</h3> <asp:label id="Message" forecolor="Red" runat="server"/> <br/><br/> <asp:gridview id="CustomersGridView" datasourceid="CustomersSource" autogeneratecolumns="true" emptydatatext="No data available." autogenerateselectbutton="true" datakeynames="CustomerID" onselectedindexchanged="CustomersGridView_SelectedIndexChanged" runat="server"> </asp:gridview> <!-- This example uses Microsoft SQL Server and connects --> <!-- to the Northwind sample database. Use an ASP.NET --> <!-- expression to retrieve the connection string value --> <!-- from the Web.config file. --> <asp:sqldatasource id="CustomersSource" selectcommand="Select [CustomerID], [CompanyName], [Address], [City], [PostalCode], [Country] From [Customers]" connectionstring="<%$ ConnectionStrings:NorthWindConnectionString%>" runat="server"/> </form> </body></html>新建一个空网站,把代码复制过去,还是会提示错误:”CustomersGridView.DataKeys[index].Value.ToString() 索引超出范围。必须为非负值并小于集合大小“ 怎么回事啊??应该怎么解决?? 展开
 我来答
1987711ning
2012-07-23
知道答主
回答量:17
采纳率:0%
帮助的人:15.9万
展开全部
你这个datakeys是在哪个方法中用的?
方法不一样,索引值获取方法不同
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式