C#GridView参数引用超出范围

chuyige 2012-07-03 04:43:41
前台代码:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
CellPadding="4"
ForeColor="#333333" GridLines="None"
OnRowDeleting="GridView1_RowDeleting"
OnRowEditing="GridView1_RowEditing"
OnRowUpdating="GridView1_RowUpdating"
OnRowCancelingEdit="GridView1_RowCancelingEdit" Width="900px"
Height="339px">
<FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
<Columns>
<asp:BoundField DataField="ID" HeaderText="客户编号" />
<asp:BoundField DataField="Master_Name" HeaderText="客户姓名" />
<asp:BoundField DataField="Master_Address" HeaderText="客户地址" />
<asp:BoundField DataField="Master_Tel" HeaderText="客户电话" />
<asp:BoundField DataField="Mark" HeaderText="备注" />
<asp:CommandField HeaderText="选择" ShowSelectButton="True" />
<asp:CommandField HeaderText="编辑" ShowEditButton="True" />

<asp:CommandField HeaderText="删除" ShowDeleteButton="true" />
</Columns>
<RowStyle ForeColor="#000066" />
<SelectedRowStyle BackColor="#669999" Font-Bold="true" ForeColor="White" />
<PagerStyle BackColor="White" ForeColor="#000066" HorizontalAlign="Left" />
<HeaderStyle BackColor="#006699" Font-Bold="true" ForeColor="White" />
</asp:GridView>
后台代码:
//删除
protected void GridView1_RowDeleting(object sender,GridViewDeleteEventArgs e)
{

string S_Del_ID = (((TextBox)(GridView1.Rows[e.RowIndex].Cells[0].Controls[0])).Text.ToString().Trim());
string sqlstr = " delete from master where id=" + S_Del_ID + "";
sqlcon = new OleDbConnection(strcon);
sqlcom = new OleDbCommand(sqlstr, sqlcon);
sqlcon.Open();
sqlcom.ExecuteNonQuery();
sqlcon.Close();
bind();

}
//更新
protected void GridView1_RowUpdating(object sender,GridViewUpdateEventArgs e)
{
sqlcon =new OleDbConnection(strcon);
string S_ID = (((TextBox)(GridView1.Rows[e.RowIndex].Cells[0].Controls[0])).Text.ToString().Trim());
string S_Nane= (((TextBox)(GridView1.Rows[e.RowIndex].Cells[1].Controls[0])).Text.ToString().Trim());
string S_Address = (((TextBox)(GridView1.Rows[e.RowIndex].Cells[2].Controls[0])).Text.ToString().Trim());
string S_Tel = (((TextBox)(GridView1.Rows[e.RowIndex].Cells[3].Controls[0])).Text.ToString().Trim());
string S_Mark = (((TextBox)(GridView1.Rows[e.RowIndex].Cells[4].Controls[0])).Text.ToString().Trim());
string sqlstr = " update master set Master_Name='" + S_Nane + "' , Master_address='" + S_Address + "' ,Master_Tel='" + S_Tel + "',Mark='" + S_Mark + "' where id=" + S_ID + " ";

sqlcom=new OleDbCommand(sqlstr,sqlcon);
sqlcon.Open();
sqlcom.ExecuteNonQuery();
sqlcon.Close();
GridView1.EditIndex=-1;
bind();
}
删除代码里,提示(((TextBox)(GridView1.Rows[e.RowIndex].Cells[0].Controls[0])).Text.ToString().Trim());
指定的引數超出有效值的範圍。
參數名稱: index
可是更新里的就可以正常使用。
这是为什么?怎么样使删除里也可以正常使用。请指导,谢谢!
...全文
127 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
dalmeeme 2012-07-04
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 的回复:]

(GridView1.Rows[e.RowIndex].Cells[0].Text.Trim());
換成這個可以瞭,但是不明白為什麼?一樣的運用,為什麼一個地方行一個地方不行。
[/Quote]
你看下html源码就知道了,表格的第一列不同。
bdmh 2012-07-04
  • 打赏
  • 举报
回复
说明Cells[0]中没有Control,你如果访问 Controls [0],就会出错
Sam830131 2012-07-04
  • 打赏
  • 举报
回复
你的第一个CELL是BoundField

你用的Controls[0],相当于在CELL中又放一个控件
chuyige 2012-07-04
  • 打赏
  • 举报
回复
(GridView1.Rows[e.RowIndex].Cells[0].Text.Trim());
換成這個可以瞭,但是不明白為什麼?一樣的運用,為什麼一個地方行一個地方不行。
dalmeeme 2012-07-03
  • 打赏
  • 举报
回复
把删除里的Controls[0]去掉试试,写成:
(GridView1.Rows[e.RowIndex].Cells[0].Text.Trim();

110,536

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

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