6 Replies - 145 Views - Last Post: 13 March 2019 - 10:23 AM Rate Topic: -----

#1 Grander   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 25
  • Joined: 15-November 18

Adding a ComboBox Column to a DataGridView

Posted 13 March 2019 - 08:41 AM

Hi
I need help with the following:

I have a datagridviews that is added at run time (it is bound to a datatable) and contains a specific column (called IntOrExt) with value in. The idea is to give users possibilities to change and/or select 2 different values (including "") and then save (the content will be saved on a SQL database: out of scope). What I need is that to remove that column and add a combobox column instead. And that the combobox as selected item for every row is showing the value of that removed column. The Column that is removed has 3 values “Int”, “Ext” and “”.

Thanks in advance for help.

Is This A Good Question/Topic? 0
  • +

Replies To: Adding a ComboBox Column to a DataGridView

#2 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 14854
  • View blog
  • Posts: 59,272
  • Joined: 12-June 08

Re: Adding a ComboBox Column to a DataGridView

Posted 13 March 2019 - 08:44 AM

Quote

datagridviews that is added at run time

Odd - why is a grid being added at run time and not laid out in a designer?


Quote

What I need is that to remove that column and add a combobox column instead.


Okay - what have you tried, thought about, or considered?
Was This Post Helpful? 0
  • +
  • -

#3 Grander   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 25
  • Joined: 15-November 18

Re: Adding a ComboBox Column to a DataGridView

Posted 13 March 2019 - 09:25 AM

It seems odd to me too :) But the application runs based on user settings and users have some choices, a few of those choices will result into a new tab that has a datagridview on it.

This is what I have tried:

  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 = "Combo box name"
                '.Items.AddRange("Int.", "Ext.", "")
                '.DataSource = items
                ' .DisplayStyle = DataGridViewComboBoxDisplayStyle.Nothing
                .DataSource = TempTable
                .DataPropertyName = "IntOrExt"
                .DisplayMember = "IntOrExt"
                .ValueMember = "IntOrExt"
            End With

            DGV_DVTracker.Columns.Add(CCB)



This results into an additional column (in my DGV) that is a combobox but the content of the combobox is the entire (IntOrExt) column and not just the two values which are “Int.” and “Ext.”. So when I click on the combobox a long (as long as there are rows in that column) apears. I assume that is what it supposed to do with the “code” that I provided; but I meant to see something else.

This post has been edited by Grander: 13 March 2019 - 09:29 AM

Was This Post Helpful? 0
  • +
  • -

#4 andrewsw   User is offline

  • Stealth IT
  • member icon

Reputation: 6737
  • View blog
  • Posts: 27,741
  • Joined: 12-December 12

Re: Adding a ComboBox Column to a DataGridView

Posted 13 March 2019 - 09:30 AM

You need to configure a DataSource for the combobox, presumably you have a database table that holds the drowndown items?
You cannot just reuse TempTable, that is why you see all the values.
Was This Post Helpful? 0
  • +
  • -

#5 Grander   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 25
  • Joined: 15-November 18

Re: Adding a ComboBox Column to a DataGridView

Posted 13 March 2019 - 09:39 AM

OK. Isn't it enough to have this:

Dim CCB As DataGridViewComboBoxColumn = New DataGridViewComboBoxColumn()
             Dim items() As String = {" ", "Int.", "Ext."}
            With CCB
                .HeaderText = "IntOrExt"
               .DataSource = items

            End With




But note that in addition to this I want taht each of the comboboxes show the value that matches the column that is bening removed. Do I need to loop through?
Was This Post Helpful? 0
  • +
  • -

#6 andrewsw   User is offline

  • Stealth IT
  • member icon

Reputation: 6737
  • View blog
  • Posts: 27,741
  • Joined: 12-December 12

Re: Adding a ComboBox Column to a DataGridView

Posted 13 March 2019 - 10:22 AM

You don't need a loop but you need to find a tutorial.
Was This Post Helpful? 0
  • +
  • -

#7 Grander   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 25
  • Joined: 15-November 18

Re: Adding a ComboBox Column to a DataGridView

Posted 13 March 2019 - 10:23 AM

I looped anyways. I usually first use violence and if it does not work I read the manual.

Thank you for the fast response and pointing to the right direction.
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 eh combo to drop. It does not response to the first click.
2-comboboxes appear grayed out.

How do I add image to this post?


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.

This post has been edited by Grander: 13 March 2019 - 10:30 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1