Getting the content of a GridView cell on edit

written by Ryan Olshan on Monday, December 19 2005

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

Kick this post on .NET Kicks

Similar Posts

  1. Common DropDownList Issue
  2. GridView Sorting
  3. VB.NET GridView Sorting/Paging w/o a DataSourceControl DataSource

Comments

  • Brian O'Neil on on 1.16.2006 at 7:58 AM

    Brian O'Neil avatar


    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,


    Brian.ONeil@MCDean.com


    Brian.F.ONeil@GMail.Com




  • Darren on on 2.05.2006 at 6:22 PM

    Darren avatar

    Ditto what Brian said.



  • Phil on on 3.03.2006 at 3:31 AM

    Phil avatar

    Amen Brother!



    Preach On It!

  • ScottC on on 5.03.2006 at 4:38 PM

    ScottC avatar

    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;  
      }
     }
    }

  • Ryan Olshan on on 5.03.2006 at 5:17 PM

    Ryan Olshan avatar

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

    Ryan

  • ScottC on on 5.03.2006 at 5:32 PM

    ScottC avatar

    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

Post a comment