problem: when i'm binding in a gridview, some product name, and theyr prices, i would like to add a column with a textbox and a button/image button to add that product with some quantity to my order, how do i do that?
example:
my solution/example is:
1º - we must add the column, of type templatefield, and then select the GridView > Edit Templates > select the column, and insert in the "item Template" the textbox and the image button, after that select imagebutton and write something in the property "CommandName" like "addbutton"
2º - Add event RowCreated, to the GridView, and insert the following code:
if (e.Row.RowType == DataControlRowType.DataRow)
{
ImageButton AddP = (ImageButton)e.Row.Cells[10].FindControl("imgBtAddP");
AddP.CommandArgument = e.Row.RowIndex.ToString();
}
this code, is needed to give the correct ID to each row, or else, it would be almost impossible to get the id of the button that was clicked, i mean, the rowid of the button clicked.
3º - now, add the event RowCommand to the GridView, and insert the following code
if (e.CommandName.ToLower().Equals("addbutton"))
{
// get the index of the row
int index = Convert.ToInt32(e.CommandArgument);
// get the value from the textbox
string quantity = ((TextBox)gView.Rows[index].Cells[5].FindControl("txtQuantity")).Text;
// if you define Key when binding the GridView, if you need to get the key of that line use the following
gView.DataKeys[index].Value;
}
and with this, you can do a lot of things.. just need to explore it.
Hope it helps :)