Any way to extract an ID from a grid?

  • Thread starter WISEMANOFNARNIA
  • 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
 
G

George

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
 
G

Guest

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
 

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

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,821
Latest member
AleidaSchi

Latest Threads

Top