3
votes

High level view of the application is:

  • Form1 displays client information in a DataGridView (pulled from a DB).
  • I save the client's information from the DataGridView in properties located in a Client class.
  • From Form1, the user can click a button that instantiates Form2 and allows the client's information to be modified.
  • Form2's constructor has a Client object as a parameter. This object is what holds all the client's information from Form1. Using this object i can re-populate the fields that i want the user to be able to edit on Form2.

There is a table in the DataSource that holds all the case types (ie. CaseType1, CaseType2, CaseType3).

I then use a ComboBox and populate it with all the case types from the DataSource when the form is instantiated. My ComboBox settings are as follows:

DataSource: set to my CaseTypeBindingSource which has the columns and data i need. DisplayMember: The result of the query being used. ValueMember: The result of the query being used. (same as DisplayMember) SelectedValue: I have tried it with "none" and with the same value as DisplayMember and ValueMember.

Here's the problem:

At run-time, i want to be able to assign the client's case type that was brought over from Form1 as the item that is currently selected in the ComboBox (SelectedItem).

I could just assign the case type to a textbox and be done with it. But the idea is that i would like the form to show the user what the client's case type is, and allow him/her to change it by using the ComboBox.

The ComboBox name is CaseTypeComboBox. The object holding the case type information and its property is client.CaseType.

I've tried the following inside the constructor:

CaseTypeComboBox.SelectedItem = client.CaseType;

I've also tried creating a class variable in Form2 called origCaseType, assigning the client.CaseType value to the origCaseType class variable in the constructor. Then doing the following when the Form2_Load(...) event fires:

CaseTypeComboBox.SelectedValue = origCaseType;

Does anyone have any thoughts on this? Any similar experience?

Please let me know if anything needs clarification, any help is appreciated.

Thanks in advance!

TS

"Form2" Constructor:

public ModifyCase(Client client)
{
    InitializeComponent();
    CaseNumberTextBox.Text = client.CaseNumber;
    LoadStatusComboBox(client.Status);
    LoadIsClosedRadioButton(client.IsClosed);
    LoadIsInStorageRadioButton(client.IsInStorage);
    LastModifiedTextBox.Text = client.LastModified.ToString();
    NotesTextBox.Text = client.Notes;

    origCaseType = client.CaseType;
}

"Form2" Load event:

private void ModifyCase_Load(object sender, EventArgs e)
{
    //Fills the ComboBox box with case types
    this.case_typesTableAdapter1.Fill(this.testDataSet1.case_types);

    //Attempts to set value for client's case type from Form1
    CaseTypeComboBox.SelectedValue = origCaseType;
    //Just to help me see what these variables are holding to figure
    //out the problem...
    MessageBox.Show(origCaseType);
    MessageBox.Show(CaseTypeComboBox.SelectedIndex.ToString());
}
1
So is the crux of the problem that you are unable to set the correct combo box value in Form2? Can you post your Form2 code relating to your combo box - wdavo
Yes, the crux of the problem is that i am unable to set the correct case type on the ComboBox in Form2 with information brought in from Form1. I posted some more code, let me know if that helps or if you need anything else in specific. - ThiagoNSiq
Can you add MessageBox.Show(CaseTypeComboBox.Items.Count.ToString()); after your .Fill statement and let me know what it is. I suspect it will be 0 meaning that at the time your are trying to set the SelectedValue, your ComboBox has not yet updated. - wdavo
Count is 53, which is the number of case types i have. I made a small adjustment that yielded a different result, but something is still off. - ThiagoNSiq
I made two small adjustments that did the trick! The problem was i had set SelectedItem and SelectedValue to the DataSource column i'm using. Except, all the was needed was to set those two values to "none", and set the ValueMember and DisplayMember to the DataSource column. Thanks for your time wdavo! - ThiagoNSiq

1 Answers

0
votes

I made two small adjustments that did the trick! The problem was i had set SelectedItem and SelectedValue to the DataSource column i'm using. Except, all the was needed was to set those two values to "none", and set the ValueMember and DisplayMember to the DataSource column. Thanks for your time wdavo!