locked
Text box to combo box in winforms RRS feed

  • Question

  • Hello all , 

    I have a winform connect to Access Db as data source ,

    1. but the Fields Namely "Condition of Dies"(1-7) are actually combo boxes in my DB (taking only "ok , nok" from drop down list) , but here(in my form) it coming as text box taking any value , how to change this?
    2. And also i have two date fields namely "calibrated on" and "next calibration due" as  "dd-mmm-yy" in my db , how to format the same in the fields of my form and data grid box too?

    Sorry cannot attach the image as the website not allowing till verification.

    Tuesday, July 21, 2020 6:06 AM

Answers

  • Hi

    An alternative idea.

    Since you start by saying you have a Winform, then I am assuming you meant DataGridView rather than a Data Grid.  If that is the case, then the following code may help.

    It defines a Combobox to contain 2 items "OK" and "NOK", and, when a cell is clicked in a column (1 or more, set by you), rhen the User is presented with rge list to choose from (the list can be anything and any number of items). If the cell already has a value then it is shown as the current item in the ComboBox, otherwise, it shows a blank (with the list shown below)

    This is a stand alone example just to show the idea. Needs a DataGridView1 named DGV (with a few Columns added to it), a Combobox1 named CB in the Designer on Form1.
    Option Strict On
    Option Explicit On
    Public Class Form1
    	Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    		With CB
    			.Items.AddRange({"OK", "NOK"})
    			.AutoCompleteMode = AutoCompleteMode.SuggestAppend
    			.AutoCompleteSource = AutoCompleteSource.ListItems
    		End With
    
    		' only for example
    		' set some random values
    		' in Column2
    		With DGV
    			Dim r As New Random
    			For i As Integer = 0 To 9
    				.Rows.Add(Nothing, IIf(r.Next(0, 2) = 1, "OK", "NOK"))
    			Next
    		End With
    	End Sub
    	Private Sub DGV_CellClick(sender As Object, e As DataGridViewCellEventArgs) Handles DGV.CellClick
    		If e.RowIndex < 0 Or e.ColumnIndex < 0 Then Exit Sub
    		If e.ColumnIndex = DGV.Columns("Column2").Index Then
    
    			Dim r As Rectangle = DGV.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, False)
    			Dim s As String = DGV.Rows(e.RowIndex).Cells(DGV.Columns("Column2").Index).Value.ToString()
    
    			Select Case e.ColumnIndex
    				Case DGV.Columns("Column2").Index
    					CB.Location = r.Location
    					CB.Width = r.Width + 20
    					If Not s Is Nothing Then
    						CB.SelectedItem = s
    					End If
    					CB.Visible = True
    					CB.DroppedDown = True
    			End Select
    		Else
    			CB.DroppedDown = False
    			CB.Visible = False
    		End If
    	End Sub
    	Private Sub CB_SelectedIndexChanged(sender As Object, e As EventArgs) Handles CB.SelectedIndexChanged
    		If CB.Visible Then
    			DGV.CurrentCell.Value = CB.Text
    			CB.Visible = False
    		End If
    	End Sub
    End Class


    Regards Les, Livingston, Scotland


    • Proposed as answer by Andrew B. Painter Tuesday, July 21, 2020 6:40 PM
    • Edited by leshay Tuesday, July 21, 2020 6:58 PM code was placed inside text
    • Marked as answer by Ravi Kumar12233 Thursday, July 30, 2020 10:49 AM
    Tuesday, July 21, 2020 6:11 PM

All replies

  • I have a winform connect to Access Db as data source

    1. but the Fields Namely "Condition of Dies"(1-7) are actually combo boxes in my DB (taking only "ok , nok" from drop down list) , but here(in my form) it coming as text box taking any value , how to change this?
    2. And also i have two date fields namely "calibrated on" and "next calibration due" as  "dd-mmm-yy" in my db , how to format the same in the fields of my form and data grid box too?

    I think you're confusing "The GUI in the Access Application" with "Access Database DataTypes" and maybe also OLE or ADO DB interface engines. For example: Access (application) gives you the ability to create comboboxes that only take value A or value B. Access Database will turn that into a different type of data field, probably boolean based on your description.

    So you're accessing the same DB from ADO or whatever engine and trying to put your own, custom, GUI on it. To do that you actually have to add comboboxes and give those comboboxes their limited range of values. By your own handiwork. 

    You might poke around in the DataGridView documentation, but I'm pretty sure it's provided as a rough outline and won't really let you make those custom mods to individual columns or cells.


    Before you can learn anything new you have to learn that there's stuff you don't know.

    Tuesday, July 21, 2020 4:20 PM
  • Hi

    An alternative idea.

    Since you start by saying you have a Winform, then I am assuming you meant DataGridView rather than a Data Grid.  If that is the case, then the following code may help.

    It defines a Combobox to contain 2 items "OK" and "NOK", and, when a cell is clicked in a column (1 or more, set by you), rhen the User is presented with rge list to choose from (the list can be anything and any number of items). If the cell already has a value then it is shown as the current item in the ComboBox, otherwise, it shows a blank (with the list shown below)

    This is a stand alone example just to show the idea. Needs a DataGridView1 named DGV (with a few Columns added to it), a Combobox1 named CB in the Designer on Form1.
    Option Strict On
    Option Explicit On
    Public Class Form1
    	Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    		With CB
    			.Items.AddRange({"OK", "NOK"})
    			.AutoCompleteMode = AutoCompleteMode.SuggestAppend
    			.AutoCompleteSource = AutoCompleteSource.ListItems
    		End With
    
    		' only for example
    		' set some random values
    		' in Column2
    		With DGV
    			Dim r As New Random
    			For i As Integer = 0 To 9
    				.Rows.Add(Nothing, IIf(r.Next(0, 2) = 1, "OK", "NOK"))
    			Next
    		End With
    	End Sub
    	Private Sub DGV_CellClick(sender As Object, e As DataGridViewCellEventArgs) Handles DGV.CellClick
    		If e.RowIndex < 0 Or e.ColumnIndex < 0 Then Exit Sub
    		If e.ColumnIndex = DGV.Columns("Column2").Index Then
    
    			Dim r As Rectangle = DGV.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, False)
    			Dim s As String = DGV.Rows(e.RowIndex).Cells(DGV.Columns("Column2").Index).Value.ToString()
    
    			Select Case e.ColumnIndex
    				Case DGV.Columns("Column2").Index
    					CB.Location = r.Location
    					CB.Width = r.Width + 20
    					If Not s Is Nothing Then
    						CB.SelectedItem = s
    					End If
    					CB.Visible = True
    					CB.DroppedDown = True
    			End Select
    		Else
    			CB.DroppedDown = False
    			CB.Visible = False
    		End If
    	End Sub
    	Private Sub CB_SelectedIndexChanged(sender As Object, e As EventArgs) Handles CB.SelectedIndexChanged
    		If CB.Visible Then
    			DGV.CurrentCell.Value = CB.Text
    			CB.Visible = False
    		End If
    	End Sub
    End Class


    Regards Les, Livingston, Scotland


    • Proposed as answer by Andrew B. Painter Tuesday, July 21, 2020 6:40 PM
    • Edited by leshay Tuesday, July 21, 2020 6:58 PM code was placed inside text
    • Marked as answer by Ravi Kumar12233 Thursday, July 30, 2020 10:49 AM
    Tuesday, July 21, 2020 6:11 PM
  • Thank you so much ..it solved my problem
    Saturday, July 25, 2020 9:57 AM
  • Hi Ravi Kumar12233,

    I'm glad to hear that your question has been answered. Please click the "Mark as Answer" Link at the bottom of the correct post(s), so that it will help other members to find the solution quickly if they face a similar issue.

    Best Regards,

    Xingyu Zhao

    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, July 27, 2020 1:18 AM
  • Sorry ...i forgot to do that ..did now.
    Thursday, July 30, 2020 10:49 AM