当前位置: 代码迷 >> ASP.NET >> GridView怎么取当前行的某列值
  详细解决方案

GridView怎么取当前行的某列值

热度:7232   发布时间:2013-02-25 00:00:00.0
GridView如何取当前行的某列值?
现有gridview1
绑定的字段有:id, name, stpreod 自定义模板列 del
是这么显示的: 其中id是user表的主键 storeid是store表的主键,删除是个按钮,定义了CommandName == "del")
  id name storeid del
  1 张三 2 删除  
  2 李四 3 删除
点删除的时候执行存储过程,把当前行的storeid作为传的值。
我试着这么写
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
  {
  // 取当前行
  if (e.CommandName == "del")
  {
  ?  
  }
  }
请问该如何写呢?谢谢

------解决方案--------------------------------------------------------
GridView1.Rows.cells[2].text
------解决方案--------------------------------------------------------
GridView1.Rows[int.Parse(e.CommandArgument.ToString())].Cells[2].Text;
------解决方案--------------------------------------------------------
把storeid参数传给存储过程,执行数据操作命令
------解决方案--------------------------------------------------------
参考:

http://blog.csdn.net/insus/archive/2008/03/30/2229898.aspx

http://blog.csdn.net/insus/archive/2008/02/27/2123970.aspx
------解决方案--------------------------------------------------------
自定义模板列 del 
加上 CommandArgument='<%#Container.DataItemIndex%>'
------解决方案--------------------------------------------------------
C# code
        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)        {            if (e.Row.RowIndex != -1)            {                int id = e.Row.RowIndex;                Button btnDel = (Button)e.Row.FindControl("btnDel"); //找刪除按鈕                HtmlButton btnEdit = (HtmlButton)e.Row.FindControl("btnEdit");                btnDel.CommandArgument = id.ToString();            }        }        protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)        {            if (e.CommandName == "Del")            {                int rowIndex = Convert.ToInt32(e.CommandArgument);                string id = this.GridView1.Rows[rowIndex].Cells[0].Text.ToString();                string sql_del = "刪除的sql語句";                //執行刪除操作            }       }
------解决方案--------------------------------------------------------
GridViewRow gr = this.GridView1.SelectedRow;
string a = gr.Cells["name"].Text.ToString();
------解决方案--------------------------------------------------------
string name = ((TextBox)this.GridView1.Rows[e.RowIndex].Cells[1].Controls[0]).Text;

------解决方案--------------------------------------------------------
为什么不把主键设上
GridView1.DataKeys="storeid";

id=GridView1.DataKeys[e.Index].value;
------解决方案--------------------------------------------------------
设置DataKeyNames 
然后用DataKeys