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

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何获取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;

<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();




大概代码 但是 我用的三层架构 不知道 你看得懂不
参考技术A 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作为参数传给删除的方法

参考技术B GridView.Rows[i].Cells[j].Text

获得i行j列的值本回答被提问者和网友采纳
参考技术C 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 );

python怎么实现修改文本中某行某列的数值为固定值

具体就是一个由两列数组成的文件bowl_profile_1,我想把第一行的第一列数字改为固定值0,这个怎么用python实现

如果你两列数据中间是用tab键来分割,两个数据都是str类型的

with open("bowl_profile_1") as f:
    data = f.readlines()            # 读入数据
out = ["%d\\t%s"%(0,data[i].split('\\t')[-1]) for i in range(len(data))]   # 修改数据
with oepn(path) as f: # 写入数据,path是新文件的路径
    for i in out:
        f.write(i)

参考技术A

简单,只需要三行代码


import numpy as np
a=np.loadtxt('bowl_profile_1')
a[:,0]=0


欢迎加入我的课程用python做些事,和大家一起学吧。

以上是关于如何获取GridView中某行某列的值的主要内容,如果未能解决你的问题,请参考以下文章

页面载入时通过获取GridView某行某列的值来控制某一列的控件属性

获取DataSet中某行某列的数据

如何从C#中获取ListView中选中某一行某一列的值

在java中创建了jtable,怎么获取这个表中某行某列的数据(即某单元格的数据int值)??

如何获取gridview单元格的值

在wpf中怎么获取datagrid某行某列的值