I've created a grid and one of my cells is a combobox where I dynamically generate the datasource. This part of the program works fine and as intended - the dropdown shows the desired text in the dropdown menu and the CellValue property corresponds to the selection.
However I'm having difficulty setting the CellValue when the program initially loads during start up (to load a default value). The code sets the cell value correctly, however the display on the combobox is left blank. If I try to manually overwrite the display by using the grid.Text property, then it also overwrites the CellValue to whatever I have assigned in Text.
The bare essentials of my program code are listed below, illustrating the problem I am having. How I'm expecting this to function is for the program to load, displaying 7:00 AM in the Grid ComboBox while also being able to use the grid.CellValue property to retrieve the DateTime object of 9/29/2021 7:00 AM.
I have included the commented out grid.Text = 7:00 AM line below, as that ends up overwriting grid.CellValue to 7:00 AM, and thus not grid.CellValue no longer returns the DateTime object. If you uncomment it, you will see the problem I am having.
-----------
Imports Syncfusion.Windows.Forms.Grid
Public Class Form1
Friend grid As New GridControl
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
LoadGrid()
End Sub
Public Sub grid_CurrentCellEditingCompleteHandler(sender As Object, e As EventArgs)
Dim cell = grid.CurrentCell
MsgBox(grid(cell.RowIndex, cell.ColIndex).CellValue)
End Sub
Public Sub grid_CurrentCellCloseDropDownHandler(sender As Object, e As EventArgs)
grid.CurrentCell.EndEdit()
End Sub
Private Sub LoadGrid()
grid.Size = New Size(1884, 840)
grid.Location = New Point(12, 168)
Controls.Add(grid)
AddHandler grid.CurrentCellEditingComplete, AddressOf grid_CurrentCellEditingCompleteHandler
AddHandler grid.CurrentCellCloseDropDown, AddressOf grid_CurrentCellCloseDropDownHandler
grid.Visible = True
grid.RowCount = 1
grid.ColCount = 1
grid(1, 1).CellType = GridCellTypeName.ComboBox
grid(1, 1).DataSource = CreateHoursDropdown("9/29/2021")
grid(1, 1).DisplayMember = "Time"
grid(1, 1).ValueMember = "DT_Obj"
grid(1, 1).AutoCompleteInEditMode = GridComboSelectionOptions.AutoComplete
grid(1, 1).ExclusiveChoiceList = True
grid(1, 1).CellValue = New DateTime(2021, 9, 29, 7, 0, 0)
'grid(1, 1).Text = "7:00 AM"
MsgBox(grid(1, 1).CellValue)
End Sub
Private Function CreateHoursDropdown(ScheduleDate As String, Optional starthr As Integer = 3, Optional endhr As Integer = 23, Optional startqh As Integer = 0) As DataTable
Dim dt As New DataTable
Dim ScheduleDateTime As DateTime = Convert.ToDateTime(ScheduleDate)
Dim timeObj As DateTime
dt.Columns.Add("Time")
dt.Columns.Add("DT_Obj")
For h As Integer = starthr + 1 To endhr - 1
For qh As Integer = 0 To 3
timeObj = New DateTime(ScheduleDateTime.Year, ScheduleDateTime.Month, ScheduleDateTime.Day, h, qh * 15, 0)
dt.Rows.Add(timeObj.ToString("t"), timeObj)
Next
Next
Return dt
End Function
End Class
-------
I'm making this cell on the Grid into a ComboBox type so the user can use to provide/change the time. When the program initially loads, it uses a database to populate these cells with the previously stored time, however the user needs to be able to modify that value if they want.
It is important that I have access to the date associated with a specific time entry in the background, as the full DateTime is required for other calculations in the program. This is why the ValueMember is assigned to a DateTime object, however displaying the the full datetime object looks messy and takes up too much space, which is why the DisplayMember is only set to the time. In some cases the DisplayMember may event be a text string such as "Close" and still have an associated DateTime object with it.
What I'm hoping to have happen is this to work similar to the SelectedValue property in a native ComboBox. When I set the SelectedValue the ComboBox text updates to show the DisplayMember associated with the ValueMember selected. However a GridControl.CellType = "ComboBox" does not have an accessible SelectedValue property.
After re-reading your response, wanted to clarify for this bit "If you just want to use the CellValue property and not the Text Property to display the value of a cell."
-----------
I want to use the CellValue property to make that row the selected value within the ComboBox Cell Type, however I want it to show in the cell the DisplayMember column for that corresponding value.
I.e. if #9/29/2021 7:00AM# is the CellValue, it should show the corresponding time of 7:00 AM from the Time column in the DataSource as that is the DisplayMember.
Currently if the user manually selects a time from the drop down menu, it behaves correctly. The display within the cell shows 7:00AM and the CellValue becomes #9/29/2021 7:00AM#. However when setting the cell value via the code it does not behave as intended, the CellValue is set, but the corresponding Time DisplayMember does not show up in the cell.
|
Private Function CreateHoursDropdown(ByVal ScheduleDate As String, Optional ByVal starthr As Integer = 3, Optional ByVal endhr As Integer = 23, Optional ByVal startqh As Integer = 0) As DataTable
Dim dt = New DataTable()
Dim ScheduleDateTime = Convert.ToDateTime(ScheduleDate)
Dim timeObj As DateTime
dt.Columns.Add("Time")
dt.Columns.Add("DT_Obj", GetType(DateTime))
Dim h As Integer = starthr + 1
Dim loopTo As Integer = endhr - 1
Do While h <= loopTo
For qh As Integer = 0 To 3
timeObj = New DateTime(ScheduleDateTime.Year, ScheduleDateTime.Month, ScheduleDateTime.Day, h, qh * 15, 0)
dt.Rows.Add(timeObj.ToString("dd"), timeObj)
Next qh
h += 1
Loop
Return dt
End Function |
This fix worked. Thank you.