gridview 中设置的删除 用代码如何实现,总出错

haiya_pl 2007-07-13 07:57:32
控件设置的删除连接,代码如果自己写的话,该怎么写?
如果光设置出来的话,提示“ASP.studentsinfor_aspx”并不包含“GridView1_RowDeleting”的定义
我想代码应该在GridView1_RowDeleting中写吧,可具体代码该怎么写,请指教
...全文
430 20 打赏 收藏 转发到动态 举报
写回复
用AI写文章
20 条回复
切换为时间正序
请发表友善的回复…
发表回复
viaivi 2007-07-13
  • 打赏
  • 举报
回复
建议你看看这个
http://blog.csdn.net/viaivi/archive/2007/06/19/1658254.aspx
所有事件的完整代码都有
viaivi 2007-07-13
  • 打赏
  • 举报
回复
我的没问题哈
haiya_pl 2007-07-13
  • 打赏
  • 举报
回复
我快疯掉了!还没有弄明白!
Jinglecat(晓风残月 >> 问题需简洁,错误要详细) ( ) 的方法我试了,到我这里怎么就查不出数据呢,没有数据显示出来

amandag(高歌) ( ) 的方法在我这里也不好使,真不知道是怎么了,我快疯了!!
viaivi 2007-07-13
  • 打赏
  • 举报
回复
#region 删除
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e){

string strSql = "delete from News where newId= @id";// +GridView1.DataKeys[e.RowIndex].Value.ToString() + "'";
SqlParameter parms = new SqlParameter("@id", SqlDbType.Int);
parms.Value = GridView1.DataKeys[e.RowIndex].Value.ToString();
int val = SqlHelper.ExecuteNonQuery(CommandType.Text, strSql, parms);
if (val > 0)
RegisterClientScriptBlock("", "<script laguage='javascrt'>alert('删除成功');</script>");
else
RegisterClientScriptBlock("", "<script laguage='javascrt'>alert('删除时发生错误');</script>");
binds();
}
#endregion
haiya_pl 2007-07-13
  • 打赏
  • 举报
回复
// .aspx 在 datakeynames 最终设置主键字段
<asp:GridView ID="GridView1" runat="server" OnRowDeleting="GridView1_RowDeleting" datakeynames="ID">
<Columns>
<asp:CommandField ShowDeleteButton="True" />
</Columns>
</asp:GridView>

// aspx.cs
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
string id = GridView1.DataKeys[e.RowIndex].Value.ToString(); // 获取主键
string sql = "DELETE FROM table WHERE id = " + id;
// ADO.NET 操作
// ..
}

---------------------------------------------------------------------------------
报错了 System.ArgumentOutOfRangeException: 索引超出范围。必须为非负值并小于集合大小。
就是获取主键那句
amandag 2007-07-13
  • 打赏
  • 举报
回复
//aspx
<asp:GridView ID="GridView1" runat="server" DataKeyNames="au_id" AutoGenerateColumns="False" OnRowDeleting="GridView1_RowDeleting" >
<Columns>
<asp:CommandField ShowDeleteButton="True" />
<asp:TemplateField>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Eval("au_id") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>

//aspx.cs
private void BindGrid()
{
SqlConnection cn = new SqlConnection(@"server=.\SQLExpress;uid=sa;pwd=sa;database=pubs");
string strSQL = "select * from authors";
SqlCommand cmd = new SqlCommand(strSQL, cn);
cn.Open();
GridView1.DataSource = cmd.ExecuteReader();
GridView1.DataBind();
cn.Close();
}

private void Page_Load(object sender, System.EventArgs e)
{
if (! IsPostBack )
{
BindGrid();
}
}

protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
SqlConnection cn = new SqlConnection(@"server=localhost;uid=sa;pwd=sa;database=pubs");
string strSQL = "delete from authors where au_id = @au_id";
SqlCommand cmd = new SqlCommand(strSQL, cn);
cmd.Parameters.Add("@au_id", SqlDbType.VarChar).Value = GridView1.DataKeys[e.RowIndex].Value.ToString();
cn.Open();
cmd.ExecuteNonQuery();
cn.Close();
BindGrid();
}
haiya_pl 2007-07-13
  • 打赏
  • 举报
回复
就是绑定到你gridview的数据库表要有主键,主键在建表的时候设置啊
---------------------
sorry,我的意思是获取
ssy888 2007-07-13
  • 打赏
  • 举报
回复
三块,1。添加 2。编辑 3。删除
haiya_pl 2007-07-13
  • 打赏
  • 举报
回复
不了解你说的情况,我只知道要有主键才可以删除
-----------------------------------------------
我的意思就是自己写删除代码,可我写了半天也没写出来,主键该怎么设置呢,有了主键就好些多了吧
cxy0303 2007-07-13
  • 打赏
  • 举报
回复
就是绑定到你gridview的数据库表要有主键,主键在建表的时候设置啊
Jinglecat 2007-07-13
  • 打赏
  • 举报
回复
// .aspx 在 datakeynames 最终设置主键字段
<asp:GridView ID="GridView1" runat="server" OnRowDeleting="GridView1_RowDeleting" datakeynames="ID">
<Columns>
<asp:CommandField ShowDeleteButton="True" />
</Columns>
</asp:GridView>

// aspx.cs
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
string id = GridView1.DataKeys[e.RowIndex].Value.ToString(); // 获取主键
string sql = "DELETE FROM table WHERE id = " + id;
// ADO.NET 操作
// ..
}
ssy888 2007-07-13
  • 打赏
  • 举报
回复
贴点1.1的代码,自己研究吧
ssy888 2007-07-13
  • 打赏
  • 举报
回复
<%@ Page language="C#" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.OleDb" %>

<script runat="server">
void Page_Load(Object sender, EventArgs e)
{
string connectionString;
string queryString;
DataSet data = new DataSet();
OleDbConnection dbConnection;
OleDbDataAdapter dataAdapter;
connectionString = "Provider=Microsoft.Jet.OLEDB.4.0; " +
"Data Source=C:\\BegASPNet11\\data\\Northwind.mdb";
queryString = "SELECT FirstName, LastName FROM Employees";
dbConnection = new OleDbConnection(connectionString);
dataAdapter = new OleDbDataAdapter(queryString, dbConnection);
dataAdapter.Fill(data, "Employees");
DataGrid1.DataSource = data;
DataGrid1.DataBind();

// ---------------------------------------------------------
DataTable table;
DataRow newRow;
table = data.Tables["Employees"];
newRow = table.NewRow();
newRow["FirstName"] = "Norman";
newRow["LastName"] = "Blake";
table.Rows.Add(newRow);
// bind the second grid to the new data
DataGrid2.DataSource = table;

// ---------------------------------------------------------
DataRow[] selectedRows;
// find the row to change
selectedRows = table.Select("FirstName='Margaret' AND
LastName='Peacock'");
selectedRows[0]["FirstName"] = "John";
selectedRows[0]["LastName"] = "Hartford";
// bind the third grid to this new data
DataGrid3.DataSource = table;
DataGrid3.DataBind();


// ---------------------------------------------------------
// The Rows collection is 0 indexed, therefore
// this deletes the sixth row
table.Rows[5].Delete();
// bind the fourth grid to the new data
DataGrid4.DataSource = table;
DataGrid4.DataBind();
}
</script>


<html>
<body>
<table width="100%">
<tr>
<td>Original Data</td>
<td>Data with new Row</td>
<td>Data with edited Row</td>
<td>Data with deleted Row</td>
</tr>
<tr>
<td valign="top"><asp:DataGrid id="DataGrid1" runat="server" /></td>
<td valign="top"><asp:DataGrid id="DataGrid2" runat="server" /></td>
<td valign="top"><asp:DataGrid id="DataGrid3" runat="server" /></td>
<td valign="top"><asp:DataGrid id="DataGrid4" runat="server" /></td>
</tr>
</table>
</body>
</html>
haiya_pl 2007-07-13
  • 打赏
  • 举报
回复
不对是我的页面,呵呵
haiya_pl 2007-07-13
  • 打赏
  • 举报
回复
ASP.studentsinfor_aspx
这是什么东西啊
-------------------------------
我的数据库
haiya_pl 2007-07-13
  • 打赏
  • 举报
回复
可是主健怎么设置呢,请楼上赐教!
zss1100 2007-07-13
  • 打赏
  • 举报
回复
ASP.studentsinfor_aspx
这是什么东西啊
cxy0303 2007-07-13
  • 打赏
  • 举报
回复
不了解你说的情况,我只知道要有主键才可以删除
haiya_pl 2007-07-13
  • 打赏
  • 举报
回复
忘了说了是c#代码,真的没有人解答吗,伤心啊~这个问题困扰很长时间了!
haiya_pl 2007-07-13
  • 打赏
  • 举报
回复
急啊,没有人解答吗,在线等

62,046

社区成员

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

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

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

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