how can I get the DataKeyField

Discussion in 'ASP .Net Datagrid Control' started by Kylin, Apr 22, 2005.

  1. Kylin

    Kylin Guest

    All that I want to get the DataKeyFielD
    this is the under code

    Response.Write(DataGrid1.DataKeyField.ToString());


    but the result is no I want ..


    <!-- html.. -->
    <%@ Page language="c#" Codebehind="DG_DataKeyFied.aspx.cs"
    AutoEventWireup="false" Inherits="ForTest.DG_DataKeyFied" %>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
    <HTML>
    <HEAD>
    <title>DG_DataKeyFied</title>
    <meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
    <meta name="CODE_LANGUAGE" Content="C#">
    <meta name="vs_defaultClientScript" content="JavaScript">
    <meta name="vs_targetSchema"
    content="http://schemas.microsoft.com/intellisense/ie5">
    </HEAD>
    <body>
    <form id="Form1" method="post" runat="server">
    <asp:DataGrid id="DataGrid1" runat="server" Width="100%"
    DataKeyField="au_id">
    <Columns>
    <asp:TemplateColumn>
    <ItemTemplate>
    <asp:CheckBox ID="remove" Runat="server"></asp:CheckBox>
    </ItemTemplate>
    </asp:TemplateColumn>
    </Columns>
    </asp:DataGrid>
    <asp:Button id="btnGet" runat="server" Text="Get the Value of the checked
    Item !"></asp:Button>
    </form>
    </body>
    </HTML>

    //Behind.cs

    using System;
    using System.Collections;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Web;
    using System.Web.SessionState;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.HtmlControls;
    using System.Data.SqlClient;

    namespace ForTest
    {
    /// <summary>
    /// DG_DataKeyFied µÄժҪ˵Ã÷¡£
    /// </summary>
    public class DG_DataKeyFied : System.Web.UI.Page
    {
    protected System.Web.UI.WebControls.DataGrid DataGrid1;
    private SqlConnection myConn;
    private SqlCommand myComm;
    protected System.Web.UI.WebControls.Button btnGet;
    private SqlDataAdapter myAdapter;

    private void Page_Load(object sender, System.EventArgs e)
    {
    // ÔÚ´Ë´¦·ÅÖÃÓû§´úÂëÒÔ³õʼ»¯Ò³Ãæ
    if(!this.IsPostBack)
    {
    DGBind();
    }


    }

    private void DGBind()
    {
    DataSet data=new DataSet();
    myConn=new SqlConnection("server=127.0.0.1;user
    id=sa;pwd=sa;database=pubs");
    myComm=new SqlCommand("Select * From authors",myConn);
    myAdapter=new SqlDataAdapter();
    myAdapter.SelectCommand=myComm;
    myAdapter.Fill(data);
    DataGrid1.DataSource=data;
    DataGrid1.DataBind();
    }

    #region Web ´°ÌåÉè¼ÆÆ÷Éú³ÉµÄ´úÂë
    override protected void OnInit(EventArgs e)
    {
    //
    // CODEGEN: ¸Ãµ÷ÓÃÊÇ ASP.NET Web ´°ÌåÉè¼ÆÆ÷Ëù±ØÐèµÄ¡£
    //
    InitializeComponent();
    base.OnInit(e);
    }

    /// <summary>
    /// Éè¼ÆÆ÷Ö§³ÖËùÐèµÄ·½·¨ - ²»ÒªÊ¹ÓôúÂë±à¼­Æ÷ÐÞ¸Ä
    /// ´Ë·½·¨µÄÄÚÈÝ¡£
    /// </summary>
    private void InitializeComponent()
    {
    this.btnGet.Click += new System.EventHandler(this.btnGet_Click);
    this.Load += new System.EventHandler(this.Page_Load);

    }
    #endregion

    private void btnGet_Click(object sender, System.EventArgs e)
    {
    int id;
    for(int i=0;i<DataGrid1.Items.Count;i++)
    {
    CheckBox remove=(CheckBox)DataGrid1.Items.FindControl("remove");

    if (remove.Checked==true)
    {

    Response.Write(DataGrid1.DataKeyField.ToString());


    }
    }

    }
    }
    }





    --
    FireCrow Studio
    Kylin Garden
    EMail:
    ICQ:156134382
    Kylin, Apr 22, 2005
    #1
    1. Advertising

  2. Kylin

    Elton W Guest

    Hi Kylin,


    Try

    datagrid.DataKeys

    HTH


    Elton Wang



    >-----Original Message-----
    >All that I want to get the DataKeyFielD
    >this is the under code
    >
    >Response.Write(DataGrid1.DataKeyField.ToString());
    >
    >
    >but the result is no I want ..
    >
    >
    ><!-- html.. -->
    ><%@ Page language="c#" Codebehind="DG_DataKeyFied.aspx.cs"
    >AutoEventWireup="false" Inherits="ForTest.DG_DataKeyFied"

    %>
    ><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0

    Transitional//EN" >
    ><HTML>
    > <HEAD>
    > <title>DG_DataKeyFied</title>
    > <meta name="GENERATOR" Content="Microsoft Visual

    Studio .NET 7.1">
    > <meta name="CODE_LANGUAGE" Content="C#">
    > <meta name="vs_defaultClientScript"

    content="JavaScript">
    > <meta name="vs_targetSchema"
    >content="http://schemas.microsoft.com/intellisense/ie5">
    > </HEAD>
    > <body>
    > <form id="Form1" method="post" runat="server">
    > <asp:DataGrid id="DataGrid1" runat="server"

    Width="100%"
    >DataKeyField="au_id">
    > <Columns>
    > <asp:TemplateColumn>
    > <ItemTemplate>
    > <asp:CheckBox ID="remove"

    Runat="server"></asp:CheckBox>
    > </ItemTemplate>
    > </asp:TemplateColumn>
    > </Columns>
    > </asp:DataGrid>
    > <asp:Button id="btnGet" runat="server" Text="Get the

    Value of the checked
    >Item !"></asp:Button>
    > </form>
    > </body>
    ></HTML>
    >
    >//Behind.cs
    >
    >using System;
    >using System.Collections;
    >using System.ComponentModel;
    >using System.Data;
    >using System.Drawing;
    >using System.Web;
    >using System.Web.SessionState;
    >using System.Web.UI;
    >using System.Web.UI.WebControls;
    >using System.Web.UI.HtmlControls;
    >using System.Data.SqlClient;
    >
    >namespace ForTest
    >{
    > /// <summary>
    > /// DG_DataKeyFied µÄժҪ˵Ã÷¡£
    > /// </summary>
    > public class DG_DataKeyFied : System.Web.UI.Page
    > {
    > protected System.Web.UI.WebControls.DataGrid DataGrid1;
    > private SqlConnection myConn;
    > private SqlCommand myComm;
    > protected System.Web.UI.WebControls.Button btnGet;
    > private SqlDataAdapter myAdapter;
    >
    > private void Page_Load(object sender, System.EventArgs

    e)
    > {
    > // ÔÚ´Ë´¦·ÅÖÃÓû§´úÂëÒÔ³õʼ»¯Ò³Ãæ
    > if(!this.IsPostBack)
    > {
    > DGBind();
    > }
    >
    >
    > }
    >
    > private void DGBind()
    > {
    > DataSet data=new DataSet();
    > myConn=new SqlConnection("server=127.0.0.1;user
    >id=sa;pwd=sa;database=pubs");
    > myComm=new SqlCommand("Select * From authors",myConn);
    > myAdapter=new SqlDataAdapter();
    > myAdapter.SelectCommand=myComm;
    > myAdapter.Fill(data);
    > DataGrid1.DataSource=data;
    > DataGrid1.DataBind();
    > }
    >
    > #region Web ´°ÌåÉè¼ÆÆ÷Éú³ÉµÄ´úÂë
    > override protected void OnInit(EventArgs e)
    > {
    > //
    > // CODEGEN: ¸Ãµ÷ÓÃÊÇ ASP.NET Web ´°ÌåÉè¼ÆÆ÷Ëù±ØÐèµÄ¡£
    > //
    > InitializeComponent();
    > base.OnInit(e);
    > }
    >
    > /// <summary>
    > /// Éè¼ÆÆ÷Ö§³ÖËùÐèµÄ·½·¨ - ²»ÒªÊ¹ÓôúÂë±à¼­Æ÷ÐÞ¸Ä
    > /// ´Ë·½·¨µÄÄÚÈÝ¡£
    > /// </summary>
    > private void InitializeComponent()
    > {
    > this.btnGet.Click += new System.EventHandler

    (this.btnGet_Click);
    > this.Load += new System.EventHandler(this.Page_Load);
    >
    > }
    > #endregion
    >
    > private void btnGet_Click(object sender,

    System.EventArgs e)
    > {
    > int id;
    > for(int i=0;i<DataGrid1.Items.Count;i++)
    > {
    > CheckBox remove=(CheckBox)DataGrid1.Items

    .FindControl("remove");
    >
    > if (remove.Checked==true)
    > {
    >
    > Response.Write(DataGrid1.DataKeyField.ToString());
    >
    >
    > }
    > }
    >
    > }
    > }
    >}
    >
    >
    >
    >
    >
    >--
    >FireCrow Studio
    >Kylin Garden
    >EMail:
    >ICQ:156134382
    >
    >
    >.
    >
    Elton W, Apr 22, 2005
    #2
    1. Advertising

  3. Kylin

    Kylin Guest

    Thanks your for your help.
    I have solved this problem!



    "Elton W" <> ????
    news:0fe601c54737$9df85c40$...
    Hi Kylin,


    Try

    datagrid.DataKeys

    HTH


    Elton Wang



    >-----Original Message-----
    >All that I want to get the DataKeyFielD
    >this is the under code
    >
    >Response.Write(DataGrid1.DataKeyField.ToString());
    >
    >
    >but the result is no I want ..
    >
    >
    ><!-- html.. -->
    ><%@ Page language="c#" Codebehind="DG_DataKeyFied.aspx.cs"
    >AutoEventWireup="false" Inherits="ForTest.DG_DataKeyFied"

    %>
    ><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0

    Transitional//EN" >
    ><HTML>
    > <HEAD>
    > <title>DG_DataKeyFied</title>
    > <meta name="GENERATOR" Content="Microsoft Visual

    Studio .NET 7.1">
    > <meta name="CODE_LANGUAGE" Content="C#">
    > <meta name="vs_defaultClientScript"

    content="JavaScript">
    > <meta name="vs_targetSchema"
    >content="http://schemas.microsoft.com/intellisense/ie5">
    > </HEAD>
    > <body>
    > <form id="Form1" method="post" runat="server">
    > <asp:DataGrid id="DataGrid1" runat="server"

    Width="100%"
    >DataKeyField="au_id">
    > <Columns>
    > <asp:TemplateColumn>
    > <ItemTemplate>
    > <asp:CheckBox ID="remove"

    Runat="server"></asp:CheckBox>
    > </ItemTemplate>
    > </asp:TemplateColumn>
    > </Columns>
    > </asp:DataGrid>
    > <asp:Button id="btnGet" runat="server" Text="Get the

    Value of the checked
    >Item !"></asp:Button>
    > </form>
    > </body>
    ></HTML>
    >
    >//Behind.cs
    >
    >using System;
    >using System.Collections;
    >using System.ComponentModel;
    >using System.Data;
    >using System.Drawing;
    >using System.Web;
    >using System.Web.SessionState;
    >using System.Web.UI;
    >using System.Web.UI.WebControls;
    >using System.Web.UI.HtmlControls;
    >using System.Data.SqlClient;
    >
    >namespace ForTest
    >{
    > /// <summary>
    > /// DG_DataKeyFied µÄժҪ˵Ã÷¡£
    > /// </summary>
    > public class DG_DataKeyFied : System.Web.UI.Page
    > {
    > protected System.Web.UI.WebControls.DataGrid DataGrid1;
    > private SqlConnection myConn;
    > private SqlCommand myComm;
    > protected System.Web.UI.WebControls.Button btnGet;
    > private SqlDataAdapter myAdapter;
    >
    > private void Page_Load(object sender, System.EventArgs

    e)
    > {
    > // ÔÚ´Ë´¦·ÅÖÃÓû§´úÂëÒÔ³õʼ»¯Ò³Ãæ
    > if(!this.IsPostBack)
    > {
    > DGBind();
    > }
    >
    >
    > }
    >
    > private void DGBind()
    > {
    > DataSet data=new DataSet();
    > myConn=new SqlConnection("server=127.0.0.1;user
    >id=sa;pwd=sa;database=pubs");
    > myComm=new SqlCommand("Select * From authors",myConn);
    > myAdapter=new SqlDataAdapter();
    > myAdapter.SelectCommand=myComm;
    > myAdapter.Fill(data);
    > DataGrid1.DataSource=data;
    > DataGrid1.DataBind();
    > }
    >
    > #region Web ´°ÌåÉè¼ÆÆ÷Éú³ÉµÄ´úÂë
    > override protected void OnInit(EventArgs e)
    > {
    > //
    > // CODEGEN: ¸Ãµ÷ÓÃÊÇ ASP.NET Web ´°ÌåÉè¼ÆÆ÷Ëù±ØÐèµÄ¡£
    > //
    > InitializeComponent();
    > base.OnInit(e);
    > }
    >
    > /// <summary>
    > /// Éè¼ÆÆ÷Ö§³ÖËùÐèµÄ·½·¨ - ²»ÒªÊ¹ÓôúÂë±à¼­Æ÷ÐÞ¸Ä
    > /// ´Ë·½·¨µÄÄÚÈÝ¡£
    > /// </summary>
    > private void InitializeComponent()
    > {
    > this.btnGet.Click += new System.EventHandler

    (this.btnGet_Click);
    > this.Load += new System.EventHandler(this.Page_Load);
    >
    > }
    > #endregion
    >
    > private void btnGet_Click(object sender,

    System.EventArgs e)
    > {
    > int id;
    > for(int i=0;i<DataGrid1.Items.Count;i++)
    > {
    > CheckBox remove=(CheckBox)DataGrid1.Items

    .FindControl("remove");
    >
    > if (remove.Checked==true)
    > {
    >
    > Response.Write(DataGrid1.DataKeyField.ToString());
    >
    >
    > }
    > }
    >
    > }
    > }
    >}
    >
    >
    >
    >
    >
    >--
    >FireCrow Studio
    >Kylin Garden
    >EMail:
    >ICQ:156134382
    >
    >
    >.
    >
    Kylin, Apr 23, 2005
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. sramruttun
    Replies:
    0
    Views:
    339
    sramruttun
    Jul 10, 2003
  2. Felbrigg

    Composite DataKeyField?

    Felbrigg, Feb 9, 2004, in forum: ASP .Net
    Replies:
    4
    Views:
    2,821
    Ismail Rajput
    Feb 11, 2004
  3. Beryl Small

    DatakeyField with multi-column primary key

    Beryl Small, Mar 1, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    436
    Beryl Small
    Mar 1, 2004
  4. Sean

    how to get datagrid item index from DataKeyField value?

    Sean, Aug 6, 2003, in forum: ASP .Net Datagrid Control
    Replies:
    1
    Views:
    761
    Arthur
    Aug 7, 2003
  5. Kylin

    How to get the DataKeyField in the DataGrid ?

    Kylin, May 31, 2005, in forum: ASP .Net Datagrid Control
    Replies:
    1
    Views:
    462
    Brock Allen
    May 31, 2005
Loading...

Share This Page