e.Item.ItemIndex获取值超出范围??

zzxap 2008-07-15 01:34:09

string ID =MyDataGrid.DataKeys[e.Item.ItemIndex].ToString();
e.Item.ItemIndex是獲取第一列的id?
获取第二列第三列相应行值是用什么语句?
提示错误
Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index
...全文
196 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
jacksion520 2008-07-15
  • 打赏
  • 举报
回复
datagrid设置好主键字段后,只要是通过System.Web.UI.WebControls.DataGridCommandEventArgs e对象得到的e.Item.ItemIndex都是当前行的索引值,而不是你说的一定获取第一行的值,相应的为了获取e.Item.ItemIndex行的某列的值可以这样做e.Item.Cells[0].Text(第一列),e.Item.Cells[1].Text(第二列).........
brooklyng60 2008-07-15
  • 打赏
  • 举报
回复
顶8L的,要设置DataKeyField="id" 属性后
然后在事件中写MyDataGrid.DataKeys[e.Item.ItemIndex].ToString();
如果是在RowDataBound()事件,别忘了加RowType的条件
静_心 2008-07-15
  • 打赏
  • 举报
回复
换用别的方式看看.
criedshy 2008-07-15
  • 打赏
  • 举报
回复
for(int i=0;i<e.item.cells.Count;i++)
{
Response.Write(e.item.cells[i]);
}
一品梅 2008-07-15
  • 打赏
  • 举报
回复
<asp:datagrid id="dg1" runat="server" Height="342px" OnSelectedIndexChanged="dg1_SelectedIndexChanged"
AllowSorting="True" Width="424px" AutoGenerateColumns="False" DataKeyField="id" BorderStyle="Dotted" AllowPaging="True" OnPageIndexChanged="cgpage"
OnDeleteCommand="usersoft_Delete" OnCancelCommand="dg1_Cancel" OnEditCommand="dg1_Edit" OnUpdateCommand="dg1_Update">
<Columns>
<asp:BoundColumn DataField="id" HeaderText="id"></asp:BoundColumn>
<asp:BoundColumn DataField="au_id" HeaderText="au_id"></asp:BoundColumn>
<asp:BoundColumn DataField="au_fname" HeaderText="密码"></asp:BoundColumn>
<asp:BoundColumn DataField="au_lname" HeaderText="用户名"></asp:BoundColumn>
<asp:EditCommandColumn ButtonType="LinkButton" UpdateText="更新" CancelText="取消" EditText="编辑"></asp:EditCommandColumn>
<asp:ButtonColumn Text="删除" CommandName="Delete"></asp:ButtonColumn>
</Columns>
<PagerStyle NextPageText="下一页" PrevPageText="上一页" Position="TopAndBottom"></PagerStyle>
</asp:datagrid>
你在aspx中设置没?
一品梅 2008-07-15
  • 打赏
  • 举报
回复
e.item.cells[1];
e.item.cells[2];
绑定列?模板列?
zzxap 2008-07-15
  • 打赏
  • 举报
回复
MyDataGrid.DataKeys[e.Item.ItemIndex].ToString();
这句是获取当前行主键列的值,
那么获取当前行其他列的值怎么么获取?
wzy_love_sly 2008-07-15
  • 打赏
  • 举报
回复
获取第二列第三列相应行值是用什么语句?

下标是0开始,二列和三列用 1和2表示
amandag 2008-07-15
  • 打赏
  • 举报
回复
你的DataGrid设置DateKeyField属性为主键列了么?
Mylida 2008-07-15
  • 打赏
  • 举报
回复
e.Item.ItemIndex //获取当前行的索引
e.Item.Cell[1].Text
e.Item.Cell[2].Text
nopole 2008-07-15
  • 打赏
  • 举报
回复
MyDataGrid.DataKeys[e.Item.ItemIndex].ToString();

这只是根据设置的 MyDataGrid 的关键字字段 来得到 第e.Item.ItemIndex行的 关键字字段的值
  • 打赏
  • 举报
回复
你的这个e.Item是在什么事件里呢?

62,133

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧