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

 

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

Your comment:



 (will not be displayed)


 
 
 
 
 

Live Comment Preview:

 
«November»
SunMonTueWedThuFriSat
25262728293031
1234567
891011121314
15161718192021
22232425262728
293012345