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

RadGridView with a ComboBox which is binded to a DataTable

7 Answers 78 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Suresh Krishna
Top achievements
Rank 1
Suresh Krishna asked on 27 Jan 2012, 07:09 AM
Hi, 

I have a radgridview binded to a data-table. I need to make the first column of this grid Combo-box. I have tried the following and it did not worked out well as the combo-column is not showing the data-table data.
My code behind,
Class MainWindow
 
    Private Colours As String()
    Public Sub New()
 
        ' This call is required by the designer.
        InitializeComponent()
        Colours = New String(1) {}
        Colours(0) = "Yellow"
        Colours(1) = "Green"
 
        ' Add any initialization after the InitializeComponent() call.
        RadGridView1.ItemsSource = BindData()
 
    End Sub
    Public Function BindData()
        Dim dtTemp_Start As New DataTable
        If Not dtTemp_Start.Columns.Count > 0 Then
            Dim column As DataColumn = New DataColumn
            column.DataType = System.Type.GetType("System.Int32")
            With column
                .AutoIncrement = True
                .AutoIncrementSeed = 1
                .ReadOnly = True
                .Unique = True
                .ColumnName = "Auto_ID"
            End With
            dtTemp_Start.Columns.Add(column)
            dtTemp_Start.Columns.Add("tType", GetType(String))
            dtTemp_Start.Columns.Add("Expression", GetType(String))
            Dim sDataRow As DataRow
            sDataRow = dtTemp_Start.NewRow()
            sDataRow(1) = "black"
            sDataRow(2) = "bbk"
            dtTemp_Start.Rows.Add(sDataRow)
            ' dtTemp.Columns.Add("VarCollection", GetType(ObservableCollection(Of SimulationVariableInfo)))
            'dtTemp_Start.Columns.Add("ConditionCollection", GetType(clsCondCollection))
        End If
        Return dtTemp_Start
    End Function
    
    Private Sub RadGridView1_BeginningEdit(ByVal sender As System.Object, ByVal e As Telerik.Windows.Controls.GridViewBeginningEditRoutedEventArgs) Handles RadGridView1.BeginningEdit
        DirectCast(Me.RadGridView1.Columns("tType"), GridViewComboBoxColumn).ItemsSource = Colours
    End Sub
End Class

My XAML,
<telerik:RadGridView IsFilteringAllowed="False" ShowGroupPanel="False" AutoGenerateColumns="false" HorizontalAlignment="Left" Margin="0,67,0,0" Name="RadGridView1" VerticalAlignment="Top" Height="207" Width="503" >
           <telerik:RadGridView.Columns>
               <telerik:GridViewDataColumn Header="ID"  DataMemberBinding="{Binding Auto_ID}" />
               <telerik:GridViewComboBoxColumn  Header="Type"  DataMemberBinding="{Binding tType}"  >
                   
               </telerik:GridViewComboBoxColumn>
                
               <telerik:GridViewDataColumn Header="Expression"  DataMemberBinding="{Binding Expression}" />
           </telerik:RadGridView.Columns>
       </telerik:RadGridView>

I have attached the output I'm getting. 

I need to retain the value selected in the combo. replacing the original content.
 
regards,

SK

7 Answers, 1 is accepted

Sort by
0
Pavel Pavlov
Telerik team
answered on 27 Jan 2012, 04:30 PM
Hello Suresh ,

Please change the Binding in the combo column as follows:
Binding [tType] 

if adding the square brackets does not help let me know so we can investigate the problem further.

Kind regards,
Pavel Pavlov
the Telerik team

Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

0
Suresh Krishna
Top achievements
Rank 1
answered on 27 Jan 2012, 05:34 PM
hi Pavel Pavlov ,

I have tried that, but no change.. Also, one the user selects the combobox item that should be retained in the cell once you deselect the cell.

Help me out with an example.
regards,


0
Suresh Krishna
Top achievements
Rank 1
answered on 28 Jan 2012, 06:22 AM
Hi ,

I have modified the code as shown below,

XAML,
<telerik:GridViewDataColumn Header="ID"  DataMemberBinding="{Binding Auto_ID}" />
               <telerik:GridViewComboBoxColumn  Header="Type"  DataMemberBinding="{Binding [tType]}"  >

Code Behind,

Imports System.Data
Imports Telerik.Windows.Controls
 
Class MainWindow
 
    Private Colours As String()
    Public Sub New()
        Try
 
        
        ' This call is required by the designer.
        InitializeComponent()
        Colours = New String(1) {}
        Colours(0) = "Yellow"
        Colours(1) = "Green"
 
            ' Add any initialization after the InitializeComponent() call.
            RadGridView1.ItemsSource = BindData()
 
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub
    Public Function BindData()
        Dim dtTemp_Start As New DataTable
        If Not dtTemp_Start.Columns.Count > 0 Then
            Dim column As DataColumn = New DataColumn
            column.DataType = System.Type.GetType("System.Int32")
            With column
                .AutoIncrement = True
                .AutoIncrementSeed = 1
                .ReadOnly = True
                .Unique = True
                .ColumnName = "Auto_ID"
            End With
            dtTemp_Start.Columns.Add(column)
            dtTemp_Start.Columns.Add("tType", GetType(String))
            dtTemp_Start.Columns.Add("Expression", GetType(String))
            Dim sDataRow As DataRow
            sDataRow = dtTemp_Start.NewRow()
            sDataRow(1) = "black"
            sDataRow(2) = "bbk"
            dtTemp_Start.Rows.Add(sDataRow)
            ' dtTemp.Columns.Add("VarCollection", GetType(ObservableCollection(Of SimulationVariableInfo)))
            'dtTemp_Start.Columns.Add("ConditionCollection", GetType(clsCondCollection))
        End If
        Return dtTemp_Start
    End Function
    
    Private Sub RadGridView1_BeginningEdit(ByVal sender As System.Object, ByVal e As Telerik.Windows.Controls.GridViewBeginningEditRoutedEventArgs) Handles RadGridView1.BeginningEdit
        DirectCast(Me.RadGridView1.Columns("[tType]"), GridViewComboBoxColumn).ItemsSource = Colours
    End Sub
End Class

Output on initial Run
output1.jpg (attached)
Output after double click of cell
output2.jpg (attached) 
output3.jpg (attached)  


As you can see in the initial run(output1.jpg) the column is blank!!!


regards
0
Suresh Krishna
Top achievements
Rank 1
answered on 30 Jan 2012, 04:52 AM
Anybody have any idea why its happening?
0
Pavel Pavlov
Telerik team
answered on 30 Jan 2012, 03:51 PM
Hi Suresh ,

While observing your code again to search what may be causing the problem , I have just realized that you are setting the ItemsSource for the combo  column in the beginning edit method. I believe this is part of the trouble. 
Is there any specific reason to set it in the BeginningEdit event handler ? The column will need the ItemsSource even when not in edit mode . 

I may also offer an alternative way of assistance  - if you send me a small project with your data and a Gridview , I will setup the combo column for you and get the project back to you .

Greetings,
Pavel Pavlov
the Telerik team

Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

0
Suresh Krishna
Top achievements
Rank 1
answered on 02 Feb 2012, 05:17 AM
hi,

There is no specific reason i moved the binding in to edit. When I have done the binding after the table binding, I had the same result .

Anyway furnishing the code .
Please look in to this and let me know.
XAML File
<Window x:Class="MainWindow"
    Title="MainWindow" Height="350" Width="525" xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation">
    <Grid>
        <telerik:RadGridView IsFilteringAllowed="False" ShowGroupPanel="False" AutoGenerateColumns="false" HorizontalAlignment="Left" Margin="0,67,0,0" Name="RadGridView1" VerticalAlignment="Top" Height="207" Width="503" >
            <telerik:RadGridView.Columns>
                <telerik:GridViewDataColumn Header="ID"  DataMemberBinding="{Binding Auto_ID}" />
                <telerik:GridViewComboBoxColumn  Header="Type"  DataMemberBinding="{Binding [tType]}"  >
                    
                </telerik:GridViewComboBoxColumn>
                 
                <telerik:GridViewDataColumn Header="Expression"  DataMemberBinding="{Binding Expression}" />
            </telerik:RadGridView.Columns>
        </telerik:RadGridView>
    </Grid>
</Window>
Code Behind(Vb .net)
Imports System.Data
Imports Telerik.Windows.Controls
 
Class MainWindow
 
    Private Colours As String()
    Public Sub New()
        Try
 
        
        ' This call is required by the designer.
        InitializeComponent()
        Colours = New String(1) {}
        Colours(0) = "Yellow"
        Colours(1) = "Green"
 
            ' Add any initialization after the InitializeComponent() call.
            RadGridView1.ItemsSource = BindData()
            DirectCast(Me.RadGridView1.Columns("[tType]"), GridViewComboBoxColumn).ItemsSource = Colours
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub
    Public Function BindData()
        Dim dtTemp_Start As New DataTable
        If Not dtTemp_Start.Columns.Count > 0 Then
            Dim column As DataColumn = New DataColumn
            column.DataType = System.Type.GetType("System.Int32")
            With column
                .AutoIncrement = True
                .AutoIncrementSeed = 1
                .ReadOnly = True
                .Unique = True
                .ColumnName = "Auto_ID"
            End With
            dtTemp_Start.Columns.Add(column)
            dtTemp_Start.Columns.Add("[tType]", GetType(String))
            dtTemp_Start.Columns.Add("Expression", GetType(String))
            Dim sDataRow As DataRow
            sDataRow = dtTemp_Start.NewRow()
            sDataRow(1) = "black"
            sDataRow(2) = "bbk"
            dtTemp_Start.Rows.Add(sDataRow)
            ' dtTemp.Columns.Add("VarCollection", GetType(ObservableCollection(Of SimulationVariableInfo)))
            'dtTemp_Start.Columns.Add("ConditionCollection", GetType(clsCondCollection))
        End If
        Return dtTemp_Start
    End Function
    
    'Private Sub RadGridView1_BeginningEdit(ByVal sender As System.Object, ByVal e As Telerik.Windows.Controls.GridViewBeginningEditRoutedEventArgs) Handles RadGridView1.BeginningEdit
 
    'End Sub
End Class
I moved the ComboBoxColumn binding to Init() after the datatable binding
regards,

Suresh
0
Pavel Pavlov
Telerik team
answered on 06 Feb 2012, 03:34 PM
Hello Suresh ,

I have made the fixes neccesary in your code to make things work .  Please find a runnable project attached.


All the best,
Pavel Pavlov
the Telerik team
Sharpen your .NET Ninja skills! Attend Q1 webinar week and get a chance to win a license! Book your seat now >>
Tags
GridView
Asked by
Suresh Krishna
Top achievements
Rank 1
Answers by
Pavel Pavlov
Telerik team
Suresh Krishna
Top achievements
Rank 1
Share this question
or