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