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

auto filling other column works for edits but not adds

4 Answers 60 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Daniel Sprague
Top achievements
Rank 1
Daniel Sprague asked on 24 Mar 2010, 07:28 PM
Hi Everyone,

In my grid there's 3 columns, "Client Id", "Client first name", "Client last name".  The client first and last name columns are read only.  When the user enters the Client Id the CellValueChange event is fired, which gets the first and last name for the Client Id from the database, then puts the first and last name values in their respective columns. 

Here's the code:

Private Sub gridClients_CellValueChanged(ByVal sender As Object, ByVal e As Telerik.WinControls.UI.GridViewCellEventArgs)

 

 

        Dim column As GridViewDataColumn = TryCast(e.Column, GridViewDataColumn)

 

 


        If
column IsNot Nothing AndAlso column.FieldName = "ClientId" Then

 

                e.Row.Cells(

"vcFirstName").Value = GetClientFirstName(e.Value)

 

                e.Row.Cells(

"vcLastName").Value = GetClientLastName(e.Value)

 

 

        End If

 

 

End Sub


This works great when editing existing records, but not at all for adding new records.  During add new, the event does fire, and does get the correct first/last name values, and the code to put the first/last name value does fire and seems to work, but when looking at the cell value, nothing actually gets there. 

If the new row is committed (by <enter> or clicking out of the row),  then going back and setting the client id, the code works.  But of course, that doesn't fulfill the requirements.  The code needs to work by merely <tabbing> out of the Client Id column on a new row.

Thanks in advance for any and all suggestions and advice.

 

 

4 Answers, 1 is accepted

Sort by
0
Svett
Telerik team
answered on 29 Mar 2010, 12:52 PM
Hello Daniel Sprague,

Thank you for the provided code snippet.

You cannot set the value of the specified cell through the Cells collection of GridViewNewRowInfo directly. You should use DataRowInfo property of the new row to get its data row equivalent that will be added in the rows collection of the grid:
Private Sub radGridView_CellValueChanged(ByVal sender As Object, ByVal e As GridViewCellEventArgs)
    Dim dataColumn As GridViewDataColumn = TryCast(e.Column, GridViewDataColumn)
 
    If dataColumn IsNot Nothing AndAlso dataColumn.UniqueName = "ClientID" Then
        Dim dataRow As GridViewDataRowInfo = Nothing
        Dim newRow As GridViewNewRowInfo = TryCast(e.Row, GridViewNewRowInfo)
 
        If newRow IsNot Nothing Then
            dataRow = TryCast(newRow.DataRowInfo, GridViewDataRowInfo)
        Else
            dataRow = TryCast(e.Row, GridViewDataRowInfo)
        End If
 
        dataRow.Cells("FirstName").Value = "Svetlin"
        dataRow.Cells("LastName").Value = "Ralchev"
    End If
End Sub

If you have further questions feel free to write us back.

Sincerely yours,
Svett
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
0
Daniel Sprague
Top achievements
Rank 1
answered on 29 Mar 2010, 02:12 PM
Thank you Svett.  Your solution was in fact a working solution.  Works likes a charm now.
0
Fabio Masini
Top achievements
Rank 1
answered on 27 Jan 2011, 11:49 AM

Hi,

I have the same Daniel Sprague’s behavior, but your solution doesn’t work for me.

I’m using Telerik WinControls Q3 2010.

 

In the VS editor the instruction: dataRow = TryCast(newRow.DataRowInfo, GridViewDataRowInfo)
give me this error:
‘DataRowInfo’ is not a member of ‘Telerik.WinControls.UI.GridViewNewRowInfo’

I’m sorry, I’m not sure the error message is correctly translate in EN.

The problem is in GridViewNewRowInfo: there isn’t a property DataRowInfo

I update the project from Q1 SP1 2010 to Q3 2010, cause I need to fix another problem.

For this behavior I can’t update the project to production, but I need to do this asap.

Thank you

0
Svett
Telerik team
answered on 01 Feb 2011, 01:25 PM
Hello Fabio Masini,

In Q2 2010 we removed the DataRowInfo property of GridViewNewRowInfo class. The previous code snippet should be modified for Q3 2010:

C#:

private void radGridView1_CellValueChanged(object sender, Telerik.WinControls.UI.GridViewCellEventArgs e)
{
    if (e.Column.Name == "YourColumn")
    {
        e.Row.Cells["AnotherColumn"].Value = 999;
    }
}

Visual Basic:
Private Sub radGridView1_CellValueChanged(ByVal sender As Object, ByVal e As Telerik.WinControls.UI.GridViewCellEventArgs)
    If e.Column.Name = "YourColumn" Then
        e.Row.Cells("AnotherColumn").Value = 999
    End If
End Sub

Kind regards,
Svett
the Telerik team
Q3’10 SP1 of RadControls for WinForms is available for download; also available is the Q1'11 Roadmap for Telerik Windows Forms controls.
Tags
GridView
Asked by
Daniel Sprague
Top achievements
Rank 1
Answers by
Svett
Telerik team
Daniel Sprague
Top achievements
Rank 1
Fabio Masini
Top achievements
Rank 1
Share this question
or