How to read Column0 from DataGrid

Discussion in 'ASP .Net Datagrid Control' started by brian yeo via .NET 247, Mar 7, 2005.

  1. I could not get the User ID/Column0 from the datagrid. Asillustrated below, I was able to retrieve the ItemIndex when Iclick the Edit button. What is the best method to get the UserID/Column0 (primary key) from the ItemIndex and pass the User IDto KeyValue? DataKeyField is not used due to date formattingreason.

    Please show sample as I am new to this stuff. Help is greatlyappreciated.

    ** DataGrid **
    Column0 Column1
    Edit Del John John Sample

    ** end **

    void DataGrid_Edit(object sender, DataGridCommandEventArgs e) {

    if (!isEditing) {
    DataGrid1.EditItemIndex = e.Item.ItemIndex;
    Response.BufferOutput = true;

    // working
    int keyValue = e.Item.ItemIndex; //eg 3

    string strline ="http://localhost/Quote/Employee/AddUser.aspx?id="+keyValue;
    Response.Redirect(strline,true);
    }
    }


    void BindGrid() {
    SqlConnection dbConnection = newSqlConnection(ConnectionString);
    SqlCommand dbCommand = new SqlCommand();

    dbCommand.CommandText = SelectCommand;
    dbCommand.Connection = dbConnection;

    SqlDataReader dr;
    dbConnection.Open();
    dr = dbCommand.ExecuteReader();

    DataTable dt = new DataTable();
    dt.Columns.Add("User ID");
    dt.Columns.Add("Username");
    dt.Columns.Add("Email");
    dt.Columns.Add("Create Date");
    dt.Columns.Add("Terminate Date");

    while (dr.Read())
    {
    DataRow r;

    r = dt.NewRow();

    r[0] = dr["Uid"].ToString().Trim();
    r[1] = dr["Username"].ToString().Trim();
    r[2] = dr["Email"].ToString().Trim();
    DateTime dtcdate = Convert.ToDateTime(dr["CreateDate"]);
    r[3] = string.Format("{0:d}",dtcdate);

    DateTime dttdate =Convert.ToDateTime(dr["TerminateDate"]);
    r[4] = string.Format("{0:d}",dttdate);

    dt.Rows.Add(r);
    }

    DataGrid1.DataSource = dt;
    DataGrid1.DataBind();

    }

    --------------------------------
    From: brian yeo

    -----------------------
    Posted by a user from .NET 247 (http://www.dotnet247.com/)

    <Id>dCycS+rsUUCknB6jgO8uOw==</Id>
     
    brian yeo via .NET 247, Mar 7, 2005
    #1
    1. Advertising

  2. brian yeo via .NET 247

    Elton Wang Guest

    Hi Brian,

    If it's a BoundColumn, you can use
    e.Item.Cells[colIndex].Text
    to get value in the colIndex.

    For your case you may use

    string strUid = e.Item.Cells[2].Text;

    HTH

    Elton Wang


    >-----Original Message-----
    >I could not get the User ID/Column0 from the datagrid. As

    illustrated below, I was able to retrieve the ItemIndex
    when I click the Edit button. What is the best method to
    get the User ID/Column0 (primary key) from the ItemIndex
    and pass the User ID to KeyValue? DataKeyField is not used
    due to date formatting reason.
    >
    >Please show sample as I am new to this stuff. Help is

    greatly appreciated.
    >
    >** DataGrid **
    > Column0 Column1
    >Edit Del John John Sample
    >
    >** end **
    >
    >void DataGrid_Edit(object sender,

    DataGridCommandEventArgs e) {
    >
    > if (!isEditing) {
    > DataGrid1.EditItemIndex = e.Item.ItemIndex;
    > Response.BufferOutput = true;
    >
    > // working
    > int keyValue = e.Item.ItemIndex; //eg 3
    >
    > string strline

    = "http://localhost/Quote/Employee/AddUser.aspx?
    id="+keyValue;
    > Response.Redirect(strline,true);
    > }
    >}
    >
    >
    >void BindGrid() {
    > SqlConnection dbConnection = new SqlConnection

    (ConnectionString);
    > SqlCommand dbCommand = new SqlCommand();
    >
    > dbCommand.CommandText = SelectCommand;
    > dbCommand.Connection = dbConnection;
    >
    > SqlDataReader dr;
    > dbConnection.Open();
    > dr = dbCommand.ExecuteReader();
    >
    > DataTable dt = new DataTable();
    > dt.Columns.Add("User ID");
    > dt.Columns.Add("Username");
    > dt.Columns.Add("Email");
    > dt.Columns.Add("Create Date");
    > dt.Columns.Add("Terminate Date");
    >
    > while (dr.Read())
    > {
    > DataRow r;
    >
    > r = dt.NewRow();
    >
    > r[0] = dr["Uid"].ToString().Trim();
    > r[1] = dr["Username"].ToString().Trim();
    > r[2] = dr["Email"].ToString().Trim();
    > DateTime dtcdate = Convert.ToDateTime(dr

    ["CreateDate"]);
    > r[3] = string.Format("{0:d}",dtcdate);
    >
    > DateTime dttdate = Convert.ToDateTime(dr

    ["TerminateDate"]);
    > r[4] = string.Format("{0:d}",dttdate);
    >
    > dt.Rows.Add(r);
    > }
    >
    > DataGrid1.DataSource = dt;
    > DataGrid1.DataBind();
    >
    >}
    >
    >--------------------------------
    >From: brian yeo
    >
    >-----------------------
    >Posted by a user from .NET 247 (http://www.dotnet247.com/)
    >
    ><Id>dCycS+rsUUCknB6jgO8uOw==</Id>
    >.
    >
     
    Elton Wang, Mar 7, 2005
    #2
    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. Isaac
    Replies:
    0
    Views:
    2,465
    Isaac
    Jul 10, 2003
  2. Doug
    Replies:
    3
    Views:
    4,379
    Bruce Barker
    Nov 4, 2005
  3. Replies:
    3
    Views:
    534
    John W. Krahn
    Apr 7, 2006
  4. Steve
    Replies:
    6
    Views:
    3,685
    Steve
    May 13, 2004
  5. Andreas Klemt
    Replies:
    0
    Views:
    310
    Andreas Klemt
    Oct 8, 2003
Loading...

Share This Page