主题:  datagrid删除问题???

ztong

职务:普通成员
等级:1
金币:0.0
发贴:129
#12006/1/24 16:42:36
我在程序中有以下代码:
public void materials_ItemCreated(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
if(e.Item.ItemType==ListItemType.Item||e.Item.ItemType==ListItemType.AlternatingItem)
{
System.Web.UI.WebControls.CheckBox CheckBox1=e.Item.FindControl("CheckBox1" as System.Web.UI.WebControls.CheckBox;
CheckBox1.CheckedChanged+= new EventHandler(CheckBox1_CheckedChanged);
}
if(e.Item.ItemIndex>-1)
{
LinkButton tmp = (LinkButton)e.Item.Cells[8].Controls[0];
tmp.Attributes.Add("onclick","return confirm('确认要删除该条记录吗?');";
}
}

public void materials_DeleteCommand(object O, DataGridCommandEventArgs E)
{
string selID=materials.DataKeys[E.Item.ItemIndex].ToString();
string sql="delete from 材料信息全 where 材料名称='"+selID+"'";
conn.openr();
SqlCommand cmd = new SqlCommand(sql,conn);
cmd.ExecuteNonQuery();
conn.Close();
this.binddatagrid();
}

public void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
this.SmartNavigation = true;
if(!Page.IsPostBack)
{
this.binddatagrid();
}
}
其中materials为datagrid,conn可与数据库连接。
html里有:
<aspatagrid id="materials" style="Z-INDEX: 105; LEFT: 16px; POSITION: absolute; TOP: 80px" runat="server" Width="100%" Height="360px" DataKeyField="材料名称" PagerStyle-Mode="NumericPages" FooterStyle-Font-Size="9pt" PagerStyle-Visible="false" PageSize="20" OnItemCreated="materials_ItemCreated" ShowFooter="True" PagerStyle-HorizontalAlign="Center" AutoGenerateColumns="False" AllowSorting="True" HorizontalAlign="Center"
AllowPaging="True" BackColor="White" BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px" CellPadding="3" OnDeleteCommand="materials_DeleteCommand" OnCancelCommand="materials_CancelCommand"
OnEditCommand="materials_EditCommand" OnItemDataBound="materials_ItemDataBound" OnUpdateCommand="materials_UpdateCommand">
<SelectedItemStyle Font-Bold="True" HorizontalAlign="Center" ForeColor="#CCFF99" BackColor="#009999"></SelectedItemStyle>
<EditItemStyle Wrap="False" HorizontalAlign="Center"></EditItemStyle>
<AlternatingItemStyle HorizontalAlign="Center"></AlternatingItemStyle>
<ItemStyle HorizontalAlign="Center" ForeColor="#000066"></ItemStyle>
<HeaderStyle Font-Bold="True" HorizontalAlign="Center" ForeColor="White" BackColor="#006699"></HeaderStyle>
<FooterStyle Font-Size="9pt" HorizontalAlign="Center" ForeColor="#000066" BackColor="White"></FooterStyle>
<Columns>
<asp:TemplateColumn>
<ItemTemplate>
<asp:CheckBox id="CheckBox1" runat="server" AutoPostBack="True" Checked="False"></asp:CheckBox></A>
</ItemTemplate>
</asp:TemplateColumn>
<asp:BoundColumn DataField="材料名称" HeaderText="材料名称">
<HeaderStyle Width="20%"></HeaderStyle>
<ItemStyle Font-Size="X-Small"></ItemStyle>
</asp:BoundColumn>
<asp:BoundColumn DataField="交件人" HeaderText="交件人">
<HeaderStyle Width="20%"></HeaderStyle>
<ItemStyle Font-Size="X-Small"></ItemStyle>
</asp:BoundColumn>
<asp:BoundColumn DataField="收件日期" HeaderText="收件日期">
<HeaderStyle Width="16%"></HeaderStyle>
<ItemStyle Font-Size="X-Small"></ItemStyle>
</asp:BoundColumn>
<asp:BoundColumn DataField="收件人" HeaderText="收件人">
<HeaderStyle Width="16%"></HeaderStyle>
<ItemStyle Font-Size="X-Small"></ItemStyle>
</asp:BoundColumn>
<asp:BoundColumn DataField="页数" HeaderText="页数">
<HeaderStyle Width="8%"></HeaderStyle>
<ItemStyle Font-Size="X-Small"></ItemStyle>
</asp:BoundColumn>
<asp:BoundColumn DataField="份数" HeaderText="份数">
<HeaderStyle Width="8%"></HeaderStyle>
<ItemStyle Font-Size="X-Small"></ItemStyle>
</asp:BoundColumn>
<asp:EditCommandColumn ButtonType="LinkButton" UpdateText="更新" CancelText="取消" EditText="编辑"></asp:EditCommandColumn>
<asp:ButtonColumn Text="删除" CommandName="Delete"></asp:ButtonColumn>
</Columns>
<PagerStyle Visible="False" Font-Size="9pt" HorizontalAlign="Left" ForeColor="#000066" BackColor="White"    Mode="NumericPages"></PagerStyle>
</aspatagrid>
执行后出现错误在 string selID=materials.DataKeys[E.Item.ItemIndex].ToString();
System.ArgumentOutOfRangeException: 索引超出范围。必须为非负值并小于集合大小。参数名: index
是何原因?
datagrid如下图:
图片如下:



蓝鲸

职务:版主
等级:5
金币:42.1
发贴:2614
#22006/1/24 23:53:22
当前页数超出了表最大页数
try
{
DataBoundGrid();
}
catch
{
DataGrid1.CurrentPageIndex --;
DataBoundGrid();
}


非常大鱼

ztong

职务:普通成员
等级:1
金币:0.0
发贴:129
#32006/1/25 17:14:52
我只是把datakeyfield="材料名称" 重新写了一遍,就行了,多奇怪亚!完全一样,没有任何变化。可是在以后的datagrid删除时又出现这样的问题了,就是说错误在行
string selID=materials.DataKeys[E.Item.ItemIndex].ToString();
错误是:
System.ArgumentOutOfRangeException: 索引超出范围。必须为非负值并小于集合大小。参数名: index
是何原因?