【gridview手动绑定行列,取DataKeys】
索引超出范围。必须为非负值并小于集合大小。参数名: index
在手动绑定表头的情况下,我要想取隐藏的key值怎么样去取
贴下代码:
前台:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="TestGridView._Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
</asp:UpdatePanel>
<asp:GridView ID="allmessage" runat="server" AutoGenerateColumns="False"
AutoGenerateDeleteButton="True" AutoGenerateEditButton="True"
onrowcommand="allmessage_RowCommand" onrowdeleting="allmessage_RowDeleting">
</asp:GridView>
</div>
</form>
</body>
</html>
后台:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
namespace TestGridView
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack){
allmessage.ID = "detailsmessage";
BoundField boundField2 = new BoundField();
allmessage.Columns.Add(boundField2);
boundField2.HeaderText = "学生学号 ";
boundField2.DataField = "StudentID";
boundField2.Visible = false;
BoundField boundField1 = new BoundField();
allmessage.Columns.Add(boundField1);
boundField1.HeaderText = "学生姓名 ";
boundField1.DataField = "StudentName";
DataTable dt = new DataTable();
dt.Columns.Add("StudentID", typeof(string));
dt.Columns.Add("StudentName", typeof(string));
dt.Rows.Add("1", "wanlin");
dt.Rows.Add("2", "chengdp");
allmessage.DataSource = dt;
allmessage.DataKeyNames = new string[] { "StudentName" };
allmessage.DataBind();
}
}
protected void allmessage_RowCommand(object sender, GridViewCommandEventArgs e)
{
string url = "";
if (e.CommandName == "Delete")
{
int index = Convert.ToInt32(e.CommandArgument);
DataKey key = this.allmessage.DataKeys[index];
string vehicleId = key.Value.ToString();
ScriptManager.RegisterStartupScript(UpdatePanel1, UpdatePanel1.GetType(), "", "alert('" + vehicleId + "!')", true);
}else if(e.CommandName=="Update")
{
}
}
protected void allmessage_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
// string aaa = allmessage.DataKeys[e.RowIndex][0].ToString();
//// string aaa=allmessage.DataKeys[e.RowIndex].Value.ToString();
//ScriptManager.RegisterStartupScript(UpdatePanel1, UpdatePanel1.GetType(), "", "alert('" + aaa + "!')", true);
}
}
}
--------------------编程问答-------------------- 在断电进入allmessage_RowCommand事件后的调试信息
e.CommandArgument
"1"
allmessage.Columns.Count
2
allmessage.Rows.Count
2
allmessage.Rows[0].Cells[0].Text
"" 这里为什么是"",而不是1呢
allmessage.SelectedDataKey
必须在 GridView“allmessage”上指定数据键,然后才能检索选定的数据键。使用 DataKeyNames 属性指定数据键。加载的时候我不是指定了datakey吗 怎么这里又没了
我太菜了希望大家多多指教 --------------------编程问答-------------------- 首先,你allmessage.Rows[0].Cells[0].Text为""的原因是应为你第一列是隐藏的。
另外,你说的"allmessage.SelectedDataKey”我在你代码中没有见到。
这是我一篇关于如何获取隐藏列的值的文章,你可以参考:
http://www.cnblogs.com/lerit/archive/2010/05/27/1744978.html
--------------------编程问答-------------------- lerit
(往事随风)
------------------
刚刚调试的都为空
allmessage.Rows[0].Cells[1].Text
""
allmessage.Rows[1].Cells[1].Text
""这个我没有隐藏也为空
allmessage.Rows[1].Cells[1].Text
""
所以我觉得你没有看懂我的意思
ps:我最关心的不是cell为不为空
目前只想把datekey取出来
至于其他信息只是在调试的时候调试到了而已 --------------------编程问答-------------------- 冒得人顶 ( ⊙ o ⊙ )啊! --------------------编程问答-------------------- 哦哦,看了一下,问题找到了,你不应该把绑定gridview的语句放在
if(!IsPostBack){
}里面,你试试放出来,就ok了,访问方法:
if (e.CommandName == "Delete")
{
int index = Convert.ToInt32(e.CommandArgument);
DataKeyArray key = this.allmessage.DataKeys;
string vehicleId = this.allmessage.DataKeys[index]["StudentName"].ToString();
}
记得结贴
补充:.NET技术 , ASP.NET