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

[Solved] How to add GridViewDataColumn programmatically

2 Answers 384 Views
GridView
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Vassili King
Top achievements
Rank 1
Vassili King asked on 09 Dec 2010, 12:50 AM
Hello,

I'm having some problems trying to add the following DataColumn to the grid programmatically:

<telerik:GridViewDataColumn>
    <telerik:GridViewDataColumn.CellTemplate>
        <DataTemplate>
            <Image Source="Images/vcf_small.jpg" Height="16" 
                 MouseLeftButtonUp="VcardImage_MouseLeftButtonUp" 
                 Tag="{Binding ID}" ToolTipService.ToolTip="Click to open the vCard." 
            />
        </DataTemplate>
    </telerik:GridViewDataColumn.CellTemplate>
</telerik:GridViewDataColumn>

I've tried many ways to convert the layout above into code, but then the app blows up. Here is one of the versions of the code behind:

...
GridViewDataColumn gc;
DataTemplate dt;
dt = new DataTemplate();
dt = (DataTemplate)XamlReader.Load(
                        @"<DataTemplate
                                            xmlns=""
http://schemas.microsoft.com/client/2007"">
                                            <Image Source=""Images/vcf_small.jpg"" Height=""16"" ToolTipService.ToolTip=""Click to open the vCard."" Tag=""{Binding ID}"" MouseLeftButtonUp=""VcardImage_MouseLeftButtonUp""/>
                                          </DataTemplate>"
                      );
gc = new GridViewDataColumn();
gc.CellTemplate = dt;
rgvMainGrid.Columns.Add(gc); //Adding the column to the grid

If I use the code above, the app blows up because of the highlighted MouseLeftButtonUp="...". If I remove it, the column adds up just fine. If i use a Button instead of the Image, I get the same problem. How can I add the column like this to the grid?

Thank you!
-VK.

2 Answers, 1 is accepted

Sort by
0
Timothy
Top achievements
Rank 1
answered on 09 Dec 2010, 01:07 AM
I believe you define the column in code behind and then define the MouseLeftUp Event for the grid seperately in code behind as well.

VB.net example,

Event definition,

Private Sub CheckListItemssDataGrid_MouseLeftButtonUp(ByVal sender As Object, ByVal e As System.Windows.Input.MouseButtonEventArgs) Handles CheckListItemssDataGrid.MouseLeftButtonUp
' Do Something
End Sub

Column Definition,

Dim Task1 As String = EventAdmin.Task1.ToString
                    Dim Task1Column As New CustomColumn()
                    Task1Column.Width = 48
                    Task1Column.UniqueName = "Task1Column"
                    Dim border1 As New Grid()
                    border1.Height = 170
                    border1.Margin = New Thickness(1, 0, -151, 0)
                    Dim textBlock1 As New TextBlock()
                    textBlock1.Text = Task1
                    textBlock1.VerticalAlignment = VerticalAlignment.Bottom
                    textBlock1.HorizontalAlignment = HorizontalAlignment.Left
                    textBlock1.FontSize = 11
                    textBlock1.FontWeight = FontWeights.Normal
                    textBlock1.TextAlignment = System.Windows.TextAlignment.Center
                    textBlock1.UseLayoutRounding = True
                    textBlock1.TextTrimming = System.Windows.TextTrimming.None
                    textBlock1.Margin = New Thickness(9, 0, 0, 0)
                    Dim transform1 As New RotateTransform()
                    transform1.Angle = 270
                    textBlock1.RenderTransform = transform1
                    border1.Children.Add(textBlock1)
                    Task1Column.Header = border1
                    'Task1Column.IsThreeState = True
                    Task1Column.IsSortable = False
                    Task1Column.IsResizable = False
                    Task1Column.IsFilterable = False
                    Task1Column.IsGroupable = False
                    'Task1Column.AutoSelectOnEdit = True
                    'Task1Column.CellTemplate = TryCast(Me.Resources("Task1CBTemplate"), DataTemplate)
                    Task1Column.DataMemberBinding = New System.Windows.Data.Binding("TASK1")
                    Task1Column.DataMemberBinding.Mode = BindingMode.TwoWay
                    ''Dim selector As New ConditionalStyleSelector()
                    ''selector.ColumnToCheck = ConditionalStyle'selector.CellColumn.TASK1
                    ''selector.EventToCheck = SelectorEvent
                    'Task1Column.CellStyleSelector = selector
                    'ToolTipTemplateSelector="{StaticResource selector}" DataFormatString="{}{0:c}" />
                    Me.CheckListItemssDataGrid.Columns.Add(Task1Column)
,
0
Vassili King
Top achievements
Rank 1
answered on 09 Dec 2010, 05:55 PM
I guess I need to clarify something.

I have the grid and all columns in it are generated in code. The grid is working fine and it bounds to my data without a problem. What I'm trying to do now is add yet another column, also in code, with an Image in it that would trigger the VcardImage_MouseLeftButtonUp when somebody clicks on it (on the image).

I already have the method written (private void VcardImage_MouseLeftButtonUp(object sender, System.Windows.Input.MouseButtonEventArgs e) { ...... }) and it works fine if I create the column in xaml. However, when I'm trying to add the column in code instead, the app is breaking. If I remove the MouseLeftButtonUp=""VcardImage_MouseLeftButtonUp"" from my @"<DataTemplate ..., the thing works again just fine (but, of course, the event is not triggered).

I'm thinking the grid breaks during binding. I think that there must be an issue of order of execution... or not. I'm just trying to see if there a way to fix it or if there a better way to add a Button to a column of a grid in code.

Thank you!
Tags
GridView
Asked by
Vassili King
Top achievements
Rank 1
Answers by
Timothy
Top achievements
Rank 1
Vassili King
Top achievements
Rank 1
Share this question
or