Page 1 of 1
Combobox to update DBF
Posted: Wed Jan 29, 2025 11:09 pm
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!
Re: Combobox to update DBF
Posted: Thu Jan 30, 2025 4:40 am
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.
Re: Combobox to update DBF
Posted: Fri Jan 31, 2025 10:16 pm
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.
Re: Combobox to update DBF
Posted: Fri Jan 31, 2025 10:30 pm
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.
Re: Combobox to update DBF
Posted: Sun Feb 02, 2025 4:56 am
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
Re: Combobox to update DBF
Posted: Sun Feb 02, 2025 10:11 pm
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:
Re: Combobox to update DBF
Posted: Sun Feb 02, 2025 11:42 pm
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);
Re: Combobox to update DBF
Posted: Mon Feb 03, 2025 6:53 pm
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!
Re: Combobox to update DBF
Posted: Tue Feb 04, 2025 4:37 pm
by Jamal
You're welcome. I am glad that worked for you.