Create textboxes and buttons in gridview - asp.net

I have a gridview dynamicaly create from an ODBC datasource.
I want to make something like shopping cart so i want to add a column with textboxes for the quantity and some buttons to add to shopping cart the item in the row.
Is there any example?

Create a EventHandler for RowCreated , this will fire everytime a row is dynamically created
Something similar to this:
void gridView1_RowCreated(object sender, GridViewRowEventArgs e)
{
GridViewRow row = e.Row;
TableCell actionsCell = new TableCell();
//put code here to add to actionsCell
// you might have to modify this to specify where in the row to insert
row.Cells.Add(actionsCell);
}
Then put your button or text or whatever you want in this dynamically created cell in the row

Related

Non Gridview event capture edit mode row index

How can I get the index of a gridview row in edit mode from an event that is not a gridview event?
I have a DDL in a cell of the Gridview. When the DDL changes I want to perform an update to a label of another cell in the same row.
So I am using the DDL_SelectedIndexChanged Event and have everything working including the post to the label, but its always the frist row. What is the correct way to get the row index in edit mode from outside of a gridview event?
Thanks,
try this
protected void DDL_SelectedIndexChanged(Object sender, EventArgs e)
{
DropDownList ddlcontrol = (DropDownList) sender;
GridViewRow grrow = (GridViewRow) ddlcontrol.NamingContainer;
int rowindex = grrow.RowIndex;
}

How to get values from Controls which are in GridView?

I am new to asp.net website developer.
In my website I use GridView Control.In the GridView row i place some controls like
TextBox,DropDownList.
I can display the values in GridView.
But my requirement is ,Get the values from TextBox and DropdownList Which are existed in GridView.
Please help me to go forward..
thank you,
bye..
You need to access them by row. This code project article explains it in detail.
TextBox tb = (TextBox)gridview1.Rows[0].FindControl("idOfTextBox");
It above statement will find control in the first row of grid which has id idOfTextBox and type is textbox.
You can directly get any value of any control by casting directly, without using an extra textbox or dropdown variable.
Example:
string val = ((TextBox)gridview1.Rows[0].FindControl("TextBox_ID")).Text;
Hope it helps :)
Subscribe the RowDataBound event(this will be fired on each row available in gridview) of gridview and access like stated below,
protected void grdBillingdata_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
//these controls are available inside gridview
HiddenField hdnNagetiveAmt = (HiddenField)e.Row.FindControl("hdnNagetiveAmt");
DropDownList ddlFeeType = (DropDownList)e.Row.FindControl("ddlFeeType");
TextBox txtFeeDesc = (TextBox)e.Row.FindControl("txtFeeDesc");
Button btnUpdate = (Button)e.Row.FindControl("btnUpdate");
}
}
not only text box we can get values from all the controls that used inside gridview that placed by using itemTemplate or simply a Bound field .
you need to loop for each row to get the values
foreach (GridViewRow row in grd_popup_details.Rows)
{
//get control values
}
Refference link

How to hide Template Field Edit column in Gridview based on Gridview Row Count

I have a gridview, where i can add/delete rows. I dont want to give the delete option on the first row, that is I can delete any row but not the first one. How can i disable edit option for the first row ?
You can disable that cell from GridView.RowDataBound as you have used Template field, you can use FindControl method and then disable that cell..
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowIndex == 0)
{
//Disable that cell here..
}
}

how to get selected cell value in gridview using asp..net

I am using a Link button in a GridView. I need the value of the button I have clicked. Is there any way to achieve this ?
I suppose you want to get related row from GridView
protected void GridView1_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
{
GridViewRow row = GridView1.Rows[e.NewSelectedIndex];
}
The required cell you can get from cells collection row.Cells

Asp.Net Gridview Buttonfield get Row Data

I have a Gridview which is binded to a datatable that I created. The data in the table is ever changing.
I added a buttonfield to the Gridview. When clicked I want to send a specific columns value in that row through a link or sent through a function.
How do you go about doing this?
I found out how to do it. You set the commandName property to whatever you want to call it inside the properties.
Then if you double click on the button after creating it in design view, it should pop up a function in the code behind page. You can then access the row by doing the following.
protected void gvUsers_RowCommand(object sender, GridViewCommandEventArgs e)
{
int rowNum = int.Parse(e.CommandArgument.ToString());
}
from there you can use the row number to access specific column data.
Here's a decent example from MSDN: http://msdn.microsoft.com/en-us/library/bb907626.aspx

Resources