Any way to extract an ID from a grid?

  • Thread starter Thread starter WISEMANOFNARNIA
  • Start date Start date
W

WISEMANOFNARNIA

I have a grid that is based on a select statement that returns an ID
as well as some other fields. I don't display the ID in a cell, but I
have the 'datakeynames' property equal to the ID.
I have a button called 'Edit' in every row which is not a command
button, but rather is a regular "button field" that sets off a
grid_rowcommand event. In this event, I'd like to capture the ID.
I've tried various things, but nothing works.
Is this possible to do?
Thanks,
Marvin
 
Usually Edit Command does not do much except makes Grid editable.
Here is a sample from my code.

private void grdIngridients_EditCommand(object source,
DataGridCommandEventArgs e)
{
grdIngridients.EditItemIndex = e.Item.ItemIndex;
grdIngridients.ShowFooter = false;
BindGrid();
}

private void grdIngridients_DeleteCommand(object source,
DataGridCommandEventArgs e)
{
int iId =
Convert.ToInt32(grdIngridients.DataKeys[e.Item.ItemIndex]);
//check if option is connected to an item.
string sSql = String.Format("DELETE tblIngridients WHERE Id={0} AND
MenuId={1}", iId, _iMenuId);
clsGlobal.ExecuteSql(sSql);
grdIngridients.EditItemIndex = -1;
grdIngridients.ShowFooter = true;
BindGrid();
}

private void grdIngridients_UpdateCommand(object source,
DataGridCommandEventArgs e)
{
int iId =
Convert.ToInt32(grdIngridients.DataKeys[e.Item.ItemIndex]);
GTextBox txtName = (GTextBox)e.Item.Cells[0].FindControl("txtName");
string sSql = String.Format("UPDATE tblIngridients SET Name='{2}'
WHERE Id={0} AND MenuId={1}", iId, _iMenuId, txtName.Value.Replace("'",
"''"));
clsGlobal.ExecuteSql(sSql);
grdIngridients.EditItemIndex = -1;
grdIngridients.ShowFooter = true;
BindGrid();
}

private void grdIngridients_CancelCommand(object source,
DataGridCommandEventArgs e)
{
grdIngridients.EditItemIndex = -1;
grdIngridients.ShowFooter = true;
BindGrid();
}

George
 
I have a grid that is based on a select statement that returns an ID
as well as some other fields.  I don't display the ID in a cell, but I
have the 'datakeynames' property equal to the ID.
I have a button called 'Edit' in every row which is not a command
button, but rather is a regular "button field" that sets off a
grid_rowcommand event.  In this event, I'd like to capture the ID.
I've tried various things, but nothing works.
Is this possible to do?
Thanks,
Marvin

Try http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.datakeys.aspx
 
Back
Top