DataKeyNames='FID'   //前台绑定一个值
GridView1.DataKeys[e.Row.RowIndex].Value.ToString;
--------------------------------------------------------------------------------------------------------------------------------
DataKeyNames='FID,FName'   //前台绑定两个值
GridView1.DataKeyNames = new String[] { "Id","WkNumber" }; //后台绑定两个字段值
//取关键字段值
GridView1.DataKeys[e.Row.RowIndex].Values[0].ToString;
GridView1.DataKeys[e.Row.RowIndex].Values[1].ToString;
--------------------------------------------------------------------------------------------------------------------------------
gridview取得某行的datakey:GridView中每行最后一列设有按钮,我需要在点击按钮后,得到该行的Datakey得值,然后触发其他操作。
答:首先绑定DataKeyNames   GridView.DataKeyNames=new String[]{"字段名称"};
取值string aaa=GridView.DataKeys[e.Row.RowIndex].Value.ToString();
--------------------------------------------------------------------------------------------------------------------------------
index=((GridViewRow)(((Button)sender).Parent.Parent)).RowIndex;
//就是一层一层往上找。通过现在的button找到他所在的cell,然后再调用parent,就找他所在的行,然后取rowindex,这个方法很常用的。
在GV中按选择链接(CommandName="Select")后获取ID索引值:
protected void Gv_SelectedIndexChanged(object sender, EventArgs e)
    {
        int id = Int32.Parse(GvLv.DataKeys[GvLv.SelectedIndex].Value.ToString());
        int id = Int32.Parse(Gv.DataKeys[Gv.SelectedIndex].Values[0].ToString());//多个关键字段时使用
        Response.Redirect("MdfLvApro.aspx?id=" + id);
    }        int id = Int32.Parse(GridView1.SelectedDataKey.Value.ToString());
--------------------------------------------------------------------------------------------------------------------------------
protected void CKAprd_CheckedChanged(object sender, EventArgs e)//在GV中有一checkbox/Button,点击后获取索引值
    {
        GridViewRow GvRow=(sender as CheckBox).NamingContainer as GridViewRow;//將事件源轉化為行
GridViewRow GvRow = (sender as Button).NamingContainer as GridViewRow;//將事件源轉化為行
        int index=GvRow.RowIndex;//index就是点击行的索引
        CheckBox cbox = (CheckBox)GridView1.Rows[index].FindControl("CKAprd");//找到所在行的控件
        string LvUs = ((Label)GridView1.Rows[index].FindControl("Label1")).Text;
    }
--------------------------------------------------------------------------------------------------------------------------------
protected void GvDpt_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        int id = Convert.ToInt32(GvDpt.DataKeys[e.RowIndex].Value.ToString());
        DtSc.ExcuteQuery("delete from Departments where id=" + id + "");
    }
--------------------------------------------------------------------------------------------------------------------------------
    protected void GvDpt_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        int id = Convert.ToInt32(GvEpyLst.DataKeys[e.RowIndex].Values[0].ToString());
        string WkNumber = GvEpyLst.DataKeys[e.RowIndex].Value.ToString();
        int id = int.Parse(GvDpt.DataKeys[e.RowIndex]["Id"].ToString());//Id,WkNumber关键字段有显示时获取其值
        string WkNumber = GvEpyLst.DataKeys[e.RowIndex]["WkNumber"].ToString();
    }
--------------------------------------------------------------------------------------------------------------------------------
    protected void GvEpyLst_RowDataBound(object sender, GridViewRowEventArgs e)//在DataBound中获取关键字段值
    {            string WkNb = GvEpyLst.DataKeys[e.Row.RowIndex].Values["WkNumber"].ToString();    }
--------------------------------------------------------------------------------------------------------------------------------
在 GridView1_PageIndexChanging中获取主键的值
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{ int index=GridView1.DataKeys[e.NewPageIndex].Value; }
--------------------------------------------------------------------------------------------------------------------------------
在 GridView1_RowEditing中获取主键的值
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{ int index = GridView1.DataKeys[e.NewEditIndex].Value;   }
--------------------------------------------------------------------------------------------------------------------------------
在GridView中有一“详细信息”按,点击后在RowCommand中获取主健值
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {     if (e.CommandName == "xs ")
           {    string v = e.CommandArgument;
             int id = Int32.Parse(GridView1.DataKeys[Convert.ToInt32(v)].Value.ToString());
int OrderId = Convert.ToInt32(GridView1.DataKeys[Convert.ToInt32(e.CommandArgument)].Value);
                Response.Redirect("delfault.aspx?key= " + id);        }      }
在RowCommand事件里是没有e.RowIndex这个定义的。你要是想得到某一行的索引。
首先,在你数据进行绑定的时候用RowDataBound这个事件,在这个事件里LinkButton linkbutton = (LinkButton)e.Row.Cells[0].FindControl("lbProjectName");
linkbutton.CommandArgument = e.Row.RowIndex.ToString();其中linkbutton.CommandArgument就是给你要执行的命令行设置一个行索引,这样你在你RowCommand事件里直接用e.CommandArgument就可以得到某一行的索引,然后通过这个索引就可以获取主键的值
--------------------------------------------------------------------------------------------------------------------------------
<asp:LinkButton id="linkbtn" runat="server" CommandName="L" CommandArgument=' <%# Eval("主键ID") %>'>
protected void GridView1_RowCommand(Object sender, GridViewCommandEventArgs e)
{     if(e.CommandName=="L") { int ID= Convert.ToInt32(e.CommandArgument);     response.write(ID);     } } 
string str = ((TextBox)this.GridView1.Rows[ID].FindControl("TextBox1")).Text.Trim();
protected void LinkButton1_Click(object sender, EventArgs e)
{ string id=((LinkButton)sender).CommandArgument.ToString() ;   }
或 LinkButton btn = sender as LinkButton;   GridViewRow row = btn.NamingContainer as GridViewRow; GridView grd = row.NamingContainer as GridView; int id = (int)grd.DataKeys[row.RowIndex].Value;
--------------------------------------------------------------------------------------------------------------------------------
            string strUsCls = (string)Session["UsCls"];
            int? UsCls = string.IsNullOrEmpty(strUsCls) ? 0 : Convert.ToInt32(strUsCls);// 
--------------------------------------------------------------------------------------------------------------------------------
    protected void CheckBox3_CheckedChanged(object sender, EventArgs e)
    {   if (((CheckBox)sender).Checked)
          { for (int i = 0; i < GridView1.Rows.Count; i++)
               {   ((CheckBox)GridView1.Rows[i].Cells[6].FindControl("CheckBox4")).Checked = true;    }
           }
        else
           {            fill();        }
   }

GridView绑定DataKeyNames以及如何取这些值的更多相关文章

  1. Jquery实现数据双向绑定(赋值和取值),类似AngularJS

    <!DOCTYPE html> <html> <head> <meta name="viewport" content="wid ...

  2. GridView中DataKeyNames的应用小结

    一. GridView的DataKeyNames属性设为"ID,Name" GridView1.DataKeyNames = new string[]{ "ID" ...

  3. GridView绑定数据与隐藏指定控件(模板列)

    1.1.    GridView绑定数据 1)       可以配置SqlDataSource数据源,修改select语句生成框架(不想手动绑定) 2)       删除DataSourceID属性和 ...

  4. php取默认值以及类的继承

    (1)对于php的默认值的使用和C++有点类似,都是在函数的输入中填写默认值,以下是php方法中对于默认值的应用: <?phpfunction makecoffee($types = array ...

  5. 在android的spinner中,实现取VALUE值和TEXT值。 ZT

    在android的spinner中,实现取VALUE值和TEXT值.   为了实现在android的 spinner实现取VALUE值和TEXT值,我尝试过好些办法,在网上查的资料,都是说修改适配器, ...

  6. 在android的spinner中,实现取VALUE值和TEXT值

    为了实现在android的spinner实现取VALUE值和TEXT值,我尝试过好些办法,在网上查的资料,都是说修改适配器,刚开始我也是通过修改适配器的方法来做的,但是如果一个activity有多个s ...

  7. java.sql.ResultSet技术(从数据库查询出的结果集里取列值)

    里面有一个方法可以在查询的结果集里取出列值,同理,存储过程执行之后返回的结果集也是可以取到的. 如图: 然后再运用 java.util.Hashtable 技术.把取到的值放入(K,V)的V键值里,K ...

  8. 快速排序 之添加复合插入排序和原始序列取中值左pivot

    quicksort中,当n小于一定值时,排序效率就比直接插入排序底了,所以,此时就不要再递归下去了,直接插入排序好了:快速的原理就是因为折半递归,所以初始pivot应该有个好一点的选择,这里在原序列左 ...

  9. 前端动态属性页面的 要用id做name 因为这样方便在提交表单时候取到值

    前端动态属性页面的 要用id做name 因为这样方便在提交表单时候取到值

随机推荐

  1. PHP 5 数据类型

    本页内容来自http://www.runoob.com/php/php-datatypes.html String(字符串), Integer(整型), Float(浮点型), Boolean(布尔型 ...

  2. OpenCV ——背景建模之CodeBook(1)

    1,CodeBook算法流程介绍 CodeBook算法的基本思想是得到每个像素的时间序列模型.这种模型能很好地处理时间起伏,缺点是需要消耗大量的内存.CodeBook算法为当前图像的每一个像素建立一个 ...

  3. putty 直接连 快捷键方式

    快捷方式 : "C:\Program Files (x86)\puTTY\putty.exe" root@linux.9hlh.com d:\soft\putty.exe -pw ...

  4. jsp第1讲(上集)

    jsp讲解框架 (一)Java EE核心十三种技术介绍 (二)Java EE程序员修炼成精的法门 (三)jsp版本的用户管理系统演示 (四)jsp概述 (五)jsp的运行原理 (六)jsp版的计算器 ...

  5. ios 中NSString的一些调用

    #import <Foundation/Foundation.h> int main(int argc, const char * argv[]) {    @autoreleasepoo ...

  6. 关于PHP执行超时的问题

    PHP配置文件的参数max_execution_time表示脚本执行超时时间 max_execution_time=0表示不限制 max_execution_time=2表示执行两秒后终止,同时报错F ...

  7. linux下mysql root密码忘记修改方法

    一.MySQL密码的恢复方法之一 如果忘记了MySQL的root密码,可以用以下方法重新设置:1.切换到root下su root 2. KILL掉系统里的MySQL进程: killall -TERM ...

  8. 四维dp 或者 剪枝 + dfs Codeforces Beta Round #6 (Div. 2 Only) D

    http://codeforces.com/contest/6/problem/D 题目大意:有一队人,排成一列,每个人都有生命值,你每次可以攻击2~n位置的一个的人,假设每次攻击的位置为pos,那么 ...

  9. List<T> 求差集

    List<, , , , , }; List<, , , , , }; List<int> c = b.Except(a).ToList(); foreach (int i i ...

  10. redis - java 基本操作

    import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; im ...