This is a migrated thread and some comments may be shown as answers.

Obtaining the value of a cell in a different column in the current row

4 Answers 277 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Cyprexx Services
Top achievements
Rank 1
Cyprexx Services asked on 09 Dec 2010, 02:31 AM
I have a WPF radGridView populated with data.  There are 3 columns - "ID", "FirstName", and "LastName" for simplicity.

"ID" is a hidden column.  It's only there so I can reference the ID in code behind.

So the Grid is displaying FirstName and LastName.  In the interface, I have code-behind on the CellEditEnded event which is supposed to update the database with any changes in data.  The code is archaic still and I know there are validity checks that have to be added, etc., but my problem is I cannot obtain the ID of the row I'm in.  In this event, e is of type Telerik.Windows.Controls.GridViewCellEditEndedEventArgs

I'm trying to use e.Cell.ParentRow.Cells(0).ToString but that returns the value of the first visible cell.  Moreover, I don't want to use Cells(0), I'd rather use the cell name in case the index value changes - but this only takes index values.  I've tried going back to the grid itself using something like uxGRIDNAME.row(0)... etc but there doesn't seem to be any property that I can use to go to a specific row.  Moreover, "e" doesn't contain a rowindex anyway.  I'm baffled that this is so difficult to figure out - it seems like something that should be so simple.  I'm sure someone has some kind of insight here.  Thank you in advance!

4 Answers, 1 is accepted

Sort by
0
Maya
Telerik team
answered on 09 Dec 2010, 08:30 AM
Hello Jason McAdams,

You may use the Items Collection of the grid. Once you case an item towards your Business Object type, you will be able to get to the ID property and save it:

foreach(Player player in this.playersGrid.Items)
{
    var name = player.Name;
    var country = player.Country;
}

In this case Name and Country are properties of the Player.
 

Best wishes,
Maya
the Telerik team
Browse the videos here>> to help you get started with RadControls for WPF
0
Cyprexx Services
Top achievements
Rank 1
answered on 09 Dec 2010, 05:37 PM
I'm not sure what you mean by any of that honestly.  I can iterate through the items collection of the grid but how do I determine when I'm in the correct row?  I need to get to the row that has contains the cell I'm modifying then go to the "ID" column of that row to obtain the value I need.

I have figured out one way to do it, but I think it's a terrible way to find the call value I need.  I can use the following:

e.cell.ParentRow.DataContext("ID") or
e.cell.ParentRow.DataContext(0)

The problem is, there is implicit conversion here and I have Option Strict turned on.  Therefore I have to do the following (keep in mind I need to return a string):

CStr(DirectCast(e.cell.ParentRow.DataContext, System.Data.DataRowView)("ID")) or
CStr(DirectCast(e.cell.ParentRow.DataContext, System.Data.DataRowView)(0))

This seems to be the only way I can retrieve the data I need.  If there is a simpler way, please let me know as I will be doing this all over in the application.
0
Cyprexx Services
Top achievements
Rank 1
answered on 09 Dec 2010, 05:41 PM
Just for clarification, this is the code I have for the CellEditEnded event right now.  The ADONetActions is a custom DLL that I wrote for my datalayer - just the way I like to work:

Private Sub uxUserMaintenanceGridView_CellEditEnded(ByVal sender As System.Object, ByVal e As Telerik.Windows.Controls.GridViewCellEditEndedEventArgs) Handles uxUserMaintenanceGridView.CellEditEnded
  
        Dim SDB As New DataClass.ADONETActions
  
        SDB.ConnectString = My.Settings.DatabaseConnectionString
        SDB.StoredProcedureName = "procUsersEditUpdate"
        SDB.AddParams("@UserID", CStr(DirectCast(e.Cell.ParentRow.DataContext, System.Data.DataRowView)("ID")))
  
        If e.NewData IsNot DBNull.Value Then
  
            Select Case uxUserMaintenanceGridView.Columns(e.Cell.Column.DisplayIndex).UniqueName
                Case "FirstName"
                    SDB.AddParams("@FirstName", e.NewData.ToString)
                Case "LastName"
                    SDB.AddParams("@LastName", e.NewData.ToString)
                Case "IsNurse"
                    SDB.AddParams("@isNurse", e.NewData.ToString)
            End Select
  
            SDB.AddParams("@UpdateSource", Me.Name)
            SDB.RunAction()
  
        End If
    End Sub
0
Vlad
Telerik team
answered on 09 Dec 2010, 05:43 PM
Hi,

 The standard way for such scenarios in both WPF and Silverlight is MVVM. You should not work with the visual tree directly - instead this prepare desired data model and bing the grid to the model. Can you post more info why you need to use this event instead separate your logic in the data model - for example in property setters?

Greetings,
Vlad
the Telerik team
Browse the videos here>> to help you get started with RadControls for WPF
Tags
GridView
Asked by
Cyprexx Services
Top achievements
Rank 1
Answers by
Maya
Telerik team
Cyprexx Services
Top achievements
Rank 1
Vlad
Telerik team
Share this question
or