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
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.
Yavor Georgiev
the Telerik team
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
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
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
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
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
Do you have our latest version - Q1 2010 SP2?
Kind regards,Vlad
the Telerik team
thanks for your help
Michael Lutz
BITsoft
With this version you can use DataLoaded to achieve your goal. I've attached an example application.
All the best,Vlad
the Telerik team
best regards,
Michael Lutz
BITsoft
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
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 subYavor Georgiev
the Telerik team
best regards,
Michael Lutz
BITsoft
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 SubVB.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 SubAnd 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
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 IfEnd SubYavor Georgiev
the Telerik team
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
Can you verify using the debugger where exactly this exception is thrown?
Sincerely yours,Vlad
the Telerik team
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
