Access to BoundField value when GridView row is selected.

I have a GridView with BoundField , and  I want to access it value when  GridView row is selected:

            <asp:GridView ID="grvCascadeRulesTemplates" runat="server" AutoGenerateColumns="False" DataKeyNames="CascadeRuleKey" DataSourceID="odsCascadeRulesTemplates">

                <Columns>

                    <asp:CommandField ShowSelectButton="True"

                     SelectText="Show Values">asp:CommandField>

                    <asp:BoundField DataField="CascadeRuleKey" HeaderText="CascadeRuleKey" ReadOnly="True"

                        SortExpression="CascadeRuleKey" />

                    <asp:BoundField DataField="SourceTable" HeaderText="Source Table" SortExpression="SourceTable" />

                Columns>

            asp:GridView>

        Protected Sub grvCascadeRulesTemplates_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles grvCascadeRulesTemplates.SelectedIndexChanged

            DebugHelper.PrintChildren(grvCascadeRulesTemplates.SelectedRow)

 

        End Sub

I have valid SelectedRow , but I didn't find a way to easily get reference to control in the BoundField.

I don't want to hardcode access the individual cells of the GridViewRow object by using the Cells property with numeric (non-mnemonic) index.. I can not  use the FindControl method , because the control doesn't have an ID.
The possible solutions that I found (from some places including here) are:

1. Convert column to template and add an ID to be able use FindControl. Quite simple, but adds more markup code that I actually need.

2. Add my data column to  DataKeyNames - should work, but my data field is not part of primary key, so it is confusing.

3. Search GridView.Columns by my ColumnName to find index of the cell. I preferred it.

DataControlFieldCollection doesn't provide Find method and furtermore the base class DataControlField doesn't have ID or Name property, so I have to search by FooterText , HeaderText  or SortExpression. Alternatively I can cast DataControlField to BoundField and use DataField.

I choose SortExpression as it is less likely to change and should be the same as DataField.

I've included helper functions  CellBySortExpression,ColumnBySortExpression, GetColumnValueBySortExpression into my WebFormsHelper class

 

  • Share This Post:
  • Share on Twitter
  • Share on Facebook
  • Share on Technorati
posted @ Thursday, August 31, 2006 2:13 PM
Print

Comments on this entry:

# re: Access to BoundField value when GridView row is selected.

Left by Ranu Mandan at 7/11/2007 4:45 PM
Gravatar
Your R&D on the above matter have helped me a lot......
Thank you for sharing this Information with newbie like me

# re: Access to BoundField value when GridView row is selected.

Left by Jim R at 8/19/2007 3:32 PM
Gravatar
Thanks! Surprising there isn't an easier way to do this, but it works great.

# re: Access to BoundField value when GridView row is selected.

Left by Chris F at 10/25/2008 10:58 AM
Gravatar
Yes, I'm surprised there's no easier way too, the functions work wonderfully though. Thanks for the excellent tip!

# Manipulation with cells, generated by GridView DataControlField derived classes.

Left by Michael Freidgeim at 10/31/2008 12:27 AM
Gravatar
Sometimes you need to customise DataControlField derived field (e.g. HyperLinkField or ButtonField )to show it differently then default behavior, but you don't want to create template for the field....

# re: Access to BoundField value when GridView row is selected.

Left by Rolf Hendriks at 1/22/2009 11:11 AM
Gravatar
Thanks, this was EXACTLY what I was looking for!

# re: Access to BoundField value when GridView row is selected.

Left by Hassan Mehmood, ISlamabad, PAK at 4/23/2009 1:36 PM
Gravatar
Thanks, GREAT R&D

# re: Access to BoundField value when GridView row is selected.

Left by JoeBilly at 8/13/2009 5:19 AM
Gravatar
Hello guys,

There's a best way :

GridView gridView = ((GridView)sender);

// Get row key (unique)
System.Web.UI.WebControls.DataKey key = gridView.DataKeys[0];
int rowKey = (int)key.Values[e.RowIndex];

// Get row values
IOrderedDictionary fieldValues = new OrderedDictionary();
GridViewRow editedRow = gridView.Rows[e.RowIndex];

foreach (TableCell cell in editedRow.Cells)
{
if (cell is DataControlFieldCell)
((DataControlFieldCell)cell).ContainingField.ExtractValuesFromCell(fieldValues, ((DataControlFieldCell)cell), DataControlRowState.Edit, false);
}

Thens do what you want with IDictionary fieldValues.

The best way is always to work with a Dictionnary because there's a key which is the DataField in most case and the value.

Joe.

# re: Access to BoundField value when GridView row is selected.

Left by JoeBilly at 8/13/2009 5:33 AM
Gravatar
Nevermind its for Edited rows and not selected ones ;)

But the ExtractValuesFromCell should work if used correctly with DataControlRowState.Selected and last parameter to true if the cell does not contains a TextBox.

# re: Access to BoundField value when GridView row is selected.

Left by rajinder singh at 9/28/2009 7:22 PM
Gravatar
hi,,i used a datagrid to show details of hotels with common location and catagory..i want when a user click on button book in each row the corresponding hotelid go in to a session so that i can access it on next page....how can i do it????help me

# re: Access to BoundField value when GridView row is selected.

Left by Doug Leary at 1/2/2010 11:38 AM
Gravatar
Here's an example of how I did it. I wanted to modify the format of 2 gridview columns in the GridView.OnRowDataBound event handler, based on data content. I declared two vars to hold the column indexes of the two cells and then iterated through GridView1.Columns and checked the DataField property. I had to check for the column datatype because the grid contains a Template column and casting it as BoundField threw an error.
protected int iZipCode;
protected int iEmail;
protected void Page_Load(object sender, EventArgs e)
{
for (int i = 0; i < GridView1.Columns.Count; i++)
{
if (GridView1.Columns[i].GetType() == typeof(BoundField))
{
BoundField bound = (BoundField)(GridView1.Columns[i]);
if (bound.DataField == "IsActive")
{
iZipCode = i;
}
else if (bound.DataField == "IsEmailPublic")
{
iEmail = i;
}
}
}
}
...
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
// manipulate e.Row.Cells[iZipCode] and e.Row.Cells[iEmail]
}
}

# re: Access to BoundField value when GridView row is selected.

Left by lkjhs at 6/15/2010 8:33 AM
Gravatar
or

(GridView).SelectedRow.Cells[1].Text

Am I wrong ?

# re: Access to BoundField value when GridView row is selected.

Left by David Rogers at 1/12/2011 12:28 PM
Gravatar
Excellent! I have alway thought the template solution seemed clunky.

# re: Access to BoundField value when GridView row is selected.

Left by Shoaib at 6/30/2011 4:36 AM
Gravatar
thats very useful, thanks alot for sharing this info

# re: Access to BoundField value when GridView row is selected.

Left by John Guthrie at 1/6/2012 4:59 AM
Gravatar
In an ASP.NET page I recently wrote, I needed access to the value of a BoundField. This BoundField held the value of the "ID", which was the primary key and a value assigned to DataKeyNames.

In this particular application I needed to loop through all rows displayed and for those rows which had a value in a textbox field, Update the row in a database table.

Extracting just the code I used to get a reference to the BoundField value for each row:

For Each r As GridViewRow In gvAddExcessInventory.Rows

Dim currentID As Int32 = Convert.ToInt32(gvAddExcessInventory.DataKeys(r.RowIndex).Value)

Next

# re: Access to BoundField value when GridView row is selected.

Left by Michael Freidgeim at 1/6/2012 10:08 AM
Gravatar
John Guthrie,
You approach is good for PRIMARY key.
My data field was not a part of primary key, so more work is required.

Your comment:



(not displayed)


 
 
 
 
 

Live Comment Preview:

 
«February»
SunMonTueWedThuFriSat
2930311234
567891011
12131415161718
19202122232425
26272829123
45678910