回 帖 发 新 帖 刷新版面

主题:datagrid删除问题???

我在程序中有以下代码:
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如下图:

checkbox1   材料名称  收件人  收件日期  交件人 页数  份数  编辑  删除
其中编辑和删除是linkbutton.

回复列表 (共10个回复)

沙发

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);

LinkButton tmp = (LinkButton)e.Item.Cells[8].Controls[0];
tmp.Attributes.Add("onclick","return confirm('确认要删除该条记录吗?');";
}
}

public void materials_DeleteCommand(object O, DataGridCommandEventArgs E)
{
if(e.Item.ItemType==ListItemType.Item||e.Item.ItemType==ListItemType.AlternatingItem)
{

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();
}
}
这样看看

板凳

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

3 楼

public void materials_DeleteCommand(object O, DataGridCommandEventArgs E)
{
}
错了吧,是这样的吗?
private void DataGrid1_DeleteCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
if(e.Item.ItemType==ListItemType.Item||e.Item.ItemType==ListItemType.AlternatingItem)
{
string selID=materials.DataKeys[e.Item.ItemIndex].ToString();
}
}
这样的吧
if(e.Item.ItemType==ListItemType.Item||e.Item.ItemType==ListItemType.AlternatingItem)
{
}
这个要加上去的
因为第一次加的是头
或者
DataKeys[e.Item.ItemIndex]

DataKeys[e.Item.ItemIndex+1]
呵呵!具体的我没数据源,我不能帮你测试
不好意思
我写个用这样的类试的方法做的给你

4 楼

发现问题了,如果是删除最后一行,就出现上述错误,如果删除中间行,就删除两行,很奇怪!,怎么改呢?我加一个全局变量,i,如果i=1就运行此过程,如果i〉1就不运行,倒是达到目的了,但是感觉不是解决的办法,为什么只点击一次却运行两次呢?

5 楼

页面文件
<asp:button id="Button1" style="Z-INDEX: 101; LEFT: 408px; POSITION: absolute; TOP: 224px" runat="server"
                    Text="选中并删除"></asp:button><asp:datagrid id="DataGrid1" style="Z-INDEX: 102; LEFT: 272px; POSITION: absolute; TOP: 272px"
                    runat="server" AutoGenerateColumns="False" Width="376px" DataKeyField="LastName">
                    <Columns>
                        <asp:TemplateColumn HeaderText="rtrtrt">
                            <ItemTemplate>
                                <asp:CheckBox Runat="server" ID="ck"></asp:CheckBox>
                            </ItemTemplate>
                        </asp:TemplateColumn>
                        <asp:BoundColumn DataField="LastName" HeaderText="vvvvv"></asp:BoundColumn>
                        <asp:BoundColumn HeaderText="455545"></asp:BoundColumn>
                        <asp:ButtonColumn Text="删除" CommandName="Delete"></asp:ButtonColumn>
                    </Columns>
                </asp:datagrid>

页面加载的:
if(!IsPostBack)
            {
                                DataTable tbl = CreateDataSource();
                this.DataGrid1.DataSource = tbl;
                this.DataGrid1.DataBind();
                ViewState["tbl"] = tbl;

                
            }
public DataTable CreateDataSource()
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("LastName", Type.GetType("System.String"));
            dt.Rows.Add(new object[] {"Smith"});
            dt.Rows.Add(new object[] {"Jones"});
            dt.Rows.Add(new object[] {"Clark"});
            dt.Rows.Add(new object[] {"Clark1"});
            dt.Rows.Add(new object[] {"Jones1"});
            dt.Rows.Add(new object[] {"S1mith1"});
            return dt;
        }
private void DataGrid1_DeleteCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
        {
            DataColumn col = (ViewState["tbl"] as DataTable).Columns["LastName"];
            DataTable tbl = ViewState["tbl"] as DataTable;
            tbl.PrimaryKey = new DataColumn[]{col};
            if(e.Item.ItemType==ListItemType.Item||e.Item.ItemType==ListItemType.AlternatingItem)
            {
                string selID=this.DataGrid1.DataKeys[e.Item.ItemIndex].ToString();
                tbl.Rows.Find(selID).Delete();
            }
            this.DataGrid1.DataSource=tbl;
            this.DataGrid1.DataBind();
        }
没有数据库,自己写了个数据表做数据源
点删除就可以删除里面的行了
相信你也是为了这个目的

6 楼

DataGrid的关键列是"LastName"

7 楼

我知道你的程序可以执行。可是我的错误原因是什么呢?为什么执行两次呢?
我只点击一次呀??
大侠帮忙分析一下!!谢谢了!

8 楼

你要看数据库里的数据,被你定义的关键列里面有重复一样的数据吗?

9 楼

没有重复数据呀,关键列是唯一值,并不重复,里面的值是唯一的,怎么回事?

10 楼

这个可能你要单步执行,看到底是什么原因,这样我也说不清楚

我来回复

您尚未登录,请登录后再回复。点此登录或注册