Get value from current Gridview column on button click

 I am searching for the answer to this, should be easy, but I just can't get it.

I have a Gridview.  I placed a button column on it.  Now each row has a button.  When they click the button I want to grab the value of one of the columns and do some stuff.  I made it a template field with a button, on in the on_click of that button I put:

myString = me.GridView1.SelectedRow.Cells("mycolumn").text

That said it was a nullRefernceException.  I know there is data in that column.  So I tried

 myString = me.GridView1.SelectedRow.Cells(1).text

Got the same thing.

What am I missing?

Thank You 

3
SteveInBeloit
9/7/2007 7:03:33 PM
asp.net.presentation-controls 72751 articles. 3 followers. Follow

2 Replies
3339 Views

Similar Articles

[PageSpeed] 44

Hi, 

You should use the RowCommand event of the GridView Control to get the cell's text.

eg. protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)

{

Response.Write(GridView1.Rows[
Convert.ToInt32(e.CommandArgument)].Cells[1].Text);

}

Hope this helps.


Best Regards,
Raja

MCAD .Net, MCDBA
Please choose "Mark as Answer" for the post that leads you to a solution
-1
raja
9/7/2007 8:01:21 PM

Bind the data item to the button's CommandArgument field something like this:

<asp:TemplateField>
<ItemTemplate>
<asp:Button runat="server" CommandName="DoSomething" CommandArgument='<%#Eval("ID")%>' Text="DoSomething" />
</ItemTemplate>
</asp:TemplateField>

Then in your RowCommand event handler you will have access to the data through the CommandArgument property like this:

protected void grid_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "DoSomething")
{
// Response.Write is an example,
// but data item bound will be passed when clicked
Response.Write(e.CommandArgument); } }

-=JW=-
0
jamezw
9/7/2007 8:03:32 PM
Reply:

Similar Artilces:

using javascript getting value from textbox when edit button is clicked and both controls are present in gridview control
Hi All using javascript I want to get value  from textbox when edit button is clicked and both controls that is texbox and button are present in gridview control .Alok Hi aloksinha83, Please see this post: http://forums.asp.net/t/1069245.aspx Thanks,Qin Dian TangMicrosoft Online Community SupportPlease remember to mark the replies as answers if they help and unmark them if they provide no help....

How to access GridView hidden Column value by clicking a template column button?
Hi: My GridView contains(for simplicity sake) three columns: a hiden template column that contains the DocID (the DataKey), a template column that contains DocTitle and a template button column that contains a button called btnDetails. When the GridView is loaded with data, the user may click the button on a row and obtain the DocID from the hidden column of that row. This seems to be simple enough. However, when the button is clicked, the GridView always returns blank. Any idea why this happens? My code is as follows: ASPX <asp:GridView ID="gvList" runat=&...

Get a Clicked Column Index Integer value for a GridView column using the Sorting Event Handler
I am creating a GridView nested within a UpdatePanel and I got the sorting, paging all down to a sciene using the GridView events handlers. Now, I stumbled to a minor problem that I can't seem to figure out how to go about it. When sorting, I would like the heading color of the clicked sorted column to change (using CSS). I can't seem to dynamically pass the value of the clicked column index to the method. I am using the following reference to change the color of the column but I cannot seem to retreive the value of the clicked index in integer value: GridView1.Columns(trying to...

Get Value from Gridview Button Column
Hi,  They changed the DataGrid control in VS.Net 2003 to the GridView in VS.Net 2005. Not sure how to set/get the value of the Select Button.  <asp:GridView ID="GridView1" runat="server" AllowPaging="True" AutoGenerateColumns="False" DataSourceID="SqlDataSource1"> <Columns> <asp:ButtonField ButtonType="Button" CommandName="SelectAffiliate" Text="Select" /> <asp:BoundField DataField="FullName" HeaderText="Fu...

Getting column value from a gridview control
I have a grid view control and has one datakey and other columns. In my grid view I have an Edit Control and a Delete Control. 1) When I press Edit control, I am getting datakey value as expected. But I am not getting other column name i.e. pdf_filename 2) when I press Delete control, I should get the datakey value. But I am not getting I am using c#  Please help me Hi vmk, Could you be a littlemore elaborate? like how have you bound your grid? 1.Columns autogenerated 2.Using TemplateFields 3.Using BoundFields.Regards,NaveenPlease remember to click Mark as Answer on the pos...

Update GridView Column Content on click of Button which is included as a column in gridview
 I have Gridview wich has 4 columns1.Name of the Person2.Card Amount3.Template Field of TextBox4. Amount After Transfer I want user to enter the amount in the textbox which gets updated as the total amount (Card Amount + Amount Entered in TextBox) in the 4th column of "Amount After Transfer".I hv tried update Panel and Dynamic Populate bt nothing is working coz the textBox doesnt provide any event function in the code behing page.and javascript function doesnt allow me to refer to any control within the gridview.........PLZ HELP ME...............Thanx   You just place a...

Iwant to select a row and column in gridview with select radiobutton. but No checked change event.below put a button .if click that button to seleced row values will select
I want to select a row and column in gridview using radiobutton in gridview . if which radio button is select that row will select and i want that row column values in outside individual textboxes of next page.  but in that only single radiobutton will select and no post back="true" bcz iam not using checked change property. i inserted button below gridview. if i click the that selected row and columns values are stored into textboxes of next page im using this code .but im getting one column in that row. but i want all columns in that selected row so plz repla...

How to get the current row value clicked on a gridview
Hi, I am using gridview with 5 columns. One of the column is a link button. when I click the link button i need to open a new window of different size and display the entire row details in a new page for editing/deleting. How do i get the current row/column value clicked on the gridview Stevenkaitan,You can use the DataKeyNames property on the gridview. This will be your primary key(s)  Then in your Code behind you can do gvYourGridView.DataKeys[rowIndex].ValueRegards. ~Ben Stevenkaitan,You can use the DataKeyNames property on the gridview. This will be your primary key(s...

Get Value from Hidden Column in Gridview control
I have managed to get the value from my hidden field by using the SelectedIndexChanged event and the following line of code:string keyValue = GridView1.SelectedDataKey.Value.ToString()I would however prefer to get access to the hidden column value on the GridView1_RowDataBound event, so that I can then evaluate each row against the hidden columns value and decide whether or not to display the edit button in each row. Can I do this a different way? You can use e.Row.DataItem("field name") in the RowDataBound event -  as long as the field is in your SQL statement doesn't m...

Getting the value of a dropdownlist in a gridview when button is clicked
Hi, I have a gridview with several boundcolumns and two templatecolumns (1 containing a dropdownlist and the other containing a button) as shown in the attached image.   What I want the user to do is select an option from the dropdownlist and click on the "Submit" button which executes a SQL update command to update a value in a table with the value of the dropdownlist selection. What I am unsure of is how to get the value of the dropdownlist so I can use this value in an SQL parameter for the update statement. I am using a GridView1_RowCommand sub when the submit button ...

Unable to get control values after image button click
Hi, All I have trouble with getting control values after I click a 'save' image button. I am doing a editing data process. I use a textbox to display a record data. After I click the 'save' button I want to get the textbox new data. Code in 'save' button click event: listorder = me.txtListOrder.text The error is I am always getting the old data which I displayed in the textbox but not the data I entered later. When I look at the trace Form Collection it shows the new data I entered later. $ContentPlaceHolder1$txtListorder       456 The strange thing ...

How to get gridviews datakeyname value in button click event
Hi I have a gridview and it is binded with database here gridview has datakey assigned,Here i want in button click event i want to retreive datakey column value and  databound column value( <asp:BoundField DataField="ObjID" ) This is my gridview  <asp:GridView ID="grdObjectiveE" runat="server" AutoGenerateColumns="False" DataKeyNames="ObjID" DataSourceID="dsObjectiveE" Width="100%">                    &n...

How can I get the value of a cell in a gridview when a button is clicked
I need to get the value of a cell in my gridview when a button is clicked that is not in the gridview. I tried this, no luck. It does not matter what row because all rows have the same entry in the 33 rd cell. Protected Sub btnGenID_Click(ByVal sender As Object, ByVal e As System.EventArgs)        'Check to see if the previous batch was cleared, if not get the Id from any row.        If Me.gvBatch.Rows.Count > 0 Then            Me.txtBatchID.Text = Me.gvBa...

How do you get a column value from a gridview column
How do you get a column value from a gridview column in asp.net 3.5? I tried this but it does not work.Dim myRow As GridViewRowFor Each myRow In GridView1.Rows    Dim strTest As String = DataBinder.Eval(myRow.DataItem, "EmailSubject")Nextif I change the column to a Template field then EmailSubject = myRow.FindControl("EmailSubject") works but I was hoping for a more direct way of getting a column value EmailSubject = myRow.Cells(4).Text also works but is not flexible if I change the column orderThanks Craig Try: Dim strTest As String = CType(...

Web resources about - Get value from current Gridview column on button click - asp.net.presentation-controls

GridView SelectedItem Binding Gotcha in WinRT
Last night when I was doing some WinRT hacking I was trying to setup databinding to the selected item in the GridView control and I ran into ...

CNW Group
Home Services Beyond the Wire About CNW Financials Webcasts Events My CNW All News Releases Advanced Search Gridview Latest News Releases All ...

CNW Group
... Services Beyond the Wire About CNW my CNW CNW Access CNW Directories Financials Webcasts Events my CNW All News Releases Advanced Search Gridview ...

CNW Group
Home Services Beyond the Wire About CNW Financials Webcasts Events My CNW All News Releases Add a Filter Gridview Latest News Releases All Today ...

New CmdLets of Powershell 2.0
New CmdLets of Powershell 2.0

Price Drop: Softbox
... objects- create product reflection- changing the background with something more interestingUse it as:- softbox- honeycomb softbox- gridview ...

Profile.aspx: ASP Alliance
.NET,ASP,ASP.NET,SQL,XML,HTML,ADO,ADO.NET,JavaScript,JScript,CSharp,VBScript and VB tutorials from AspAlliance.

Quick Answers - CodeProject
Questions - Free source code and tutorials for Software developers and Architects.; Updated: 25 Oct 2012

DotNetKicks.com - Upcoming stories
DotNetKicks.com is a community based news site edited by our members. It specialises in .NET development techniques, technologies and tools including ...

View Features - ExtendASP
Call 847-681-9827 or Request a Demo View Features Testimonials Pricing About Us Coming Soon ExtendASP exposes every feature, more than any other ...

Resources last updated: 2/4/2016 10:37:00 PM