这是gridview中删除代码,现在的问题是红线部分取不到值,请问问大家,选择要进行删除操作的这一行时,第一元素jh值已经取到了,sgmc和fssj应该怎么取呢?笨猪难住了!protected void accidentInfo_RowDeleting(object sender, GridViewDeleteEventArgs e)
{ string jh = accidentInfo.DataKeys[e.RowIndex].Values[0].ToString();
string sgmc = accidentInfo.Rows[e.RowIndex].Cells[1].Text;
string fssj = accidentInfo.Rows[e.RowIndex].Cells[2].Text;
string SqlStr = "delete from GCSG where filename='" +fssj+jh +sgmc+ "'";
try
{
OracleConnection conn = DB.createConnection();
if (conn.State.ToString() == "Closed") conn.Open();
OracleCommand comm = new OracleCommand(SqlStr, conn);
OracleDataReader r = comm.ExecuteReader();
r.Read();
if (r.HasRows)
{ Response.Write("<script language =javascript>window.alert('删除成功!');</script>");
} if (conn.State.ToString() == "Open") conn.Close(); accidentInfo.EditIndex = -1;
gridViewDataBind();
Response.Redirect("../accidentQuery/jingHao_Query.aspx"); }
catch (Exception ex)
{
Response.Write("数据库错误,错误原因:" + ex.Message);
Response.End();
}
}
{ string jh = accidentInfo.DataKeys[e.RowIndex].Values[0].ToString();
string sgmc = accidentInfo.Rows[e.RowIndex].Cells[1].Text;
string fssj = accidentInfo.Rows[e.RowIndex].Cells[2].Text;
string SqlStr = "delete from GCSG where filename='" +fssj+jh +sgmc+ "'";
try
{
OracleConnection conn = DB.createConnection();
if (conn.State.ToString() == "Closed") conn.Open();
OracleCommand comm = new OracleCommand(SqlStr, conn);
OracleDataReader r = comm.ExecuteReader();
r.Read();
if (r.HasRows)
{ Response.Write("<script language =javascript>window.alert('删除成功!');</script>");
} if (conn.State.ToString() == "Open") conn.Close(); accidentInfo.EditIndex = -1;
gridViewDataBind();
Response.Redirect("../accidentQuery/jingHao_Query.aspx"); }
catch (Exception ex)
{
Response.Write("数据库错误,错误原因:" + ex.Message);
Response.End();
}
}
string fssj = accidentInfo.Rows[e.RowIndex].Cells[2].Text;
貌似代码没错
你SQL delete 什么了?
http://blog.csdn.net/insus/archive/2008/03/30/2229898.aspx另外,如果是DataList:
http://www.cnblogs.com/insus/articles/1443147.html
做好的辦法是將你的Cell【1】,Cell[2]的值輸出來,看是否取到值,取值正確的話檢查SqL羅。
<asp:LinkButton ID="DeleteTask" runat="server" CommandName="delete" CommandArgument='<%# Eval("ID")%>' Text="删除" OnClientClick="return confirm('确定要删除该任务信息吗?')" />
后台在ItemCommand中写方法protected void repReport_ItemCommand(object source, RepeaterCommandEventArgs e)
{
string ID = e.CommandArgument.ToString(); if (e.CommandName == "delete")
{
del()//你的删除方法,上边的ID是删除数据的ID;
}
Bind();
}
注意:我这个是在Repeater中实现的很简单。
<asp:Repeater ID="repReport" runat="server" OnItemCommand="repReport_ItemCommand">
<HeaderTemplate>
<tr class="title">
<td width="1%">
组ID
</td>
<td width="2%">
组中文描述
</td>
<td width="1%">
操作
</td>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr class="tdbg" onmouseover="this.className='tdbgmouseover'" onmouseout="this.className='tdbg'">
<td>
<%# Eval("GroupId")%>
</td>
<td>
<%# Eval("GroupExp")%>
</td>
<td>
<asp:LinkButton ID="DeleteTask" runat="server" CommandName="delete" CommandArgument='<%# Eval("OpName")%>'
Text="删除" OnClientClick="return confirm('确定要删除该任务信息吗?')" />
</td>
</tr>
</ItemTemplate>
</asp:Repeater>
<asp:Repeater ID="repReport" runat="server" OnItemCommand="repReport_ItemCommand">
<HeaderTemplate>
<tr class="title">
<td width="1%">
组ID
</td>
<td width="2%">
组中文描述
</td>
<td width="1%">
操作
</td>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr class="tdbg" onmouseover="this.className='tdbgmouseover'" onmouseout="this.className='tdbg'">
<td>
<%# Eval("GroupId")%>
</td>
<td>
<%# Eval("GroupExp")%>
</td>
<td>
<asp:LinkButton ID="DeleteTask" runat="server" CommandName="delete" CommandArgument='<%# Eval("OpName")%>'
Text="删除" OnClientClick="return confirm('确定要删除该任务信息吗?')" />
</td>
</tr>
</ItemTemplate>
</asp:Repeater>
</table>
string fssj = accidentInfo.Rows[e.RowIndex].Cells[2].Text; 先將sgmc,fssj Response.Write()出來
如果有值,就應該是sql語句有問題強烈建議
string sgmc = accidentInfo.Rows[e.RowIndex].Cells[1].Text.Trim();
string fssj = accidentInfo.Rows[e.RowIndex].Cells[2].Text.Trim();
先定义要相加的值,然后在写SQL语句!
是不是加了什么东西在模版列里面?
若用模版列OnRowDataBond()绑定 个人感觉你的取值方法不正确 尝试:
string sgmc = accidentInfo.Rows[e.RowIndex]。FindControl(“控件名”).Text。tostring()
2. string SqlStr = "delete from GCSG where filename='" +fssj+jh +sgmc+ "'";看着怪怪的 尝试下这样写试试:
string abc=fss+jh+sgmc;
string SqlStr = "delete from GCSG where filename='" + abc + "'" ;
暂时只想到这么多
protected void gvAfterService_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
int id = int.Parse(gvAfterService.DataKeys[e.RowIndex].Value.ToString());
string result = after.AfterServiceDel(id);
ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('" + result + "');</script>");
Bind();
}
//全选删除
protected void lbtDel_Click(object sender, EventArgs e)
{
string result = "";
foreach (GridViewRow row in gvAfterService.Rows)
{
if ((row.FindControl("CheckBox1") as CheckBox).Checked)
{
int id = int.Parse(gvAfterService.DataKeys[row.RowIndex].Value.ToString());
result = after.AfterServiceDel(id);
}
}
ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('" + result + "');</script>");
Bind();
}
判断是否为数据行
if (e.Row.RowType==DataControlRowType.DataRow)
{
string jh = accidentInfo.DataKeys[e.RowIndex].Values[0].ToString();
string sgmc = accidentInfo.Rows[e.RowIndex].Cells[1].Text;
string fssj = accidentInfo.Rows[e.RowIndex].Cells[2].Text;
string SqlStr = "delete from GCSG where filename='" +fssj+jh +sgmc+ "'";
try
{
OracleConnection conn = DB.createConnection();
if (conn.State.ToString() == "Closed") conn.Open();
OracleCommand comm = new OracleCommand(SqlStr, conn);
OracleDataReader r = comm.ExecuteReader();
r.Read();
if (r.HasRows)
{ Response.Write(" <script language =javascript>window.alert('删除成功!'); </script>");
} if (conn.State.ToString() == "Open") conn.Close(); accidentInfo.EditIndex = -1;
gridViewDataBind();
Response.Redirect("../accidentQuery/jingHao_Query.aspx");
}
catch (Exception ex)
{
Response.Write("数据库错误,错误原因:" + ex.Message);
Response.End();
} }
string jh = accidentInfo.DataKeys[e.RowIndex].Values[0].ToString(); 这个不变
下面的两个取值把
string sgmc = accidentInfo.Rows[e.RowIndex].Cells[1].Text;
string fssj = accidentInfo.Rows[e.RowIndex].Cells[2].Text;
变成
string sgmc = accidentInfo.DataKeys[e.RowIndex].Values[1].ToString();
string fssj = accidentInfo.DataKeys[e.RowIndex].Values[2].ToString();
然后再 前台gridview的datakeys中增加 sgmc和fssj