Combobox to update DBF

This forum is meant for questions and discussions about the X# language and tools
Post Reply
RolWil
Posts: 73
Joined: Mon Jul 18, 2022 3:16 am

Combobox to update DBF

Post by RolWil »

Hi everyone,

I’ve been beating myself up for days now trying to get this to work – no luck.

On a Form, I’m trying to implement a combobox for gender, let’s say with 3 options: “Male”, “Female” and blank.
I would like to tie this combobox directly to a gender field in a DBF so that :
a) the combobox is updated by the DBF when it is browsed
b) changing the combobox updates the record in the DBF

Windows Forms, dialect: Harbour. XSharp in VS is current, 2.20

Thanks so much!
Jamal
Posts: 332
Joined: Mon Jul 03, 2017 7:02 pm

Re: Combobox to update DBF

Post by Jamal »

A sample working project that shows what you tried would help get you answers. It is not clear what data controls you are using and events are being utilized.

Aside from the above, hopefully someone familiar with Harbour would jump in since I have not used it, however, in .NET, I guess the DataGridView control comes to mind and should work in any dialect and use the DataGridViewComboBoxColumn and bind to your values on load, then on leave you would code the CurrentCellDirtyStateChanged event and do further processing to save your data.
Last edited by Jamal on Sat Feb 01, 2025 5:05 pm, edited 1 time in total.
RolWil
Posts: 73
Joined: Mon Jul 18, 2022 3:16 am

Re: Combobox to update DBF

Post by RolWil »

I created a very simple project to highlight the issue I’m having which is linking a Combobox to a column in a DBF table. The goal is to have the Combobox show the long-form of gender, but the table only has the single character (“F”,”M”,” “).

On the form, you have the navigator bar, the “Name” and “Gender” Combobox and textbox. Both the “Name” and the “Gender” text boxes are directly linked to the table and update correctly when using the navigator bar. However, the Combobox does not.

I’ve pre-filled the Combobox with 3 gender options: “Male”, “Female” and “Unknown”. If I don’t link the Combobox to the table, its drop-down shows these 3 gender options, but of course the Combobox’s text-box portion is empty. If I do link the Combobox to the table’s “Gender” column, I get something weird: the dropdown list changes to an array and the text-box portion still remains empty.

Code: Select all

USE PEOPLE shared new   
VAR oDataSourceBFMB := DbDataSource()  // create a data object of the current workarea

SELF:bindingSourceBFMB:DataSource := oDataSourceBFMB

// Link Name:
SELF:tbSurName:DataBindings:Add("Text", bindingSourceBFMB,"NAME")
            
// Link Gender column to textbox
SELF:tbGender:DataBindings:Add("Text", bindingSourceBFMB,"GENDER");
  
// build Combobox and then attempt to link it to the Gender column          
SELF:cbGender:Items:Add(KeyValuePair<STRING, STRING>{"F","Female"})
SELF:cbGender:Items:Add(KeyValuePair<STRING, STRING>{"M","Male"})
SELF:cbGender:Items:Add(KeyValuePair<STRING, STRING>{" ","Undefined"})
SELF:cbGender:DisplayMember := "value"
SELF:cbGender:ValueMember := "key"            
SELF:cbGender:DisplayMember:= ("Text", bindingSourceBFMB,"GENDER")

Note: the attached ZIP contains the project, including the “PEOPLE” table.
Attachments
ComboBoxIssue.jpg
Testing Combobox.ZIP
(1.31 MiB) Downloaded 27 times
Last edited by RolWil on Sat Feb 01, 2025 3:51 pm, edited 1 time in total.
RolWil
Posts: 73
Joined: Mon Jul 18, 2022 3:16 am

Re: Combobox to update DBF

Post by RolWil »

Jamal wrote: Thu Jan 30, 2025 4:40 am A sample working project that shows what you tried would help get you answers. It is not clear what data controls you are using and events are being utilized.

Aside from the above, hopefully someone familiar with Harbour would jump in since I have it used it, however, in .NET, I guess the DataGridView control comes to mind and should work in any dialect and use the DataGridViewComboBoxColumn and bind to your values on load, then on leave you would code the CurrentCellDirtyStateChanged event and do further processing to save your data.

Thanks Jamal, I just posted more info and a sample project.
Jamal
Posts: 332
Joined: Mon Jul 03, 2017 7:02 pm

Re: Combobox to update DBF

Post by Jamal »

Set the combobox value using SelectedValue

Code: Select all

 SELF:cbGender:DisplayMember := "Value"
 SELF:cbGender:ValueMember := "Key"

 SELF:cbGender:SelectedValue := ("Text", bindingSourceBFMB,"GENDER")    //  This line
 
RolWil
Posts: 73
Joined: Mon Jul 18, 2022 3:16 am

Re: Combobox to update DBF

Post by RolWil »

Jamal wrote: Sun Feb 02, 2025 4:56 am Set the combobox value using SelectedValue

Code: Select all

 SELF:cbGender:DisplayMember := "Value"
 SELF:cbGender:ValueMember := "Key"

 SELF:cbGender:SelectedValue := ("Text", bindingSourceBFMB,"GENDER")    //  This line
 
Thanks Jamal, that does improve the drop down list, but the text box is still not updated:
ComboBoxIssue2.jpg
Jamal
Posts: 332
Joined: Mon Jul 03, 2017 7:02 pm

Re: Combobox to update DBF

Post by Jamal »

The following should display the values. Please read the code comments:

Code: Select all

// set up ComboBox with KeyValuePair list.  This is much cleaner
LOCAL genderList AS list<KeyValuePair<STRING, STRING>>
genderList := list<KeyValuePair<STRING, STRING>>{}
genderList:Add(KeyValuePair<STRING, STRING>{"M", "Male"})
genderList:Add(KeyValuePair<STRING, STRING>{"F", "Female"})
genderList:Add(KeyValuePair<STRING, STRING>{"U", "Undefined"})   // Undefined will not show up unless there is a Key value. I used U

SELF:cbGender:DataSource := genderList   // set the combobox DataSource
SELF:cbGender:DisplayMember := "Value"
SELF:cbGender:ValueMember := "Key"

// Bind ComboBox to Gender property. This is critical.
SELF:cbGender:DataBindings:Add("SelectedValue", SELF:bindingSourceBFMB, "GENDER", TRUE, DataSourceUpdateMode.OnPropertyChanged);


RolWil
Posts: 73
Joined: Mon Jul 18, 2022 3:16 am

Re: Combobox to update DBF

Post by RolWil »

Jamal wrote: Sun Feb 02, 2025 11:42 pm The following should display the values. Please read the code comments:

Code: Select all

// set up ComboBox with KeyValuePair list.  This is much cleaner
LOCAL genderList AS list<KeyValuePair<STRING, STRING>>
genderList := list<KeyValuePair<STRING, STRING>>{}
genderList:Add(KeyValuePair<STRING, STRING>{"M", "Male"})
genderList:Add(KeyValuePair<STRING, STRING>{"F", "Female"})
genderList:Add(KeyValuePair<STRING, STRING>{"U", "Undefined"})   // Undefined will not show up unless there is a Key value. I used U

SELF:cbGender:DataSource := genderList   // set the combobox DataSource
SELF:cbGender:DisplayMember := "Value"
SELF:cbGender:ValueMember := "Key"

// Bind ComboBox to Gender property. This is critical.
SELF:cbGender:DataBindings:Add("SelectedValue", SELF:bindingSourceBFMB, "GENDER", TRUE, DataSourceUpdateMode.OnPropertyChanged);


Thanks so much Jamal, that worked! :D
Jamal
Posts: 332
Joined: Mon Jul 03, 2017 7:02 pm

Re: Combobox to update DBF

Post by Jamal »

You're welcome. I am glad that worked for you.
Post Reply