Olukayode Oluwole
asked on
How can i get my comboBox selection to work properly without a message box
I have in my c# application a combo box from which i select a record and then
use the split command to get then items in the combo (See Screen with the Combo box Below)
The problem i have is that ordinarily if i click on a record it show give me the last figure exchange rate
in the exchange rate box.
What happen right no is that is i just click I dont get anything selected into the exchange rate box (see below)
Now when i introduced the message box and just click ok after the messagebox comes up i get the required result
see screen below.
Definitely i dont want my application to ship out with users having to Click ok on message box
How can i solve this problem.
There must be a way to get the selection without the message box
The detailed code is shown below:
[private void glTRANSCurrencyValue_Selec tedIndexCh anged(obje ct sender, EventArgs e)
{
// Setting index to 0 at form load ensure that script below is not processed during combo initial load
if (this.glTRANSCurrencyValue .SelectedI ndex != 0)
{
glTRANSExchrateValue.Text = "0";
System.Data.DataRowView selected_row = glTRANSCurrencyValue.Selec tedItem as System.Data.DataRowView;
// Always use this TryParse to determin a selected value. Thencomment ou the Message Box once satisfied
bool isNumeric;
int i;
string str = glTRANSCurrencyValue.Selec tedText.To String();
isNumeric = int.TryParse(str, out i);
MessageBox.Show("The value of i is " + i); // Always uncomment this line to see how TryParse worked
string phrase = glTRANSCurrencyValue.Selec tedText.To String();
string[] words = phrase.Split(' ');
int j = 0;
foreach (var word in words) // Always put cursor on words to determine content of each field field[1] ..... field[n]. Can also Use switch to get the fields
{
j += 1;
switch (j)
{
case 3:
break;
case 5:
string var2 = $"{ word}";
glTRANSExchrateValue.Text = var2;
break;
}
}
}
}[/code]
Please Help
Olukay
use the split command to get then items in the combo (See Screen with the Combo box Below)
The problem i have is that ordinarily if i click on a record it show give me the last figure exchange rate
in the exchange rate box.
What happen right no is that is i just click I dont get anything selected into the exchange rate box (see below)
Now when i introduced the message box and just click ok after the messagebox comes up i get the required result
see screen below.
Definitely i dont want my application to ship out with users having to Click ok on message box
How can i solve this problem.
There must be a way to get the selection without the message box
The detailed code is shown below:
[private void glTRANSCurrencyValue_Selec
{
// Setting index to 0 at form load ensure that script below is not processed during combo initial load
if (this.glTRANSCurrencyValue
{
glTRANSExchrateValue.Text = "0";
System.Data.DataRowView selected_row = glTRANSCurrencyValue.Selec
// Always use this TryParse to determin a selected value. Thencomment ou the Message Box once satisfied
bool isNumeric;
int i;
string str = glTRANSCurrencyValue.Selec
isNumeric = int.TryParse(str, out i);
MessageBox.Show("The value of i is " + i); // Always uncomment this line to see how TryParse worked
string phrase = glTRANSCurrencyValue.Selec
string[] words = phrase.Split(' ');
int j = 0;
foreach (var word in words) // Always put cursor on words to determine content of each field field[1] ..... field[n]. Can also Use switch to get the fields
{
j += 1;
switch (j)
{
case 3:
break;
case 5:
string var2 = $"{ word}";
glTRANSExchrateValue.Text = var2;
break;
}
}
}
}[/code]
Please Help
Olukay
ASKER
ok, if you look at properties of glTRANSExchrateValue component in section Data.DataBindings do you have it connected to your datasource?
ziolko.
EE.jpg
ziolko.
EE.jpg
ASKER
Ok Now I have the Data Binding Page for Currency ComboBox and ExchangeValue TextBox (see below) as you requested.
Under ExchangeRate I could not find SelectedItem and SelectedValue BUT it exist under
Currency Comboxbox. Which i can understand because i actually loaded Currency ComboBox
from the database
I tried to examine one of the field bindings and it was asking for a connection string so i stopped and thought i should explain my situation before i make any changes.
Normally I connect to my local server or a cloud server by changing parameters in my app.config file.
If I start specifying a connection string under data bindings would I have to be changing this
setting each time i connect to either a local server or a cloud server.
Now that you know what i want and have the 2 pages on bindings please advise on what to do next
Thanks
Under ExchangeRate I could not find SelectedItem and SelectedValue BUT it exist under
Currency Comboxbox. Which i can understand because i actually loaded Currency ComboBox
from the database
I tried to examine one of the field bindings and it was asking for a connection string so i stopped and thought i should explain my situation before i make any changes.
Normally I connect to my local server or a cloud server by changing parameters in my app.config file.
If I start specifying a connection string under data bindings would I have to be changing this
setting each time i connect to either a local server or a cloud server.
Now that you know what i want and have the 2 pages on bindings please advise on what to do next
Thanks
Hi,
how do you load contents of your dropdown from DB?
do you query DB and run loop statement by adding items?
ziolko.
how do you load contents of your dropdown from DB?
do you query DB and run loop statement by adding items?
ziolko.
..my guess is that you run query and "manually" fill comboBox with some loop statement, if I'mright then indeed, this:
System.Data.DataRowView selected_row = glTRANSCurrencyValue.Selec tedItem as System.Data.DataRowView;
will not work.
And yes it is true - setting bindings in design time will make it difficult to switch between local and remote databases.
But you can set them in run-time, something like this:
comboBox1.DataSource = this.testtableBindingSourc e;
comboBox1.DisplayMember = "FieldName1";
that's of course my sample for table called "testtable" that has field called "FieldName1"
in your case that will be probably something like:
this.exchratesBindingSourc e
you can do similar run-time binding with both your controls: comboBox (glTRANSCurrencyValue) and editBox (glTRANSExchrateValue)
if you do that - switching between comboBox items wil lautomatically update values in ex. rate editbox without any additional code
ziolko.
System.Data.DataRowView selected_row = glTRANSCurrencyValue.Selec
will not work.
And yes it is true - setting bindings in design time will make it difficult to switch between local and remote databases.
But you can set them in run-time, something like this:
comboBox1.DataSource = this.testtableBindingSourc
comboBox1.DisplayMember = "FieldName1";
that's of course my sample for table called "testtable" that has field called "FieldName1"
in your case that will be probably something like:
this.exchratesBindingSourc
you can do similar run-time binding with both your controls: comboBox (glTRANSCurrencyValue) and editBox (glTRANSExchrateValue)
if you do that - switching between comboBox items wil lautomatically update values in ex. rate editbox without any additional code
ziolko.
ASKER
Below specifies how i do the load
1. At Form Load I have the script below
glTRANSCurrencyValue.DataS ource = GlobalConfig.Connection.Ge tExchRate_ All();
glTRANSCurrencyValue.Displ ayMember = "FullCodeName";
2. That then goes into the section below to load the combo using while (reader.Read()) Loop
[public List<ExchRatesModel> GetExchRate_All()
{
List<ExchRatesModel> results = new List<ExchRatesModel>();
using (var conn = new NpgsqlConnection(pgrstring ))
{
using (var command = new NpgsqlCommand("public.spex chrates_ge tall", conn))
{
conn.Open();
command.CommandType = CommandType.StoredProcedur e;
command.Parameters.Add(new NpgsqlParameter("companyco dex", NpgsqlTypes.NpgsqlDbType.V archar) { Direction = ParameterDirection.Input, Value = LoginDetails.staticcompany });
var reader = command.ExecuteReader();
// Bring in Parameters to limit selection to Login.Staticcompany
while (reader.Read())
{
ExchRatesModel model = new ExchRatesModel();
//if ((string)reader["absecode" ] != null)
if (((string)reader["mnthyear "] != null) && ((string)reader["othercurr encycode"] != null) && ((string)reader["ledgercur rencycode" ] != null) && Convert.ToDecimal(reader[" exchrate"] ) != 0)
{
model.Id = (int)reader["id"]; //id is the column name in the database
model.mnthyear = (string)reader["mnthyear"] ; //absecode is the column name in the database
model.othercurrencycode = (string)reader["othercurre ncycode"]; //absedesc is the column name in the database
model.ledgercurrencycode = (string)reader["ledgercurr encycode"] ; //absedesc is the column name in the database
model.exchrate = Convert.ToDecimal(reader[" exchrate"] ); //absedesc is the column name in the database
model.Companycode = (string)reader["companycod e"]; //absedesc is the column name in the database
results.Add(model);
}
}
}
}
return results;
}[/code]
Any advise
1. At Form Load I have the script below
glTRANSCurrencyValue.DataS
glTRANSCurrencyValue.Displ
2. That then goes into the section below to load the combo using while (reader.Read()) Loop
[public List<ExchRatesModel> GetExchRate_All()
{
List<ExchRatesModel> results = new List<ExchRatesModel>();
using (var conn = new NpgsqlConnection(pgrstring
{
using (var command = new NpgsqlCommand("public.spex
{
conn.Open();
command.CommandType = CommandType.StoredProcedur
command.Parameters.Add(new
var reader = command.ExecuteReader();
// Bring in Parameters to limit selection to Login.Staticcompany
while (reader.Read())
{
ExchRatesModel model = new ExchRatesModel();
//if ((string)reader["absecode"
if (((string)reader["mnthyear
{
model.Id = (int)reader["id"]; //id is the column name in the database
model.mnthyear = (string)reader["mnthyear"]
model.othercurrencycode = (string)reader["othercurre
model.ledgercurrencycode = (string)reader["ledgercurr
model.exchrate = Convert.ToDecimal(reader["
model.Companycode = (string)reader["companycod
results.Add(model);
}
}
}
}
return results;
}[/code]
Any advise
ASKER
I find this your statement interesting
you can do similar run-time binding with both your controls: comboBox (glTRANSCurrencyValue) and editBox (glTRANSExchrateValue)
if you do that - switching between comboBox items wil lautomatically update values in ex. rate editbox without any additional code
Do you have teamviewer and can you help on line real time
Thanks
Olukay
you can do similar run-time binding with both your controls: comboBox (glTRANSCurrencyValue) and editBox (glTRANSExchrateValue)
if you do that - switching between comboBox items wil lautomatically update values in ex. rate editbox without any additional code
Do you have teamviewer and can you help on line real time
Thanks
Olukay
Hi,
you are almost there:)
The datasource you create by calling GetExchRate_All() must be also assigned to textBox… but it must be the same datasource.
To do so you need to introduce some global variable and initialize it with GetExchRate_All() method, then use this variable as datasource for both: comboBox and textBox.
Term "global variable" may not be strictly correct but I do not know exact structure of your project.
In my case I used private static on a form that I use in test project:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private static List<ExchRatesModel> exDataSource;
now you have to initialize it and use, i used simple button, comboBox and editBox:
private void button4_Click(object sender, EventArgs e)
{
exDataSource = GetExchRate_All();
comboBox1.DataSource = exDataSource;
comboBox1.DisplayMember = "FullCodeName";
textBox2.DataBindings.Add( new System.Windows.Forms.Bindi ng("Text", exDataSource, "exchrate", true));
}
and make sure that code of private void glTRANSCurrencyValue_Selec tedIndexCh anged(obje ct sender, EventArgs e) is commented (or removed) to make sure that nothing gets executed in this method.
I used button but you can simply replace:
glTRANSCurrencyValue.DataS ource = GlobalConfig.Connection.Ge tExchRate_ All();
glTRANSCurrencyValue.Displ ayMember = "FullCodeName";
with this:
exDataSource = GetExchRate_All();
glTRANSCurrencyValue.DataS ource = exDataSource;
glTRANSCurrencyValue.Displ ayMember = "FullCodeName";
glTRANSExchrateValue.DataB indings.Ad d(new System.Windows.Forms.Bindi ng("Text", exDataSource, "exchrate", true));
ziolko.
you are almost there:)
The datasource you create by calling GetExchRate_All() must be also assigned to textBox… but it must be the same datasource.
To do so you need to introduce some global variable and initialize it with GetExchRate_All() method, then use this variable as datasource for both: comboBox and textBox.
Term "global variable" may not be strictly correct but I do not know exact structure of your project.
In my case I used private static on a form that I use in test project:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private static List<ExchRatesModel> exDataSource;
now you have to initialize it and use, i used simple button, comboBox and editBox:
private void button4_Click(object sender, EventArgs e)
{
exDataSource = GetExchRate_All();
comboBox1.DataSource = exDataSource;
comboBox1.DisplayMember = "FullCodeName";
textBox2.DataBindings.Add(
}
and make sure that code of private void glTRANSCurrencyValue_Selec
I used button but you can simply replace:
glTRANSCurrencyValue.DataS
glTRANSCurrencyValue.Displ
with this:
exDataSource = GetExchRate_All();
glTRANSCurrencyValue.DataS
glTRANSCurrencyValue.Displ
glTRANSExchrateValue.DataB
ziolko.
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
ASKER
Worked like Magic
Thanks very much
Team viewer support not needed for now
Olukay
Thanks very much
Team viewer support not needed for now
Olukay
ASKER
I am sorry i had a similar problem while doing a similar data movment
between a combobox in a grid and a textbox within same Grid.
DataBinding is not defined for the Grid
Please find attached the issue and help with the syntax
DataGridImplementation180220.docx
Olukay
between a combobox in a grid and a textbox within same Grid.
DataBinding is not defined for the Grid
Please find attached the issue and help with the syntax
DataGridImplementation180220.docx
Olukay
if you use standard DataGridView component available on VisualStudio's Toolbox then you can simply use:
dgvtxtDivision.DataSource = LoginDetails.exDataSource2 ;
ziolko.
dgvtxtDivision.DataSource = LoginDetails.exDataSource2
ziolko.
ASKER
Yes I use the DataGridView in Visual Studio
dgvtxtDivision as a textbox as no Datasource property
So i tried changing it to a combobox to enable me have the DataSource property which i got (Though i really think
it should be a textbox so that once department is selected from combo it gets filled.
my code is now as below
LoginDetails.exDataSource2 = GlobalConfig.Connection.Ge tResponsib leMgr_All( );
dgvcmbDepartment.DataSourc e = LoginDetails.exDataSource2 ;
dgvcmbDepartment.DisplayMe mber = "FullCodeName";
dgvcmbDepartment.ValueMemb er = "deptcode";
dgvtxtDivision.DataSource = LoginDetails.exDataSource2 ;
and my result from the grid combo is shown below with division columnempty
dgvtxtDivision.ValueMember = "divisioncode";
What am I doing wrongly
Regards
Olukay
dgvtxtDivision as a textbox as no Datasource property
So i tried changing it to a combobox to enable me have the DataSource property which i got (Though i really think
it should be a textbox so that once department is selected from combo it gets filled.
my code is now as below
LoginDetails.exDataSource2
dgvcmbDepartment.DataSourc
dgvcmbDepartment.DisplayMe
dgvcmbDepartment.ValueMemb
dgvtxtDivision.DataSource = LoginDetails.exDataSource2
and my result from the grid combo is shown below with division columnempty
dgvtxtDivision.ValueMember
What am I doing wrongly
Regards
Olukay
if I remember correctly you have to assign DataPropertyName of a grid column
ziolko.
ziolko.
...and one more thing, on a grid column you have to assign .DataSource property but this cannot be your LoginDetails.exDataSource2
it has to be list of unique values.
ziolko.
it has to be list of unique values.
ziolko.
suggestion I gave you:
dgvtxtDivision.DataSource = LoginDetails.exDataSource2
was my mistake, I thought "dgvtxtDivision" was a grid not just a grid column
ziolko.
dgvtxtDivision.DataSource = LoginDetails.exDataSource2
was my mistake, I thought "dgvtxtDivision" was a grid not just a grid column
ziolko.
not sure what exactly you want to achieve but quick and easy solution is:
dgvtxtDivision.DataSource = LoginDetails.exDataSource2 .Select(el => el.divisioncode).Distinct( ).ToList() ;
that's assuming you use LinQ
ziolko.
dgvtxtDivision.DataSource = LoginDetails.exDataSource2
that's assuming you use LinQ
ziolko.
you do get selected record in "selected_row" variable but later it's not used.
if your exchange rate is field in database, you can use:
glTRANSExchrateValue.Text = selected_row["name_of_fiel
other solution would be to connect your glTRANSExchrateValue textBox directly to database table, in this case you do not have to handle changing selection on glTRANSCurrencyValue.
Values will switch automatically