How to read Column0 from DataGrid

  • Thread starter brian yeo via .NET 247
  • Start date
B

brian yeo via .NET 247

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

}
 
E

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
(e-mail address removed)
-----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>
.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,822
Latest member
israfaceZa

Latest Threads

Top