-
Mar 9th, 2019, 03:44 PM
#1
Thread Starter
Fanatic Member
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.
-
Mar 9th, 2019, 08:15 PM
#2
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.
-
Mar 11th, 2019, 02:22 AM
#3
Thread Starter
Fanatic Member
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.
-
Mar 12th, 2019, 06:15 AM
#4
Thread Starter
Fanatic Member
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
-
Mar 13th, 2019, 12:38 PM
#5
Thread Starter
Fanatic Member
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.
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.
-
Mar 13th, 2019, 02:27 PM
#6
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.
-
Mar 14th, 2019, 03:34 AM
#7
Thread Starter
Fanatic Member
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
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.
-
Nov 17th, 2020, 01:06 AM
#8
New Member
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
-
Nov 17th, 2020, 04:50 PM
#9
Re: Adding combobox to a datagridview that is bond to a datatable
Originally Posted by info@geoadvanced.com
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
-
Nov 17th, 2020, 08:37 PM
#10
New Member
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.
-
Nov 17th, 2020, 10:02 PM
#11
Re: Adding combobox to a datagridview that is bond to a datatable
Originally Posted by info@geoadvanced.com
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.
-
Nov 18th, 2020, 03:31 AM
#12
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|