Updating a Typed Table

I have a form which has a combobox and a collection of textboxes. The combobox is bound to one table and the textboxes are bound to another. However the value displayed by the combobox is also a field in the table to which the text boxes are bound.

I have not been able to find a satisfactory way of updating this latter table after setting the required values on the form. The only way I have been able to do it is to put a dummy textbox onto the form and to bind this textbox to the table to which the other textboxes are bound and to set the text of the dummy textbox to a value which corresponds to the displayed value of the combobox in the TextChanged Eventhandler of the combobox.

This seems like a very roundabout way of achieving a simple task. But how do I achieve this directly

I tried assigning the value that corresponds to the text in the combobox directly with the following code, but it doesn't work!

int ind = sectorBindingSource.Position;
int selectedInvestment = this.FIdataGridView.SelectedRows[0].Index;
this.fortunaDataSet.Investment.Rows[selectedInvestment].ItemArray[4] = this.fortunaDataSet.Sector
[ind].ID;
this.fortunaDataSet.Investment.Rows[selectedInvestment].SetModified();
this.investmentBindingSource.EndEdit();
this.investmentTableAdapter.Update(this.fortunaDataSet.Investment.Select
"","",DataViewRowState.ModifiedCurrent));

The Sector table is the table bound to the combobox. It displays a "description" and the "ID" corresponding to the selected description is stored in the Investment table. The relevant row of the Investment table is found from the FIdataGridView (which is also on the form).

Can anyone help There must be some straight forward way of doing this.

Thanks.



Answer this question

Updating a Typed Table

  • Kashif Chotu

    Dennis Jelavic wrote:

    I have a form which has a combobox and a collection of textboxes. The combobox is bound to one table and the textboxes are bound to another. However the value displayed by the combobox is also a field in the table to which the text boxes are bound.

    i didn't understand your qustion well , all what i got was that you want your datagridview to display the records that related to the selected item in the combobox, and if you changed the combobox selection the datagrid to change the filter, if you what i understood is true you can achieve that this way

    you suppose to have 2 bindingsources in your form, and those 2 tables has a relation to each other

    1) select the bindingsource where your datagrid is bound to and go to properties

    2) go to datasource property and select your other bindingsource (that combobox uses) as a darasource to your other bindingsource

    3) in the datamember property select the relationship between the 2 tables

    like that when you change the item in the combobox the datagrid will make a new filter

    hope this helps



  • Prabhjeet Singh

    Thanks for the reply, but it didn't really address my problem. However I've just bought Brian Noyes book on data binding and I hope by the time I finish reading that, I'll be able to answer my own question.
  • jochenam

    The application is as follows: An Investment table in the DB has rows which contain the data to describe individual investments. One of the columns in the Investment table is an industry Sector. There is another table in the database called Sector which contains rows of two columns (ID and Description). There is a one:many relationship between the Sector and the Investment. The Sector Code in the Investment Table is the ID in the Sector Table.

    What I am trying to achieve is the presentation of an investment on a form that will allow the user to edit the Investment data and then save the changed data back to the database.

    To achieve this I have a single form which presents the data in

    - a datagridview
    - a combobox
    - a bunch of text boxes

    The datagridview (DGV) simply presents an investment code and investment name in each row - one for each row in the Investment table -- This is simply to allow the user to select which investment is required for editing. On selection of one of the rows in the DGV the information for the selected investment is presented on the form in

    - a collection of textboxes on a number to tabbed pages

    - a combobox which displays the Industry Sector of the selected Investment. A Combobox is used so that if the user wishes to change the Sector he can do this by selecting from the dropdown list of the combobox.

    To implement this I have an InvestmentBindingSource which binds both the DGV to the Investment Table and the textboxes on the form to the Investment Table. Because both the DGV and the textboxes are bound to the Investment table thru the same InvestmentBindingSource, whenever a new investment is selected in the DGV the data values displayed in the textboxes reflect the selected investment. If one of these textboxes is changed the following code saves those changed values to the database.

    int ind = sectorBindingSource.Position; // determines the ordinal of the currently selected Sector in the sectorComboBox

    int selectedInvestment = this.FIdataGridView.SelectedRows[0].Index; //Determines the ordinal of the currently selected Investment in the DGV

    this.fortunaDataSet.Investment.Rows[selectedInvestment].ItemArray[4] = this.fortunaDataSet.Sector[ind].ID; // sets the sector code in the selected ionvestment row in the dataset table.

    this.investmentBindingSource.EndEdit();

    this.sectorBindingSource.EndEdit();

    this.investmentTableAdapter.Update(this.fortunaDataSet.Investment.Select("", "", DataViewRowState.ModifiedCurrent));

    However the sectorComboBox is not bound to the Investment table, but rather to the Sector table thru a sectorBindingSource as the sectorComboBox must display the sectors (Description column in the Sector Table) in its dropdown list. What I am attempting to do in the above code is to manually change the sector value in the dataset table in the hope that the update will include this with the data from the updated textboxes when the DB update occurs. But unfortunately the sector value (ie that value that is not bound thru the investmentBindingSource) does not get reflected in the updated DB whereas the data coming from the textboxes does.


  • Damien Armstrong

    hi,

    ok if that is just some textboxs you can use the bindingsources to achieve that


    private void investmentBindingSource_PositionChanged(object sender, EventArgs e)
    {
    DataRowView drv = (DataRowView) investmentBindingSource.Current;
    sectorBindingSource.Position = sectorBindingSource.Find(
    "TheSectorIDColumnName", drv[1]);
    }
    private void comboBox1_DropDownClosed(object sender, EventArgs e)
    {
    //change the value in the dataset manualy

    }


    drv[1] replace the int 1 by the column index that hold the sector id in investment table

    to explain this code a little bit , the datatable consists of DataRows , the datarow consists of cells every cell for particular column ,
    there is other object called dataview its a special (filtered and sorted) form of datatable . the bindingsource contain a dataview underneath it, the dataview consists of DataRowView. so when you use bindingsource.current it return a datarowview but as object so i type cast this object back to datarawview to be able to use the colum index to read the value in particular cell( the sector id in investmentbindingsource), then i changed the sector bindingsource postition to select correspondent record( which will change the displayed value in the combobox)

    hope this helps



  • KooT

    shakalama,

    Thanks for your help. I finally got this to work but I had to change the following line

    this.fortunaDataSet.Investment.Rows[selectedInvestment].ItemArray[4] = this.fortunaDataSet.Sector[ind].ID;

    to

    this.fortunaDataSet.Investment.Rows[selectedInvestment][4] = SectorComboBox.SelectedValue.ToString();

    Why doesn't the first version work


  • Carl97

    Thanks for this thread, I had the same problem:

    object v = val;

    row.ItemArray[colIndex] = v;

    simply left the ItemArray elements at DBNull, whereas

    object v = val;

    row[colIndex] = v;

    worked fine. I really would like to know why!

    Greetings, Torsten



  • Updating a Typed Table