如何获取GridView中某行某列的值

是这样的,我页面里面的GridView,显示的每行记录后面都有一个【删除】和【编辑】的按钮,当按了它们后就会触发事件我需要获取到用户按【删除】后,所在的那一行中【单价】这... 是这样的,我页面里面的GridView,显示的每行记录后面都有一个【删除】和【编辑】的按钮,当按了它们后就会触发事件
我需要获取到用户按【删除】后,所在的那一行中【单价】这一列的值是多少
应该如何获取?麻烦解答,谢谢~
补充一下我的代码,这是页面的:
<asp:GridView ID="GridView1" runat="server" Width="710px" AutoGenerateColumns="False" DataSourceID="ObjectDataSource1" AllowPaging="True" OnRowDeleted="GridView1_RowDeleted">
<Columns>
。。。。。。。。。。
<asp:ButtonField ButtonType="Button" CommandName="Delete" HeaderText="删除" ShowHeader="True" Text="删除" />
</Columns>
</asp:GridView>
这是后台:
protected void GridView1_RowDeleted(object sender, GridViewDeletedEventArgs e)
{
string price = ((Label)GridView1.Rows[e.RowIndex].FindControl("labNum")).Text;
this.Label4.Text = price;
}
展开
 我来答
549265480
2009-06-03 · 超过63用户采纳过TA的回答
知道小有建树答主
回答量:286
采纳率:0%
帮助的人:281万
展开全部
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div align="center">

<table style="width: 81%">
<tr><td align="center" bgcolor="#99FF99">
<asp:Label ID="Label1" runat="server" Font-Bold="True" Font-Names="华文隶书"
Font-Size="15pt" Text="新闻类型维护"></asp:Label>
</td></tr>
<tr><td align="left">  新闻类型:     <asp:TextBox ID="txtName" runat="server" Width="319px"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="添加" onclick="Button1_Click"
Width="67px" />
</td></tr>
<tr><td align="left">
                                           
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
CellPadding="4" ForeColor="#333333" GridLines="None" Width="566px"
onrowdeleting="GridView1_RowDeleting"
onrowcancelingedit="GridView1_RowCancelingEdit"
onrowediting="GridView1_RowEditing" onrowupdating="GridView1_RowUpdating"
>
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<Columns>
<asp:TemplateField HeaderText="删除" >
<ItemTemplate>
<asp:CheckBox ID="ckDelete" runat="server" />
</ItemTemplate>
<HeaderTemplate >
<asp:CheckBox ID="ckDleteAll" runat="server" AutoPostBack="True"
oncheckedchanged="ckDleteAll_CheckedChanged" />
 删除全部
</HeaderTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="新闻ID">
<ItemTemplate>

<asp:Label ID="txtID" runat="server" Text='<%#Eval("newsTypeID") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="新闻名">
<ItemTemplate>
<asp:TextBox ID="txtName" runat="server" Text='<%#Eval("newsTypeName") %>'></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:CommandField ShowEditButton="true" HeaderText="编辑" />
<asp:CommandField ShowDeleteButton="true" HeaderText="删除" />
</Columns>
<PagerStyle BackColor="#284775" ForeColor="White"
HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#E2DED6" ForeColor="#333333" Font-Bold="True" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<EditRowStyle BackColor="#999999" />
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
</asp:GridView>
</td></tr>

<tr><td align="left">
<asp:Button ID="Button2" runat="server" Text="删除选定项" onclick="Button2_Click" />
</td></tr>
</table>
</div>

</form>
</body>
</html>

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Text;

using User6Entity;
using User6Operate;

public partial class newstype : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
bind();

}
}

private void bind()
{

NewsType Wl=new NewsType();
GridView1.DataSource = Wl.DataSetSql("select newsTypeID,newsTypeName from [newsType]");
GridView1.DataBind();

}

protected void Button1_Click(object sender, EventArgs e)
{
NewsType Wl = new NewsType();
NewstypeEntity en = new NewstypeEntity();
string id;
id = Wl.ExecuteValue("select count(*) from [newsType] ");
id = txtName.Text +"_"+ DateTime.Now.Second.ToString() +"_" + id;
en.NewsTypeID = id;
en.NewsTypeName = txtName.Text;
if (!Wl.AddNewsType(en))
Response.Write(Wl.ShowMessage("添加失败"));
bind();
}

protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{

NewsType Wl = new NewsType();
NewstypeEntity en = new NewstypeEntity();
Label txtid = (Label)GridView1.Rows[e.RowIndex].Cells[1].FindControl("txtID");
string id = txtid.Text;
en.NewsTypeID = id;
if(!Wl.DeleteNewsType(en))
Response.Write(Wl.ShowMessage("删除失败"));
bind();

}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
bind();
}
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
bind();
}
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
NewsType Wl = new NewsType();
NewstypeEntity en = new NewstypeEntity();
string id;
//id =Wl .ExecuteValue ("select count(*) from newsType");
Label txtid = (Label)GridView1.Rows[e.RowIndex].Cells[1].FindControl("txtID");
TextBox txtname=(TextBox)GridView1.Rows[e.RowIndex].Cells[2].FindControl("txtName");
//en.NewsTypeID = txtid.Text+"_"+DateTime .Now .Second +"_"+id ;
en.NewsTypeID = txtid.Text;
en.NewsTypeName = txtname.Text;
if (!Wl.UpdateNewsType(en))

Response.Write(Wl.ShowMessage("更新失败"));
GridView1.EditIndex = -1;

bind();

}
protected void ckDleteAll_CheckedChanged(object sender, EventArgs e)
{

CheckBox cbdeleteall = (CheckBox)sender;
if (cbdeleteall.Checked)
{
for (int i = 0; i < GridView1.Rows.Count; i++)
{
CheckBox cbdelete = (CheckBox)GridView1.Rows[i].FindControl("ckDelete");
if (cbdelete != null && !cbdelete.Checked)
{
cbdelete.Checked = true;
}
}
}
else
{

for (int i = 0; i < GridView1.Rows.Count; i++)
{
CheckBox cbdelete = (CheckBox)GridView1.Rows[i].FindControl("ckDelete");
if (cbdelete != null && cbdelete.Checked)
{
cbdelete.Checked = false;
}
}
}
}

protected void Button2_Click(object sender, EventArgs e)
{
NewsType Wl = new NewsType();
NewstypeEntity en = new NewstypeEntity();
for(int i=0;i<GridView1.Rows.Count;i++)
{
CheckBox ckdelete = (CheckBox)GridView1.Rows[i].FindControl("ckDelete");
if(ckdelete!=null&&ckdelete.Checked)
{
Label lbid=(Label)GridView1.Rows[i].Cells[1].FindControl("txtID");
string id = lbid.Text;
en.NewsTypeID=id;
if(!Wl.DeleteNewsType(en))
Response.Write(Wl.ShowMessage("删除失败"));
}
}
bind();

}

}
大概代码 但是 我用的三层架构 不知道 你看得懂不
as...2@163.com
2009-06-03 · TA获得超过550个赞
知道小有建树答主
回答量:594
采纳率:0%
帮助的人:389万
展开全部
string price = ((Label)GridView1.Rows[e.RowIndex].FindControl("lblprice")).Text;

lblprice是你前台页面绑定“单价”Label控件的id

<asp:TemplateField HeaderText="单价">
<ItemTemplate>
<asp:Label ID="lblprice" runat="server" Text='<%# Eval("price") %>'></asp:Label>
</ItemTemplate>

</asp:TemplateField>
*******************************************************************
你的时间激活错了,你激活了RowDeleted删除后的事件,应该是RowDeleting事件
GridView的属性里有个DataKeyNames属性,将它绑定表里的主键列
然后激活
RowDeleting事件

protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
string id= GridView1.DataKeys[e.RowIndex].Value.ToString();
Response.Write ( id);
//把获取的id作为参数传给删除的方法

}
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
侯湛恩Pf
推荐于2018-04-13 · TA获得超过407个赞
知道小有建树答主
回答量:195
采纳率:0%
帮助的人:0
展开全部
GridView.Rows[i].Cells[j].Text

获得i行j列的值
本回答被提问者和网友采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
kid83
2009-06-03 · TA获得超过2081个赞
知道大有可为答主
回答量:1818
采纳率:0%
帮助的人:1987万
展开全部
protected void GridView1_RowDeleting ( object sender, GridViewDeleteEventArgs e )
{
GridView grdProducts = sender as GridView;
string s = grdProducts.Rows [ e.RowIndex ].Cells [ 1 ].Text;

Response.Write ( s );
}
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 更多回答(2)
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式