Home All Groups Group Topic Archive Search About
Author
7 Mar 2005 7:02 PM
brian yeo via .NET 247
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>

Author
7 Mar 2005 8:40 PM
Elton Wang
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
elton_w***@hotmail.com

>-----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.
Show quoteHide quote
>
>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;
Show quoteHide quote
>       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>
>.
>