取Gridview的当前行中为什么数据是""的?

lqhly 2010-01-12 11:57:42
系统:sql2005+vs2008.
在一个Gridview中要编辑数据,前主要台位代码:

<asp:CommandField HeaderText="编辑" EditText="编辑" UpdateText="更新" CancelText="取消" ShowEditButton="True" />
<asp:CommandField HeaderText="删除" DeleteText="刪除" ShowDeleteButton="True" />
我需要在删除事件中先选出该行的各字段的内容,然后再决定是否要删除,代码如下:
protected void GridView_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
s_1 = this.GridView.Rows[e.RowIndex].Cells[0].Text;
s_2 = this.GridView.Rows[e.RowIndex].Cells[1].Text;
s_3 = this.GridView.Rows[e.RowIndex].Cells[2].Text;
}
我调试跟踪时发现e.RowIndex是正确的当前行号,但该行的各个字段的内容都是空的,即s_1="",s_2="",s_3=""
这是为什么?如何才能提出正确的数据?
...全文
120 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
wuyq11 2010-01-12
  • 打赏
  • 举报
回复
gv.DataKeys[e.RowIndex].Value.ToString()
用模版列FindControl
paulgerchen 2010-01-12
  • 打赏
  • 举报
回复
Page_load
!IsPostBack
mngzilin 2010-01-12
  • 打赏
  • 举报
回复
就这么点代码?
dapsxianfeng2010 2010-01-12
  • 打赏
  • 举报
回复
bs的是有FindControl这么一说
koukoujiayi 2010-01-12
  • 打赏
  • 举报
回复
你的是模板列吧!!
模板列用FindContrl如:
string a = ((Label)GridView1.Rows[e.RowIndex].Cells[0].FindControl("Label1")).Text;
yg15hxj 2010-01-12
  • 打赏
  • 举报
回复
问题的原因前面的几位已经回答楼主了
lqhly 2010-01-12
  • 打赏
  • 举报
回复
前台主要代码
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
bind();
}
}
public void bind()
{SqlConnection Con = new SqlConnection(ConfigurationManager.AppSettings["conStr"]);
Con.Open();
cmdtxt = "select T_PO.LNID AS LNID,T_PO.AITM AS AITM,T_PO.UORG AS UORG, T_SPDA.UPRC AS UPRC,T_UOM.NAME AS UOM,T_SPDA.Category AS Category"
+ " from T_PO ,T_SPDA,T_UOM "
+ " where T_PO.TRDJNO = '" + Request.QueryString["TRDJNO"] + "'"
+ " and T_PO.AITM = T_SPDA.AITM and T_SPDA.UOM = T_UOM.UOM"
+ " order by T_PO.LNID";
SqlDataAdapter Da = new SqlDataAdapter(cmdtxt, Con);
DataSet ds = new DataSet();
Da.Fill(ds, "T_PO");
Con.Close();
this.GridView.DataSource = ds;
GridView.DataKeyNames = new string[] { "AITM" }; //------主键
this.GridView.DataBind();
}

protected void GridView_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
s_AITM = GridView.DataKeys[e.RowIndex].Value.ToString();
s_1 = this.GridView.Rows[e.RowIndex].Cells[0].Text;
s_2 = this.GridView.Rows[e.RowIndex].Cells[1].Text;
s_3 = this.GridView.Rows[e.RowIndex].Cells[2].Text;
}
maomao90 2010-01-12
  • 打赏
  • 举报
回复
同意一楼的
v_chensheng 2010-01-12
  • 打赏
  • 举报
回复
LZ``你还是把整个GridView的前台代码给大家看看``
lqhly 2010-01-12
  • 打赏
  • 举报
回复
再增加部分后台代码
<Columns>
<asp:TemplateField HeaderText="序号" HeaderStyle-HorizontalAlign="Center">
<ItemTemplate>
<asp:Label ID="LNID" runat="server" Text='<%# Bind("LNID") %>' ></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="数量" HeaderStyle-HorizontalAlign="Center">
<ItemTemplate>
<asp:Label ID="UORG" runat="server" Text='<%# Bind("UORG") %>' ></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="UORG" runat="server" Text='<%# Bind("UORG") %>'></asp:TextBox>
</EditItemTemplate> </asp:TemplateField>
WTPMCheng 2010-01-12
  • 打赏
  • 举报
回复
同意1楼的。。
如果LZ你使用的是模版列的话,就要找控件,然后转化成对应的控件。
int productId = Int32.Parse((this.gridProductList.Rows[e.RowIndex].Cells[0].FindControl("lblProductId") as Label).Text);

这样就可以取得到值了。

110,539

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

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

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