Geeks With Blogs

News My Blog has been MOVED to https://mfreidge.wordpress.com
Michael Freidgeim's OLD Blog My Blog has been MOVED to https://mfreidge.wordpress.com

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 on Thursday, August 31, 2006 2:13 PM ASP.NET | Back to top


Comments on this post: Access to BoundField value when GridView row is selected.

# re: Access to BoundField value when GridView row is selected.
Requesting Gravatar...
Your R&D on the above matter have helped me a lot......
Thank you for sharing this Information with newbie like me
Left by Ranu Mandan on Jul 11, 2007 4:45 PM

# re: Access to BoundField value when GridView row is selected.
Requesting Gravatar...
Thanks! Surprising there isn't an easier way to do this, but it works great.
Left by Jim R on Aug 19, 2007 3:32 PM

# re: Access to BoundField value when GridView row is selected.
Requesting Gravatar...
Yes, I'm surprised there's no easier way too, the functions work wonderfully though. Thanks for the excellent tip!
Left by Chris F on Oct 25, 2008 10:58 AM

# Manipulation with cells, generated by GridView DataControlField derived classes.
Requesting 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....
Left by Michael Freidgeim on Oct 31, 2008 12:27 AM

# re: Access to BoundField value when GridView row is selected.
Requesting Gravatar...
Thanks, this was EXACTLY what I was looking for!
Left by Rolf Hendriks on Jan 22, 2009 11:11 AM

# re: Access to BoundField value when GridView row is selected.
Requesting Gravatar...
Thanks, GREAT R&D
Left by Hassan Mehmood, ISlamabad, PAK on Apr 23, 2009 1:36 PM

# re: Access to BoundField value when GridView row is selected.
Requesting 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.
Left by JoeBilly on Aug 13, 2009 5:19 AM

# re: Access to BoundField value when GridView row is selected.
Requesting 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.
Left by JoeBilly on Aug 13, 2009 5:33 AM

# re: Access to BoundField value when GridView row is selected.
Requesting 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
Left by rajinder singh on Sep 28, 2009 7:22 PM

# re: Access to BoundField value when GridView row is selected.
Requesting 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]
}
}
Left by Doug Leary on Jan 02, 2010 11:38 AM

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

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

Am I wrong ?
Left by lkjhs on Jun 15, 2010 8:33 AM

# re: Access to BoundField value when GridView row is selected.
Requesting Gravatar...
Excellent! I have alway thought the template solution seemed clunky.
Left by David Rogers on Jan 12, 2011 12:28 PM

# re: Access to BoundField value when GridView row is selected.
Requesting Gravatar...
thats very useful, thanks alot for sharing this info
Left by Shoaib on Jun 30, 2011 4:36 AM

# re: Access to BoundField value when GridView row is selected.
Requesting 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
Left by John Guthrie on Jan 06, 2012 4:59 AM

# re: Access to BoundField value when GridView row is selected.
Requesting 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.
Left by Michael Freidgeim on Jan 06, 2012 10:08 AM

Your comment:
 (will show your gravatar)


Copyright © Michael Freidgeim | Powered by: GeeksWithBlogs.net