Results 1 to 12 of 12

Thread: Adding combobox to a datagridview that is bond to a datatable

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2018
    Posts
    514

    Adding combobox to a datagridview that is bond to a datatable

    Hi
    New challenge.
    I have a datagridview that is bound to a datatable. Congratulation �� Now, I need that
    1- one of the columns be a combobox that has two items and
    2- If the content of the row of that column (in datatable or in datagridview before it is replaced by combobox) is equal to one of the two values, then that value is the text that the combobox is showing. And
    3- If the content is not equal to any of the two values, then the text is “”.


    Thanks in advance for your help.
    Last edited by Grand; Mar 9th, 2019 at 03:48 PM.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: Adding combobox to a datagridview that is bond to a datatable

    Follow the CodeBank link in my signature below and check out my thread on the specific topic of a ComboBox in a DataGridView.

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2018
    Posts
    514

    Re: Adding combobox to a datagridview that is bond to a datatable

    Thank you. I have some difficulty with this regard whcih I have explained in your code bank. Will you please follow up on that? Thanks.

  4. #4

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2018
    Posts
    514

    Re: Adding combobox to a datagridview that is bond to a datatable

    Ok, I have tried and can't get it to work. In my case the datagridviews (DGV) are added at run time and contain a column with some value in it and sometimes no value. The idea is to give users possibilities to change and/or select two different values and then save (the content will be saved on a SQL database: out of scope).
    Do I need to add a column and bind a column of my table (the column of interest) to it and then add that column to my datagridview, or replace it with the column of interest? How exactly can I do that?

    This is how I add my DGVs:

    Code:
    'add new datagrid
                Dim MyGroupGrid As New DataGridView
                MyGroupGrid.Name = "DGV_" & MyTabPageName
                EnableDoubleBuffered(MyGroupGrid)
                With MyGroupGrid
                    .Dock = DockStyle.Fill
                    .RowHeadersVisible = False
                    .ReadOnly = True
                    .AllowUserToAddRows = False
                    .AllowUserToResizeColumns = False
                    .AllowUserToResizeRows = False
                    .DataSource = TempTable
                    .ClearSelection()
                End With
                ThisTab.Controls.Add(MyGroupGrid)
    How will I add/replace that column with combobox?
    Code:
                Dim CCB As New DataGridViewComboBoxColumn()
                With CCB
                    .HeaderText = "IntOrExt"
                    .Name = "Combo box name"
                    .DataSource = TempTable
                    .DataPropertyName = "IntOrExt"
                    .DisplayMember = "IntOrExt"
                    .ValueMember = "IntOrExt"
                End With
    
                DGV_MyGroupGrid.Columns.Add(CCB)
    However, each combobox contains the entire column and also takes a lots of time to open the dropdown:

    Attachment 166449

  5. #5

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2018
    Posts
    514

    Re: Adding combobox to a datagridview that is bond to a datatable

    I have now managed to get most of the things right. However, there are two problems:
    1- I need to click twice (not double click) to get the combo to drop. It does not response to the first click.
    2- comboboxes appear grayed out.

    Name:  2019-03-13_18-09-35.jpg
Views: 1599
Size:  15.2 KB

    Code:
    With DGV_DVTracker
                    .Name = "DGV_" & MyTabPageName
                    .Dock = DockStyle.Fill
                    .RowHeadersVisible = False
                    .ReadOnly = False
                    .AllowUserToAddRows = False
                    .AllowUserToResizeColumns = False
                    .AllowUserToResizeRows = False
                    .ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.DisableResizing
                    .RowHeadersWidthSizeMode = DataGridViewRowHeadersWidthSizeMode.DisableResizing
                    .ColumnHeadersHeight = 50
                    .EnableHeadersVisualStyles = False
                    .ColumnHeadersDefaultCellStyle.ForeColor = Color.White
                    .ColumnHeadersDefaultCellStyle.BackColor = Color.Blue
                    .RowHeadersDefaultCellStyle.BackColor = Color.Black
                    .ColumnHeadersDefaultCellStyle.Font = New Font(DGV_DVTracker.Font, FontStyle.Bold)
                    .DataSource = TempTable ' adding the datasource
                    .ClearSelection()
                End With
    
                'add a combobox column
                Dim CCB As DataGridViewComboBoxColumn = New DataGridViewComboBoxColumn()
                Dim items() As String = {"", "Int.", "Ext."}
                With CCB
                    .HeaderText = "IntOrExt"
                    .Name = "CBB_DV"
                    .DataSource = items
                End With
    
                DGV_DVTracker.Columns.Add(CCB)
    
                For i As Integer = 0 To DGV_DVTracker.Rows.Count - 1
                    DGV_DVTracker.Rows(i).Cells("CBB_DV").Value = DGV_DVTracker.Rows(i).Cells("IntOrExt").Value
                Next
                'Replace the columns
                Dim CRemoveIndex As Integer = DGV_DVTracker.Columns("IntOrExt").Index
                With DGV_DVTracker
                    .AutoGenerateColumns = False
                    .Columns.Remove("IntOrExt")
                    .AllowUserToOrderColumns = True
                    .Columns("CBB_DV").DisplayIndex = CRemoveIndex
                    .AllowUserToOrderColumns = False
                End With
    What is it I am doing wrong or not doing?

    Thank again.

  6. #6
    Fanatic Member kpmc's Avatar
    Join Date
    Sep 2017
    Posts
    1,012

    Re: Adding combobox to a datagridview that is bond to a datatable

    1- I need to click twice (not double click) to get the combo to drop. It does not response to the first click.
    2- comboboxes appear grayed out.
    if you put the dgv edit mode into "EditOnEnter" It should solve problem 1

    Problem 2 is beacuse of the cmb style. When set to .DropDownButton it becomes similar to Read Only and appears to at least inherit readonly display/draw properties. Try setting the display style to Combobox

    Code:
            With MyDgv
                .AutoGenerateColumns = False 'this should precede using the Fill/Load method 
                .Columns.Add(New DataGridViewComboBoxColumn With {
                    .DataPropertyName = "ColumnName",
                    .HeaderText = "Whatever",
                    .DisplayStyle = DataGridViewComboBoxDisplayStyle.ComboBox})
            End With
    Also I am confused by what youre doing at the end there. Why the 2 with blocks? And in the second block .AutoGenerateCOlumns setting is doing nothing. That setting is generally the first setting prior to Fill/Load methods that will otherwise build your columns collection automatically.

  7. #7

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2018
    Posts
    514

    Re: Adding combobox to a datagridview that is bond to a datatable

    Thank you for your help.
    The EditOnEnter mode solve the issue partially as it now response to the first click if one clicks on the arrow of the combobox. If one clicks on the text field of the combo it still does not respond to the first click. It is OK though.

    I applied this and they look much better.
    Code:
    .DisplayStyle = DataGridViewComboBoxDisplayStyle.ComboBox
    Name:  2019-03-14_09-29-07.jpg
Views: 1635
Size:  30.1 KB

    In the end, I am replacing one of the DGVs column with the column that I have added which has combobox. Regarding with blocks; when things start to work, I will clean up. Thank you for pointing that out.

  8. #8
    New Member
    Join Date
    Nov 2020
    Posts
    2

    Re: Adding combobox to a datagridview that is bond to a datatable

    Hi, Grand,

    I want to do the same thing: add combobox to my datatable. I need three types of columns: string, checkbox and combobox. I'm able to add string and checkbox column but unsuccessful in combobox. I tried to incorporate your code. Unfortunately, I can't get it.

    Could you please help?

    Thank you.


    Attachment 179295Attachment 179296

    Code:
       Sub AddTableBorColumnHead(ByVal dgv As DataGridView, ByVal dt As DataTable, ByVal ColHeader As String(), ByVal tip As String(), ByVal SourceData As String())
            Dim chkBox As System.Type = System.Type.GetType("System.Boolean")
            Dim CCB As DataGridViewComboBoxColumn = New DataGridViewComboBoxColumn()
    
            With dgv
                For i As Integer = 0 To UBound(ColHeader)
                    Dim ColHdr As String = Trim(ColHeader(i))
                    Select Case ColHdr
                        Case "Fill", "Bulk"
                            dt.Columns.Add(ColHdr, chkBox)
                            '                        .Columns.Add(ColHdr, chkBox)
                            '                    Case "USCS"
                            ' trying to add a ComboBox column here
                            '                       dt.Columns.Add(ColHdr, GetType(DataGridViewComboBoxColumn))
                        Case Else
                            dt.Columns.Add(ColHdr, GetType(String))
                    End Select
                Next
                dt.Rows.Add()
    
                .DataSource = dt                   ' set DataTable for Boring Summary Page
                For i As Integer = 0 To .Columns.Count - 1
                    .Columns(i).ToolTipText = tip(i)
                    .Columns(i).CellTemplate.Style.Alignment = DataGridViewContentAlignment.MiddleCenter
                Next
                .ColumnHeadersHeight = 22
                .AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.DisplayedCells
                .ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
                .RowTemplate.Height = 22
                .RowHeadersVisible = True
                .ScrollBars = ScrollBars.Both
            End With
    
        End Sub

  9. #9
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,684

    Re: Adding combobox to a datagridview that is bond to a datatable

    Quote Originally Posted by info@geoadvanced.com View Post
    Hi, Grand,

    I want to do the same thing: add combobox to my datatable. I need three types of columns: string, checkbox and combobox. I'm able to add string and checkbox column but unsuccessful in combobox. I tried to incorporate your code. Unfortunately, I can't get it.

    Could you please help?

    Thank you.


    Attachment 179295Attachment 179296

    Code:
       Sub AddTableBorColumnHead(ByVal dgv As DataGridView, ByVal dt As DataTable, ByVal ColHeader As String(), ByVal tip As String(), ByVal SourceData As String())
            Dim chkBox As System.Type = System.Type.GetType("System.Boolean")
            Dim CCB As DataGridViewComboBoxColumn = New DataGridViewComboBoxColumn()
    
            With dgv
                For i As Integer = 0 To UBound(ColHeader)
                    Dim ColHdr As String = Trim(ColHeader(i))
                    Select Case ColHdr
                        Case "Fill", "Bulk"
                            dt.Columns.Add(ColHdr, chkBox)
                            '                        .Columns.Add(ColHdr, chkBox)
                            '                    Case "USCS"
                            ' trying to add a ComboBox column here
                            '                       dt.Columns.Add(ColHdr, GetType(DataGridViewComboBoxColumn))
                        Case Else
                            dt.Columns.Add(ColHdr, GetType(String))
                    End Select
                Next
                dt.Rows.Add()
    
                .DataSource = dt                   ' set DataTable for Boring Summary Page
                For i As Integer = 0 To .Columns.Count - 1
                    .Columns(i).ToolTipText = tip(i)
                    .Columns(i).CellTemplate.Style.Alignment = DataGridViewContentAlignment.MiddleCenter
                Next
                .ColumnHeadersHeight = 22
                .AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.DisplayedCells
                .ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
                .RowTemplate.Height = 22
                .RowHeadersVisible = True
                .ScrollBars = ScrollBars.Both
            End With
    
        End Sub
    See the following.

    https://github.com/karenpayneoregon/...ViewComboBoxes

    Name:  1111.jpg
Views: 1624
Size:  37.9 KB

  10. #10
    New Member
    Join Date
    Nov 2020
    Posts
    2

    Re: Adding combobox to a datagridview that is bond to a datatable

    Thank you, Karen.

    I checked your posts on github. Unfortunately I got totally lost. I'm not an IT guy but a civil engineer. I only learned Visual Basic by myself. I wonder whether you can help me to solve this problem with payable service.

    Thank you.

  11. #11
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,684

    Re: Adding combobox to a datagridview that is bond to a datatable

    Quote Originally Posted by info@geoadvanced.com View Post
    Thank you, Karen.

    I checked your posts on github. Unfortunately I got totally lost. I'm not an IT guy but a civil engineer. I only learned Visual Basic by myself. I wonder whether you can help me to solve this problem with payable service.

    Thank you.
    These forums are not about payable services but instead to assist developer. The code I suggested for a non programmer is a challenge but that is what developers do, work through task and many times will be a challenge.

  12. #12
    Super Moderator FunkyDexter's Avatar
    Join Date
    Apr 2005
    Location
    An obscure body in the SK system. The inhabitants call it Earth
    Posts
    7,900

    Re: Adding combobox to a datagridview that is bond to a datatable

    Hi Info

    I've also sent this as a personal message to you but, as a new user you may not have worked out the pm system yet so I'll post it here.

    Having your email address as a username really isn't a good idea. Various spam bots tend to troll forums looking for email addresses and will start flooding you with junk mail. For that reason we'd like to change it to something more suitable for you. We don't want to do that without communicating first, though, because it would prevent you from logging on.

    Could you so one of the following:-
    1. Set up a new account and let us know. You can then continue using the new user name and we will ban the old one and remove the posts. (N.b. this ban would not act as a black mark against you)
    2. Get in touch and let us know a more suitable name you would like to use. We will then get the admins to change it. The next time you log on after the change you would need to use the new user name (there would be a short delay while the admins processed it).

    Cheers and welcome to the forum
    FD


    PS, you can ask people to do work for you paid through this forum through the "Open Positions" sub forum. But, as Karen says, it's not really our focus and I'm not sure how much luck you'll have. You'd probably so better posting to one of the sites that are out there where people advertise for small programming jobs. I don't know the names of any but I know they're out there and we have members here who use them to find work.

    Even better, you could be brave and take a go at it yourself. We'll help you for free but it will require some effort on your part and a bit of a learning curve.
    Last edited by FunkyDexter; Nov 18th, 2020 at 03:36 AM.
    The best argument against democracy is a five minute conversation with the average voter - Winston Churchill

    Hadoop actually sounds more like the way they greet each other in Yorkshire - Inferrd

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width