ASP.NET C#GRIDVIEW将索引超出范围
IM正在处理用户控件,该用户控件在GridView上显示了从SQL表中可用的商店,供用户选择和填写文本框。当前,我能够正确地数据宾格到GridView,并在每行内的选择按钮运行操作。我目前的问题是,使用“行[索引]”时,我无法将单元格值返回到变量中。当我达到此问题时,即使在调试时我的索引值正确,我会得到索引超出范围错误。 出于实验目的,我在不同的迭代时给了该索引的手动值为0和1,但我仍然会遇到错误,就好像我的GridView不包含任何行一样。
我选择该行的方法是使用由ButtonField触发的OnrowCommand的GridView。
查看其他一些类似的问题和答案,我发现大多数人周围获得负值或不使用DatakeYnames的问题,但这不是我的情况。我仍然无法解决此错误
我的GridView代码是这样的:
<asp:GridView ID="gvStore" runat="server" AutoGenerateColumns="False" Visible="false" DataKeyNames="Name" OnRowCommand="gvExecSelectSTR_RowCommand">
<Columns>
<asp:BoundField DataField="Name" HeaderText="Name" />
<asp:ButtonField ButtonType="Link" Text="Select" CommandName="SelectStore"/>
</Columns>
</asp:GridView>
我的按钮操作
protected void gvExecSelectSTR_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "SelectStore")
{
int index = Convert.ToInt32(e.CommandArgument); //Up to this point i get correct row number for index
GridViewRow row = gvStore.Rows[index]; //This row triggers my error
IDStore.Text = row.Cells[0].Text;
}
}
是IM用于数据宾路的方法,以数据限在我的GridView中,
private void GridViewStore()
{
try
{
DataTable StoreTable = new DataTable();
StoreTable.Columns.AddRange(new DataColumn[1]
{
new DataColumn("Name",typeof(string))
});
//Can't show internal SQL commands for this but here i read and load
the SQLscript that selects the table and load it into the reader variable "readerStore"//
if (readerStore.HasRows)
{
while (readerStore.Read())
{
StoreTable.Rows.Add(readerStore["Name"].ToString());
}
}
gvStore.DataSource = StoreTable;
gvStore.DataBind();
readerStore.Close();
}
catch (Exception ex)
{
OnErrorOccurred(new ErrorOccurredEventArgs("Grid View Bind Message" + ex.Message, ex));
}
}
是否有任何问题设置错误?
Im working on a user control that shows available stores from an SQL table on a gridview for the user to select and fill up a textbox. Currently im able to databind everything correctly to the gridview and run actions with the select button inside each row. My current problem is that i can't get to return the cell value into a variable when using "rows[index]". When i reach this i get an Index is out of range error even if my index value is correct while debugging.
For experiment purpose i gave the index a manual value of 0 and 1 on different iterations and i still get the error as if my gridview didn't contain any rows.
My method to select the row is using a Gridview with an OnRowCommand triggered by a ButtonField.
Looking at some other similar questions and answers i found that most of the problems where around people getting negative values or not using DataKeyNames but this is not my case. I still can't fix this error
My gridview code is this:
<asp:GridView ID="gvStore" runat="server" AutoGenerateColumns="False" Visible="false" DataKeyNames="Name" OnRowCommand="gvExecSelectSTR_RowCommand">
<Columns>
<asp:BoundField DataField="Name" HeaderText="Name" />
<asp:ButtonField ButtonType="Link" Text="Select" CommandName="SelectStore"/>
</Columns>
</asp:GridView>
My button action
protected void gvExecSelectSTR_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "SelectStore")
{
int index = Convert.ToInt32(e.CommandArgument); //Up to this point i get correct row number for index
GridViewRow row = gvStore.Rows[index]; //This row triggers my error
IDStore.Text = row.Cells[0].Text;
}
}
Additionally, this is the method im using to databind everything to my GridView
private void GridViewStore()
{
try
{
DataTable StoreTable = new DataTable();
StoreTable.Columns.AddRange(new DataColumn[1]
{
new DataColumn("Name",typeof(string))
});
//Can't show internal SQL commands for this but here i read and load
the SQLscript that selects the table and load it into the reader variable "readerStore"//
if (readerStore.HasRows)
{
while (readerStore.Read())
{
StoreTable.Rows.Add(readerStore["Name"].ToString());
}
}
gvStore.DataSource = StoreTable;
gvStore.DataBind();
readerStore.Close();
}
catch (Exception ex)
{
OnErrorOccurred(new ErrorOccurredEventArgs("Grid View Bind Message" + ex.Message, ex));
}
}
Is there anything im setting up wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您是否尝试过而不是
这样做
Have you tried instead of
Doing this
为什么要打扰行命令?您确实不需要该事件模型,在大多数情况下,它只是会引起额外的痛苦。
并且,如果您有一个行PK ID,则单击按钮?然后使用Datakeys。 Datakeys非常好,原因有几个。
对于列表中的顶部,将数据库PK的范围揭示到客户端始终是一个巨大的安全漏洞。 (可以将标记弄乱,因此您现在使用的数据库ID可以是标记中更改的任何内容。
因此,简单的网格视图这样说:
注意以上“ ID”中的数据键是我们的行PK ID 我们没有,显示,在标记中显示PK行
ID
,非常好,
! 。
我们需要对大多数按钮进行单击事件,您只需单击按钮即可添加/电线仍然可以通过标记添加事件,
因此在OnClick中的标记类型中,Intel-Sense会弹出这样的选项来创建事件:
i.sstatic.net/ryzpr.png 在报价下,点击CTRL空间以弹出该Intel-Sense对话框)。
好的,选择创建新事件。
现在,翻转到后面的代码,应该为该按钮提供一个简单的单击事件。
因此,使用此代码:
输出:
没有混乱的行命令(然后检查某些行命令庇护)。
我轻松获得行索引。
我得到了从未暴露的数据库ID(来自Datakeys)的代码,
我可以自由从当前行获取/获取任何单元格值。
why bother with row command? You really don't need that event model, and in most cases, it just causes EXTRA pain.
And if you have a row PK id that you need on button click? Then use datakeys. Datakeys is VERY nice for several reasons.
For tops on the list, it always a HUGE security hole to expose data base PK's to the client side. (the markup can be messed with, and thus the database ID you use now could be anything that was changed in the markup.
So, simple grid view say like this:
Note the data keys setting in above "ID" is our row PK id, and VERY nice is we do not have, display, show the PK row id in the markup at ANY time!!!
And code to fill then is this:
And we now have this:
Now, we need a click event for the button. For most buttons, you can just double click on the button to add/wire up a event. However, since the button is inside of the GV, then we can't do that. But we can still add the event by markup.
So, in the markup type in onclick="" when you do this, intel-sense will pop up the option to create the event. Say like this:
(under the quotes, hit ctrl-space to pop that intel-sense dialog).
Ok, choose create new event.
Now, flip to code behind, there should be a simple click event for that button.
So, with this code:
Output:
So, with this simple setup:
If I have multiple buttons on the GV, then a get a nice simple SEPERATE click event and code stub. No messy row command (to then check some row command arugment).
I get the row index with ease.
I get the code behind NEVER exposed data base PK id (from datakeys)
I am free to get/grab any cell value from the current row.