Skip Navigation LinksHome / Community & Support / Developer Productivity Tools Forums / WPF > GridView > RadGridView with a ComboBox which is binded to a DataTable

Not answered RadGridView with a ComboBox which is binded to a DataTable

Feed from this thread
  • Suresh Krishna avatar

    Posted on Jan 27, 2012 (permalink)

    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
    Attached files

    Reply

  • Pavel Pavlov Pavel Pavlov admin's avatar

    Posted on Jan 27, 2012 (permalink)

    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 >>

    Reply

  • Suresh Krishna avatar

    Posted on Jan 27, 2012 (permalink)

    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,


    Reply

  • Suresh Krishna avatar

    Posted on Jan 28, 2012 (permalink)

    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

    Reply

  • Suresh Krishna avatar

    Posted on Jan 29, 2012 (permalink)

    Anybody have any idea why its happening?

    Reply

  • Pavel Pavlov Pavel Pavlov admin's avatar

    Posted on Jan 30, 2012 (permalink)

    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 >>

    Reply

  • Suresh Krishna avatar

    Posted on Feb 1, 2012 (permalink)

    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

    Reply

  • Pavel Pavlov Pavel Pavlov admin's avatar

    Posted on Feb 6, 2012 (permalink)

    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 >>
    Attached files

    Reply

Back to Top

Skip Navigation LinksHome / Community & Support / Developer Productivity Tools Forums / WPF > GridView > RadGridView with a ComboBox which is binded to a DataTable
Related resources for "RadGridView with a ComboBox which is binded to a DataTable"

WPF Grid Features  |  Documentation  |  Demos  |  Telerik TV  |  Self-Paced Trainer  ]