Blog ( Of Ryan Olshan)

Recent Posts

News

About Ryan Olshan

LinkedIn Profile

Follow Ryan via twitter

Join StrongTypes - Free Generics Help By Email
Join StrongTypes

Community Credit Winner

View my MVP profile

The posts on this weblog are provided "as is" with no warranties and confer no rights. The opinions expressed herin are my own personal opinions and do not represent my employer's view in any way.

Subscribe to Rolog via My AOL
Subscribe to Rolog via Bloglines


Subscribe to Rolog via Feedburner

Furl Rolog




Subscribe to Rolog via Newsburst


Subscribe to Rolog via Pluck

Add Rolog to Technorati Favorites




Thycotic Secret Server

Receive Email Updates


.NET Websites

  • => StrongTypes.com <=
  • Community Credit
  • DotNetCharity.com
  • DotNetSlackers
  • EasyListBox
  • LearnAsp.com
  • SystemNetMail.com
  • SystemWebMail.com

Blogroll

  • Alex Campbell
  • Carlos Magalhaes
  • Charles Carroll
  • Chris Taylor
  • Darren Neimke
  • David Betz
  • Eric Smith
  • Rob Caron
  • Rob Howard
  • Sam Gentile
  • Scott Guthrie
  • Scott Mitchell
  • Tess
  • Wesner Moise
  • Jason Alexander
  • Bill Ryan
  • Steve Smith
  • Sahil Malik
  • David Silverlight

Other Websites

  • 911 Pigeon Alert
  • Pacific Wildlife Project

Strong Coder Bloggers

  • Dean Fiala
  • Doug Nelson
  • Kevin Jones

Yahoo! Groups

  • => StrongTypes <=
  • AspNet2
  • AspNet20GridView
  • PayPalDevDotNet
  • RubyOnRails
  • VSTS

Getting the content of a GridView cell on edit

Another common question I see on forums and lists is how to get the content of a GridView cell when in Edit mode. The following code demonstrates how to get the text of a cell at index position 3 using an OnEditing event.

VB.NET
Private Sub GridView_RowEditing( ByVal sender As Object , ByVal e As GridViewEditEventArgs)
    Dim m_RowIndex As Integer
   m_RowIndex = e.NewEditIndex

    Dim m_ColumnText As String
   m_ColumnText = GridViewID.Rows(m_RowIndex).Cells(3).Text
End Sub

C#
private void GridView_RowEditing( object sender, GridViewEditEventArgs e)
{
    int m_RowIndex;
   m_RowIndex = e.NewEditIndex;

    string m_ColumnText;
   m_ColumnText = GridViewID.Rows[m_RowIndex].Cells[3].Text;
}

R

Share this post: | bookmark it! | digg it! | reddit! | kick it! | live it!
Posted: Dec 19 2005, 09:01 AM by Ryan Olshan | with 6 comment(s)
Filed under: ,

Comments

Brian O'Neil said:


Thanks for the good examples.

I am finding myself constantly having to back up the heirarchy to get to something about a Grid row, such as a DataKey value. This seems so awkward. In your example, there should be a Row reference provided in the event signature or as part of the Event Args. You should not have to construct that from a point higher in the control tree, as you are.

Am I alone in this?

I want to go from a reference to a row to its cells and its DataKeys. If I am in an event handler, I want references to everything about the row without having to traverse the entire code dom.

Thanks for listening,




# January 16, 2006 7:58 AM

Darren said:

Ditto what Brian said.

# February 5, 2006 6:22 PM

Phil said:

Amen Brother!

Preach On It!
# March 3, 2006 3:31 AM

ScottC said:

Had a similar issue regarding trying to access the data in a  selected row but I don't like things like cell[3] - what is 3 ? So I did some digging and discovered the DataControlFieldCell. I still have to know the name of the header but this is a slight improvement on having to calculate a number. Example is in C# but you should get the picture.

protected void OnSelectedIndexChanged(object sender,EventArgs e)
{
 string Lastname = string.Empty;
 DataControlFieldCell fieldCell;

 foreach (TableCell cell in gvAccounts.SelectedRow.Cells)
 {
  fieldCell = (DataControlFieldCell)cell;
  if (fieldCell.ContainingField.HeaderText.Equals("Lastname"))
  {
   Lastname = cell.Text;  
  }
 }
}

# May 3, 2006 4:38 PM

Ryan Olshan said:

[] indicates an indexer and 3 would indicate a cell at an index of 4 (since the index starts at 0).

Ryan

# May 3, 2006 5:17 PM

ScottC said:

Sorry Ryan I didn't make myself clear. I know that [] indicates an index. What I mean is that a number is not a good way to have to reference something since you have to know that cell[3] represents "Lastname" say. If the backend gets modified to include an extra field in the SQL statement then 3 may no longer be "Lastname" and your app may be broken whereas by using the name of the cell you will be OK until someone changes the name in the database.

Cheers
Scott

# May 3, 2006 5:32 PM