各位大侠帮帮忙,GridView取选中行的某列的值
_________________________________________
是否购买|试剂编号|试剂名称|库存|单价....
------------------------------------------
购买 253145 得灵试剂 10 600
我在GridView中加入一按钮列,CommandName为 "add ",我要点击"购买",然后取这一行的试剂编号的值保存的哈稀表中传到另一个页面,老是出错,不知道怎搞,刚用asp2.0,有点晕.
protected void GridView1_RowCommand(object sender,GridViewCommandEventArgs e)
{
if (e.CommandName == "add ")
{
string reId = ?这里的试剂编号怎么取,用了很多方法,总是不行,在asp1.0中,只要设置表格的DataKeyField= "re_id ",然后在用DataKeys取编号这个值,2.0怎么不行啊,请各位帮帮忙?
if (Session[ "bus "] == null)
{
Hashtable ht = new Hashtable();
ht.Add(reId, 1);
Session[ "bus "] = ht;
}
}
else
{
System.Collections.Hashtable ht = (Hashtable)Session[ "bus "];
if (ht[reId] == null)
{
ht[reId] = 1;
}
else
{
ht[reId] = (int)ht[reId] + 1;
}
Session[ "bus "] = ht;
}
}
[解决办法]
string reId = ?这里的试剂编号怎么取,用了很多方法,总是不行,在asp1.0中,只要设置表格的DataKeyField= "re_id ",然后在用DataKeys取编号这个值,2.0怎么不行啊,请各位帮帮忙?
===========
1。
GridView 中与之对应的是 DataKeyNames ,并且可以设置多个键
2。
// .aspx
<asp:gridview id= "GridView1 " datakeynames= "re_id " ............
// .aspx.cs
if (e.CommandName == "add ")
{
Control cmdSource = e.CommandSource as Control;
GridView grd = sender as GridView;
GridViewRow row = cmdSource.NamingContainer as GridViewRow;
int rowIndex = row.RowIndex;
string reId = grd.DataKeys[rowIndex].Value.ToString();
// .........
3。
有用的相关信息:
如何在GridView的RowCommand事件中获取当前的GridViewRow
http://www.cnblogs.com/Jinglecat/archive/2007/07/05/806460.html
[解决办法]
方法一:
step1:设置你的GridView的DataKeyNames为re_id
<asp:GridView ID= "GridView1 " runat= "server " DataKeyNames= "re_id " ...
step2:在RowCreated事件中给按钮的CommandArgument属性赋值为当前行索引
protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
{
LinkButton addButton ;
if (e.Row.RowType == DataControlRowType.DataRow)
{
addButton = (LinkButton)e.Row.Cells[0].Controls[0];//请确认你的按钮的位置
if (addButton != null)
{
if (addButton.CommandName== "add ")
addButton.CommandArgument = e.Row.RowIndex.ToString();
}
}
}
step3:在RowCommand中得到此行主键列
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "add ")
{
Response.Write(GridView1.DataKeys[Convert.ToInt32(e.CommandArgument)].Value.ToString());
}
}