GridView绑定DataKeyName以及如何取这些值

来源:互联网 发布:守望先锋帧数优化补丁 编辑:程序博客网 时间:2024/06/05 23:39

转自:http://blog.csdn.net/angel20082008/article/details/33766291

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

0 0
原创粉丝点击