Asp.Net Gridview Buttonfield get Row Data - asp.net

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

Related

Click on Edit button in GridView puts wrong row in edit mode

I have gridview putting data from one table from database using stored procedure.
On click of Edit Button, post back is happen which refreshing the data on gridview and wrong row is shown in edir mode.
I have found the reason is that on click on EDIT link javascript:__doPostBack('GridView','Edit$0') is displayed at the status bar, which is a problem here. Edit$0 means the absolute value of the row and when postback get the data from data base wrong row is shown in edit mode...
I think the solution will be put the edit mode based on not row number but some unique value of the selected row for edit.
Please help if any one has answer to it.
If you're using a gridview I suggest using the native OnRowEditing="GridViewEditEventHandler"
and then in the code behind
protected void TaskGridView_RowEditing(object sender, GridViewEditEventArgs e)
{
//Set the edit index.
TaskGridView.EditIndex = e.NewEditIndex;
//Bind data to the GridView control.
BindData();
}
see http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.rowediting(v=vs.110).aspx
However, if for some reason you are creating an OnClick function when you press the edit button and then changing the Itemtemplate to show/hide values I would suggest doing
GridViewRow gvr = (GridViewRow)(((Button)e.CommandSource).NamingContainer);
int RowIndex = gvr.RowIndex;
from there you can do the .findcontrol to find the specific item you want ex:
TextBox txtbox = (TextBox)TaskGridView.Rows[RowIndex].FindControl("textBoxID");
Hope this helps

ASP.NET Boundfield changes when I update another gridview

I have a boundfield in a gridview that programatically receives a hyperlink for the content in it. It gets a new dataset after a drop down list index change. One of the columns of the dataset will apply links to fields with data and skip those without, you can see my logic for applying links below:
if (e.Row.DataItem != null && int.TryParse(e.Row.Cells[4].Text, out incidents))
{
HyperLink incidentsLink = new HyperLink();
incidentsLink.ForeColor = System.Drawing.Color.Blue;
incidentsLink.NavigateUrl = "~/somesite.aspx?no=" + stnNum + "&dt=" + date;
incidentsLink.Text = e.Row.Cells[4].Text;
e.Row.Cells[4].Controls.Add(incidentsLink);
}
This is applied OnRowDataBound for the gridview. Then I have another gridview that is wired to another drop down list. When either drop down list changes index it grabs a new dataset for the related gridview and fires a ajax update using an update panel.
What happens is when the second gridview updates it erases the links in the first gridview. It doesn't erase the text, that stays, but the text is no longer anchored to a link. All links made this way and put into gridviews lose their link properties, however fields created using asp:HyperLinkField are left unchanged. The problem is that stnNum and date are not a part of the dataset that is returned for the first gridview, so I have to add them as a link after the gridview is already built.
The only solution I can think of is to refire the function that adds links to the gridview each time the second gridview updates. Any other solutions would be helpful, or an explanation as to why my link is being erased would be great.
Try binding the GridView/DropDowns only if there is not any POSTBACK. I believe POST Back event is letting your gridview & Controls to loose their data somehow.
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
// Bind your Grids & Dropdowns here on page load
}
}

GridView column width

I'm using VS 2010. I have a GridView with few template columns. I want the 2nd column to not be visible at all, but still to be existed so javascript will be able to see it's value.
Does someone knows how to set this width value?
Thanks
Place a HiddenField in the first column and put the value that you need to put it in the second column in it instead of creating the second column.
The Problem:
Your problem originates from the fact that when you hide a data-bound GridView's column, its bounded value is no longer available and if you tried to access it you will get an empty string.
The solution:
Enable 2 events in in your gridview:
RowDataBound: In this event you can access the hidden cell value (before hiding it yet)
protected void MyGridView_RowDataBound(Object sender, GridViewRowEventArgs)
{
// Here you store the value
this.sID = e.Row.Cells[1].Text;
}
RowCreated: In this event you hide the cell, write this in the event handler:
protected void MyGridView_RowCreated(Object sender, GridViewRowEventArgs)
{
// then you hide the cell (Only the cell not the column)
e.Row.Cells[1].Visible = false;
}
In these codes, after we save the value we need in another variable/array whatever, we can easily hide the cell. You can put that value in a hidden input to enable accessing the value from javascript.

Hide Gridview Command Buttons

I have a gridview which has rows of 'tickets'. A user has the ability to 'claim' a ticket, this is all set up and works. What i need is to be able to change/hide the 'claim' button and replace it with a 'release' or 'open' command button. Can i achieve this at a rowdatabound level? What i do not want is to query the db everytime to see if the ticket is claimed.
You could cast the button from its Cell, then change its CommandName, and CommandArgs if needed, along with its text; eg use the same actual button for many purposes.
Im asssuming there is some status field that dictates what you can do with a record? Therefore on RowDataBound get this via a datakey, and adjust the button to suit.
Then on its click, check the command name and execute the relevant function / code block?
Edit - like this:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
Button btn = e.Row.Cells[YourButtonsColumIndex].FindControl("btnYourButtonsID") as Button;
btn.CommandName = "Release";
//Or
((Button)e.Row.Cells[YourButtonsColumIndex].FindControl("btnYourButtonsID")).CommandName = "Release";
}
Bearing in mind hidden rows still count in the zero-index column list.

When GRidView's row is in edit mode, textboxes don't display current values

1) I noticed that if we don’t bind GridView to object data source control, then when user puts GridView into edit mode, we have to handle GridView.RowEditing event (else we get an exception ) and in this event put GridView’s row into editing mode. Is there a reason why GridView doesn’t automatically put a row into edit mode?
2) When we manually bind GridView to one of DataSet’s tables and user puts a row into edit mode, row’s columns will replace fields with text boxes. But for some reason these text boxes don’t display current field values, but instead they don’t display any text at all. What am I doing wrong?
3) I’ve also handled gridView.RowUpdated event, so I could put row back into non-edit mode, but to no effect. I even tried by pressing Edit button of some other row, but row still wouldn’t go out of edit mode. Any ideas what I’m doing wrong?
protected void gvwEmployees_RowUpdated(object sender, GridViewUpdatedEventArgs e)
{
e.KeepInEditMode = false;
}
Thanx
When not using a DataSource control with a GridView or other data-bound control which hide the complexity of the manual data-binding you must manually handle RowEditing, RowUpdating, and RowDeleting etc. With the built in data model and automatic binding the GridView handles these events for you.
You haven't posted your RowEditing code, but i suspect that you are not setting the GridViews EditIndex to the NewEditIndex and are not rebinding, this is probably why you are not seeing current data.
protected void gvwEmployees_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView.EditIndex = e.NewEditINdex;
BindData();
}
The same is true for your RowUpdating event. You will have to manually update your data, then set the EditIndex to -1, this will put your GridView back into ReadOnly mode. Keep in mind that e.OldValues, e.NewValues and e.Keys properties of the GridViewUpdateEventArgs are not populated when binding manually. This mean you'll have to take care of the update yourself by using e.RowIndex which is the index of the edited row.
protected void gvwEmployees_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
GridView.EditIndex = -1;
BindData();
}

Resources