各位高手!帮小弟一个GridView删除的忙!太急了!谢谢~50分!

airfish108 2007-10-15 10:56:34
aspx代码
<asp:GridView ID="gvEmpInfo" runat="server">
<HeaderStyle CssClass="headcenter" BackColor="#DDE8FF" />
<Columns>
<asp:ButtonField DataTextField="TitleName" CommandName="XianShi" HeaderText="标题" />
<asp:BoundField DataField="BranchName" HeaderText="部门" />
<asp:BoundField DataField="DateTime" HeaderText="日期" />
<asp:ButtonField CommandName="Delete" Text="删除 " HeaderText="删除" />
<asp:BoundField DataField="id" Visible="False" />
<asp:BoundField DataField="BranchID" Visible="False" />
<asp:BoundField DataField ="TitleName" Visible="False" />
</Columns>
</asp:GridView>
数据已经正常绑定了!就是删除问题。我想在点击“删除”按钮时,得到id和BranchID的值!
cs代码:
protected void gvEmpInfo_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
string a = gvEmpInfo.Rows[e.RowIndex].Cells[4].Text;
string b = gvEmpInfo.Rows[e.RowIndex].Cells[5].Text;
msgError(a);
}

各位高手请帮忙啊!实在太急了!感谢之至
...全文
84 5 打赏 收藏 举报
写回复
5 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
airfish108 2007-10-15
不明白~我菜鸟一只~麻烦写个例子好吗?谢谢了!
  • 打赏
  • 举报
回复
ATCO 2007-10-15
我一般是用自定义列,然后用LINKBUTTON绑定一个参数传值来进行这个功能。
你可以试试
  • 打赏
  • 举报
回复
airfish108 2007-10-15
大哥们~帮帮忙吧!谢谢了!!我真不行了!
  • 打赏
  • 举报
回复
airfish108 2007-10-15
忘说了~cs代码这么写后,得到的值是空的!msgError是我自己的一个显示窗口~这个不用管~
高手们帮忙啊!!!谢谢了!
  • 打赏
  • 举报
回复
hs1983 2007-10-15
给你一个删除还有编辑的例子:
ASPX 代码:
<asp:GridView ID="GridView1" runat="server" AllowSorting="True"
AutoGenerateColumns="False" Width="808px" OnRowDeleting="GridView1_RowDeleting" DataKeyNames="id" OnRowCancelingEdit="GridView1_RowCancelingEdit" OnRowEditing="GridView1_RowEditing" OnRowUpdating="GridView1_RowUpdating" style="color: gray; font-family: 宋体" >
<Columns>
<asp:TemplateField HeaderText="姓名" SortExpression="frdName">
<EditItemTemplate>
<asp:TextBox ID="txtName" runat="server" Text='<%# Bind("frdName") %>' Width="45px" Font-Names="宋体" Font-Size="9pt"></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("frdName") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="电话" SortExpression="frdPhone">
<EditItemTemplate>
<asp:TextBox ID="txtPhone" runat="server" Text='<%# Bind("frdPhone") %>' Width="80px" Font-Names="宋体" Font-Size="9pt"></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label3" runat="server" Text='<%# Bind("frdPhone") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="手机" SortExpression="frdMobilePhone">
<EditItemTemplate>
<asp:TextBox ID="txtMobilePhone" runat="server" Text='<%# Bind("frdMobilePhone") %>' Width="100px" Font-Names="宋体" Font-Size="9pt"></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label4" runat="server" Text='<%# Bind("frdMobilePhone") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="地址" SortExpression="frdAddress">
<EditItemTemplate>
<asp:TextBox ID="txtAddress" runat="server" Text='<%# Bind("frdAddress") %>' Width="130px" Font-Names="宋体" Font-Size="9pt"></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label5" runat="server" Text='<%# Bind("frdAddress") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="QQ" SortExpression="frdQQ">
<EditItemTemplate>
<asp:TextBox ID="txtQQ" runat="server" Text='<%# Bind("frdQQ") %>' Width="80px" Font-Names="宋体" Font-Size="9pt"></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label6" runat="server" Text='<%# Bind("frdQQ") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="E_Mail" SortExpression="frdEmail">
<EditItemTemplate>
<asp:TextBox ID="txtEmail" runat="server" Text='<%# Bind("frdEmail") %>' Width="130px" Font-Names="宋体" Font-Size="9pt"></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label7" runat="server" Text='<%# Bind("frdEmail") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="删除" ShowHeader="False">
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False" CommandName="Delete"
Text="删除" CommandArgument='<%# Eval("id") %>' OnClientClick='return confirm("确定要删除吗?")' OnClick="LinkButton1_Click"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:CommandField HeaderText="编辑" ShowEditButton="True" />
</Columns>
<HeaderStyle BackColor="#FFC0C0" Font-Bold=True ForeColor=White />
<EditRowStyle Width="10px" />
</asp:GridView>

ASPX.CS 代码:
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
string SqlStr = "delete from AddressLists where id=@id" ;

try
{
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["pubsConn"].ConnectionString);
if (conn.State ==ConnectionState.Closed)
conn.Open();
SqlCommand comm = new SqlCommand(SqlStr, conn);
comm.Parameters.Add("@id",SqlDbType.Int);
comm.Parameters["@id"].Value = GridView1.DataKeys[e.RowIndex]["id"].ToString();
comm.ExecuteNonQuery();
comm.Dispose();
if (conn.State.ToString() == "Open")
conn.Close();

GridView1.EditIndex = -1;
ds_bind();
}
catch (Exception ex)
{
Response.Write("数据库错误,错误原因:" + ex.Message);
ds_bind();
}
}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex; //GridView编辑项索引等于单击行的索引
ds_bind();

}
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
ds_bind();

}
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
string str_update = "update AddressLists SET frdName=@frdName,frdPhone=@frdPhone,frdMobilePhone=@frdMobilePhone,frdQQ=@frdQQ ,frdAddress=@frdAddress ,frdEmail=@frdEmail WHERE id=@id";
SqlConnection sqlconn = new SqlConnection(ConfigurationManager.ConnectionStrings["pubsConn"].ConnectionString);
SqlCommand cmd = new SqlCommand(str_update, sqlconn);
cmd.CommandType = CommandType.Text;

cmd.Parameters.Add("@id", SqlDbType.Int);
cmd.Parameters.Add("@frdName", SqlDbType.VarChar);
cmd.Parameters.Add("@frdPhone", SqlDbType.VarChar);
cmd.Parameters.Add("@frdMobilePhone", SqlDbType.VarChar);
cmd.Parameters.Add("@frdAddress", SqlDbType.VarChar);
cmd.Parameters.Add("@frdQQ", SqlDbType.VarChar);
cmd.Parameters.Add("@frdEmail", SqlDbType.VarChar);
cmd.Parameters["@id"].Value =this.GridView1.DataKeys[e.RowIndex]["id"].ToString();
cmd.Parameters["@frdName"].Value = (((TextBox)GridView1.Rows[e.RowIndex].Cells[1].FindControl("txtName")).Text).ToString();
cmd.Parameters["@frdPhone"].Value = (((TextBox)GridView1.Rows[e.RowIndex].Cells[2].FindControl("txtPhone")).Text).ToString();
cmd.Parameters["@frdMobilePhone"].Value = (((TextBox)GridView1.Rows[e.RowIndex].Cells[3].FindControl("txtMobilePhone")).Text).ToString();
cmd.Parameters["@frdAddress"].Value = (((TextBox)GridView1.Rows[e.RowIndex].Cells[4].FindControl("txtAddress")).Text).ToString();
cmd.Parameters["@frdQQ"].Value = (((TextBox)GridView1.Rows[e.RowIndex].Cells[5].FindControl("txtQQ")).Text).ToString();
cmd.Parameters["@frdEmail"].Value = (((TextBox)GridView1.Rows[e.RowIndex].Cells[6].FindControl("txtEmail")).Text).ToString();

sqlconn.Open();

try
{
cmd.ExecuteNonQuery();
this.GridView1.EditIndex = -1;
ds_bind();
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
finally
{
if (sqlconn.State == ConnectionState.Open)
sqlconn.Close();
}

}
  • 打赏
  • 举报
回复
发帖
C#

10.8w+

社区成员

.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
帖子事件
创建了帖子
2007-10-15 10:56
社区公告

让您成为最强悍的C#开发者