gridview在删除时无法获的主键的值

zz_feng 2008-09-27 10:16:57
我用gridview的控件来显示数据,在我对里面的一行数据删除的时候无法获得该行的主键值,不知是什么原因,报错是什么index范围错误,请大家给指点一下,谢谢!急用!


SqlConnection con = new SqlConnection("Data Source=ZZF;Initial Catalog=GreenWell;User ID=sa;Password=admin");
string selstr = "select * from News";
con.Open();
SqlDataAdapter sda = new SqlDataAdapter(selstr,con);
DataSet ds = new DataSet();
sda.Fill(ds);

this.GridView1.DataSource = ds.Tables[0].DefaultView;
this.GridView1.DataBind();
con.Close();


protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
string aa = this.GridView1.DataKeys[e.RowIndex].Values[0].ToString();

string str = "DELETE FROM News where NewsID="+aa;


<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" Width="800px" ShowHeader="False" OnRowDeleting="GridView1_RowDeleting" OnRowEditing="GridView1_RowEditing">
<Columns>
<asp:BoundField DataField="NewsID" Visible="False" />

<asp:TemplateField>
<ItemStyle Width="50px" />
<ItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemStyle Width="150px" />
<ItemTemplate>
<asp:Image ID="Image1" runat="server" ImageUrl='<%# Eval("picture")%>' />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="Title" HeaderText="新闻标题" />
<asp:CommandField HeaderText="编辑" ShowEditButton="True">
<ItemStyle Width="80px" />
</asp:CommandField>
<asp:CommandField HeaderText="删除" ShowDeleteButton="True">
<ItemStyle Width="80px" />
</asp:CommandField>
</Columns>
</asp:GridView>
...全文
60 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
fuzijing 2008-09-27
  • 打赏
  • 举报
回复


两个问题:
1、this.GridView1.DataKeyNames = new string[] { "aaa"};指定key


2、写错了吧LZ
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
string aa = this.GridView1.DataKeys[e.RowIndex].Values[0].ToString(); --------

string str = "DELETE FROM News where NewsID="+aa;

应该:GridView2.DataKeys[e.RowIndex].Value.ToString()
silence0716 2008-09-27
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 fang1572 的回复:]
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" Width="800px" ShowHeader="False" OnRowDeleting="GridView1_RowDeleting" OnRowEditing="GridView1_RowEditing" DataKeyNames="NewsID">
[/Quote]
正解
fang1572 2008-09-27
  • 打赏
  • 举报
回复
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" Width="800px" ShowHeader="False" OnRowDeleting="GridView1_RowDeleting" OnRowEditing="GridView1_RowEditing" DataKeyNames="NewsID">
fang1572 2008-09-27
  • 打赏
  • 举报
回复
GridView没有DataKeyNames
你的 string aa = this.GridView1.DataKeys[e.RowIndex].Values[0].ToString(); 当然会报错了
erik0930 2008-09-27
  • 打赏
  • 举报
回复
this.GridView1.DataSource = ds.Tables[0].DefaultView; 
this.GridView1.DataKeyNames = new string[] { "aaa"};
this.GridView1.DataBind();

dzjvb 2008-09-27
  • 打赏
  • 举报
回复
KeyFieldName没有指定
dzjvb 2008-09-27
  • 打赏
  • 举报
回复
KeyFieldName没有

62,046

社区成员

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

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

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

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