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

Change Column Header Text of Autogenerated Column

18 Answers 439 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Michael
Top achievements
Rank 1
Michael asked on 06 Jul 2010, 12:21 PM
hy everybody,

I can not figure out how to change the column header caption of a column which was autogenerated since the radviewgrid's itemsource property is bound to a datatable. My code as listed below does not do the job.

with me.radgridview1
    .columns(3).HeaderText = "Name"
end with

The headertext still shows the fieldname of the datatable.

Any suggestion is highly appreciated.

best regards,

Michael Lutz
BITsoft

18 Answers, 1 is accepted

Sort by
0
Yavor Georgiev
Telerik team
answered on 06 Jul 2010, 12:31 PM
Hi Michael,

 The property that controls what displays in the header cell of the GridViewColumn is called Header. So it should be column.Header = "Name". GridViewColumn for WPF & Silverlight does not contain a property called HeaderText.

Also, it's better to handle the RadGridView's AutoGeneratingColumn event, which allows you to set the properties of a column as it's being generated, instead of iterating over the Columns collection afterwards.

Regards,
Yavor Georgiev
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
Michael
Top achievements
Rank 1
answered on 06 Jul 2010, 12:56 PM
hello Yavor

thank you for your immediate (!) response. I will check it out in about 1 hour since I am trying to find a solution to a related issue (and I am close to post another question to this forum ;-))

best regards,

Michael Lutz
BITsoft

0
Michael
Top achievements
Rank 1
answered on 06 Jul 2010, 02:31 PM
hy Yavor

Since I am a newbie to WPF and have lots of paradigms on VB6 I am desperately looking for the AutoGeneratedColumn event respectively where this event can be triggered and where I can place my code.

Thanks for another hint from you on this issue already in advance.

best regards,

Michael Lutz
BITsoft
0
Yavor Georgiev
Telerik team
answered on 06 Jul 2010, 02:39 PM
Hi Michael,

 AutoGeneratingColumn is an event on the RadGridView object. It Is triggered every time a new column is generated.

All the best,
Yavor Georgiev
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
Michael
Top achievements
Rank 1
answered on 06 Jul 2010, 03:15 PM
Dear Yavor,

please be patient with an absolute newbie ;-)

I figured out that subject event is fired when generating the column. But my problem is that (due to my paradigm of VB6) I am looking for the best place to listen to this event. Quite a view events pass e as RoutedEventArgs i.e. "Loaded". I tried to build a case structure beeing able to react based on the value of e....but the more I read the more confused I am getting. Do you have a piece of pseude code for me explaining me a little bit more? Thanks in advance,

Michael Lutz
BITsoft
0
Michael
Top achievements
Rank 1
answered on 06 Jul 2010, 03:33 PM
hello Yavor

me again trying out your first investigation

>>The property that controls what displays in the header cell of the GridViewColumn is called Header. So it should be column.Header = "Name". GridViewColumn for WPF & Silverlight does not contain a property called HeaderText.<<

But I can not find a property Header. I.e. RadGridView1 gives mit "Columns(index).HeaderText". I am not using XAML to much but VB.Net code.

any hint is highly appreciated

Michael Lutz (totally confused now)
BITsoft
0
Vlad
Telerik team
answered on 06 Jul 2010, 03:34 PM
Hello Michael,

 Do you have our latest version - Q1 2010 SP2?

Kind regards,
Vlad
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
Michael
Top achievements
Rank 1
answered on 06 Jul 2010, 03:52 PM
no - I have to deal with WPF Q1 2009 - I am afraid that caused some of the confusion. Any way to accomplish my task of changing the column header autogenerated (bound to a datatable)? I know how to achieve it creating the datatable based on a datareader from scratch and manually fill it row by row. But this is what I want to avoid since I like to implement databinding of cellvalues to other UI controls.

thanks for your help

Michael Lutz
BITsoft
0
Vlad
Telerik team
answered on 07 Jul 2010, 08:05 AM
Hello Michael,

 With this version you can use DataLoaded to achieve your goal. I've attached an example application.

All the best,
Vlad
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
Michael
Top achievements
Rank 1
answered on 07 Jul 2010, 08:29 AM
Dear Yavor - I highly appreciate your outstanding support. I will try it out in about 2 hrs and will post the result here. Again - many thanks!

best regards,

Michael Lutz
BITsoft
0
Michael
Top achievements
Rank 1
answered on 07 Jul 2010, 11:57 AM
Dear Yavor

I tried to test your sample application but since some assemblies are missing I could not get it startet. In the xaml-View I am receiving an exception caused by "FrozenColumnsSplitter" (?). However - since I was able to flip through your source I think I figured out how you accomplished the task. Unfortunately I can not get a "VB.NET-Translation" of your C# code for RadGridView_Loaded going. Despite the recommendations of my intellisense I tried almost everything without luck. Maybe I am missing some of the logic. I try to explain with my own words as I understood your solution:

1. In XAML I use the following code:
<telerik:RadGridView ItemSource = "{Binding}" DataLoaded="RadGridView_DataLoaded"/>
The property AutoGenerateColumns is not set, so I assume it is the default setting "true".

2. I am building a DataTable based on a query to an access database. The column for the Name is the field name "Lname".

3. In the Eventhandler respectively Sub I place the below listed code:

private sub RadGridView_DataLoaded(ByVal sender as Object, ByVal e as System.EventArgs) Handles RadGridView.DataLoaded
    ((me.radgridview).sender).columns["Lname"].HeaderText = "Name"    
end sub
Using the typing above shows me a syntax error warning from my IDE adressing the "((" at the beginning of the code line.

What is not clear to me in your sample is where you address the ItemSource of the control respectively where you "glue" DataContext (seems this is an array list) to the RadGridView.

I hope it is understandable what I meant describing the problems I still have. Thanks for your patience.

best regards,

Michael Lutz
BITsoft

0
Accepted
Yavor Georgiev
Telerik team
answered on 07 Jul 2010, 12:12 PM
Hello Michael,

 You should rework your event handler in this way:

private sub RadGridView_DataLoaded(ByVal sender as Object, ByVal e as System.EventArgs) Handles RadGridView.DataLoaded
    DirectCast(sender, RadGridView).Columns("Lname").HeaderText = "Name"   
end sub

Greetings,
Yavor Georgiev
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
Michael
Top achievements
Rank 1
answered on 07 Jul 2010, 01:49 PM
Dear Yavor - a million thanks for your help and patience. It does the job!!!

best regards,

Michael Lutz
BITsoft
0
Michael
Top achievements
Rank 1
answered on 28 Jul 2010, 03:29 PM
Dear Yavor

Sorry for the confusion I am causing now. Please disregard my previous answer where I stated that it does the job. I was working in a sample application and what I saw was the result of my previous try by building the DataTable manually. So trying again to react to the DataLoaded Event is giving me an NullReferenceException error which I translate from German language to English meaning as: "The object reference is not set to an object instance" (German original exception message: "Der Objektverweis wurde nicht auf eine Objektinstanz festgelegt."). Please remember that I am using VB.NET and WPF (most samples are Silverlight and C#). Here is what I did so far:

XAML:
<telerik:RadGridView Name="rgv_All_Kunden" ActionOnLostFocus="CancelEdit" Background="Transparent" BorderThickness="1" BorderBrush="DarkRed" CanUserFreezeColumns="False" CanUserReorderColumns="True" CanUserResizeColumns="True" CanUserSortColumns="False" ColumnsWidthMode="None" DataLoadMode="Synchronous" Focusable="True" Grid.IsSharedSizeScope="False" IsFilteringAllowed="False" IsReadOnly="True" Margin="0" ScrollMode="RealTime" ShowColumnHeaders="True" ShowGroupPanel="False" telerik:StyleManager.Theme="Office_Black" UseAlternateRowStyle="True" ValidationMode="Cell" VerticalGridlinesThickness="1" VerticalGridlinesVisibility="Hidden" Visibility="Visible" Grid.Column="1" Grid.ColumnSpan="5" Grid.Row="5" Grid.RowSpan="9" />

VB.NET - filling the RadGridView
Private Sub init_read_to_rgv_All()
        Dim sCN As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & cApp.DataPath & "Data60.mdb;JET OLEDB:Database Password=keins"
        Dim CN As New OleDbConnection(sCN)
        Dim dt As New DataTable
        'Dim sSQL As String = "Select * from BIN_Main ORDER BY LName ASC, FName ASC"
        Dim sSQL As String = "Select LName, FName, LetzteAenderung, Wiedervorlagetermin, StammGUID from BIN_Main ORDER BY LName ASC, FName ASC"
        Dim t As String = ""
        Try
            CN.Open()
            Dim da As New OleDbDataAdapter(sSQL, CN)
            da.Fill(dt)
            If dt.Rows.Count >= 1 Then                
                With Me.rgv_All_Kunden
                    .AutoGenerateColumns = True
                    .ItemsSource = dt
                End With
            End If
        Catch
            MsgBox(Err.Description)
        End Try
    End Sub

VB.NET - reacting in the DataLoaded Event
Private Sub rgv_All_Kunden_DataLoaded(ByVal sender As Object, ByVal e As System.EventArgs) Handles rgv_All_Kunden.DataLoaded
        Try
            If Not IsNothing(sender) Then
                Select Case sender.name.ToString
                    Case Me.rgv_All_Kunden.Name.ToString
                        DirectCast(sender, RadGridView).Columns("Lname").HeaderText = "Name"
                End Select
            End If
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

And in the sub rgv_All_Kunden_DataLoaded the error occure. Any idea what I am doing wrong here? Please not also that I am not using the latest version of the radgridview. And it is not up to me to decide to update :-(

Thanks for you help in advance,

Michael Lutz
BITsoft
0
Yavor Georgiev
Telerik team
answered on 29 Jul 2010, 12:45 PM
Hello Michael,

 I think it's best if you handled the AutoGeneratingColumn event:

Private Sub RadGridView1_AutoGeneratingColumn(ByVal sender As Object, ByVal e As GridViewAutoGeneratingColumnEventArgs)
    If e.Column.Header = "LName" Then
        e.Column.Header = "Name"
    End If
End Sub

All the best,
Yavor Georgiev
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
Michael
Top achievements
Rank 1
answered on 29 Jul 2010, 02:52 PM
Dear Yavor

I think we had this issue already. Unfortunately I have to stick with WPF Q1 2009 and it does not provide me this event or method. Then you recommeded the DataLoaded method using DirectCast to force the HeaderText to be renamed. However - that is where the exception occurs and that is the point where I restarted this thread.

thanks

Michael Lutz
BITsoft
0
Vlad
Telerik team
answered on 29 Jul 2010, 03:08 PM
Hello Michael,

 Can you verify using the debugger where exactly this exception is thrown?

Sincerely yours,
Vlad
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
Michael
Top achievements
Rank 1
answered on 29 Jul 2010, 03:47 PM
hello Yavor

I found out that addressing the column by its autogenerated name is not working.

But when I use the column index it works.

DirectCast(sender, RadGridView).Columns(0).HeaderText = "Name"

BTW - DirectCast is not needed. Sender.Columns(0).HeaderText = "Name" does the job, too.

best regards and thanks for your help and patience

Michael Lutz
BITsoft
Tags
GridView
Asked by
Michael
Top achievements
Rank 1
Answers by
Yavor Georgiev
Telerik team
Michael
Top achievements
Rank 1
Vlad
Telerik team
Share this question
or