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

Combobox Column in Grid bound to generic list?

5 Answers 297 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Jonathan Hylton
Top achievements
Rank 1
Jonathan Hylton asked on 05 Jan 2011, 02:14 PM
Hi There,

I have a radgrid that I am using that i have created via the editor and is set like this:

Col0 = Checkbox = (Set to false for each row)
Col1 = textbox = Filled with info from SQL (not bound in gui, done programatically at load time using sqldatareader)
Col2 = textbox = Filled with info from SQL (not bound in gui, done programatically at load time using sqldatareader)
Col3 = combox = filled with generic list - all cells need to have same value example (1,2,3,4,5,6,7,8,9)
col4 = textbox = left blank user will edit this
col5 = textbox = left blank user will edit this

I need to be able to bind the pre created Col3 to a list that has the values already specified. I have tried using radgridview1.columns(3).... however there is no option to for datasource as an option when using an already created combobox for this.

is there an easy way to achieve this or will i have to create Col0 - Col2 in the designer, fill these columns with values from the SQL database then come back and create the new Col3 in order to bind this to a seperate source for the generic list and then create Col4 - Col5.

any help would be greatly appreciated as I used to use normal datagrid in VS and this was rather easy to do via the column editor for that, however i would rather use radgrid in my project as all other grids i use are radgrid and i really would like to have the ability to use the row reorder possibility that i get with radgrid that i don't get with the normal gridview of VS.

thanks!

Jonathan

5 Answers, 1 is accepted

Sort by
0
Accepted
Richard Slade
Top achievements
Rank 2
answered on 05 Jan 2011, 02:59 PM
Hello,

The example below shows a simple RadGridView that has the same columns you have mentioned. I then add a datasource for the ComboBoxColumn and populate the grid with rows, simulating getting the items from a database in a reader. You can see that the item that is being brought out for the value of the combo box is the selected item. Please try this in a new project. Its just a RadGridView on a form.
Hope this helps, but if you need more assistance, just let me know
Public Class Form1
  
  
    Private Sub Form1_Load(ByVal sender As System.Object,
                           ByVal e As System.EventArgs) Handles MyBase.Load
  
        ' create some columns
        Me.RadGridView1.Columns.Add(New GridViewCheckBoxColumn("MyCheckBox"))
        Me.RadGridView1.Columns.Add(New GridViewTextBoxColumn("MyTextBox1"))
        Me.RadGridView1.Columns.Add(New GridViewTextBoxColumn("MyTextBox2"))
        Me.RadGridView1.Columns.Add(New GridViewComboBoxColumn("MyComboBox"))
        Me.RadGridView1.Columns.Add(New GridViewTextBoxColumn("MyTextBoxUser1"))
        Me.RadGridView1.Columns.Add(New GridViewTextBoxColumn("MyTextBoxUser2"))
  
        ' create a data source for the combo box
        Dim list As New List(Of MyObject)
        list.Add(New MyObject("Item 1", 1))
        list.Add(New MyObject("Item 2", 2))
        list.Add(New MyObject("Item 3", 3))
        list.Add(New MyObject("Item 4", 4))
  
        ' give the column the data source
        CType(Me.RadGridView1.Columns("MyComboBox"), GridViewComboBoxColumn).DataSource = list
        CType(Me.RadGridView1.Columns("MyComboBox"), GridViewComboBoxColumn).DisplayMember = "Name"
        CType(Me.RadGridView1.Columns("MyComboBox"), GridViewComboBoxColumn).ValueMember = "Id"
  
        ' populate the grid
        Me.Populate()
    End Sub
  
    Private Sub Populate()
        'Simulate getting data from SQL via a reader
        For i As Integer = 1 To 4
            Dim rowInfo As GridViewRowInfo = Me.RadGridView1.Rows.AddNew()
            rowInfo.Cells("MyCheckBox").Value = False
            rowInfo.Cells("MyTextBox1").Value = "MyTextBox1 Value " & i.ToString()
            rowInfo.Cells("MyTextBox2").Value = "MyTextBox2 Value " & i.ToString()
            rowInfo.Cells("MyComboBox").Value = i ' The selected value 
            rowInfo.Cells("MyTextBoxUser1").Value = ""
            rowInfo.Cells("MyTextBoxUser2").Value = ""
        Next i
    End Sub
  
End Class
  
Public Class MyObject
    Public Sub New(ByVal name As String, ByVal id As Integer)
        Me.Name = name
        Me.Id = id
    End Sub
  
    Public Property Name As String
    Public Property Id As Integer
End Class

Best regards,
Richard
0
Jonathan Hylton
Top achievements
Rank 1
answered on 05 Jan 2011, 09:46 PM
Hi Richard,

I just had a chance to work on this aspect and this is now working! Many thanks for taking the time to put together this example for me!

thanks!

Jonathan
0
Richard Slade
Top achievements
Rank 2
answered on 05 Jan 2011, 09:55 PM
You're welcome. Glad that you have it all working now.
All the best
Richard
0
Brennan
Top achievements
Rank 1
answered on 06 May 2011, 09:16 PM
What if each row should have it's combobox data item's be unique based on the value in a different column of the row? (Contents of each combobox to be different)
0
Alexander
Telerik team
answered on 11 May 2011, 09:11 AM
Hello Brennan,

Please review this Knowledge Base article which describes how to create RadDropDownListEditor which depends on the value of another cell. Let me know if it helps in your scenario.

Best regards,
Alexander
the Telerik team
Q1’11 SP1 of RadControls for WinForms is available for download; also available is the Q2'11 Roadmap for Telerik Windows Forms controls.
Tags
GridView
Asked by
Jonathan Hylton
Top achievements
Rank 1
Answers by
Richard Slade
Top achievements
Rank 2
Jonathan Hylton
Top achievements
Rank 1
Brennan
Top achievements
Rank 1
Alexander
Telerik team
Share this question
or