views:

55

answers:

2

I have a dataset, and 2 datatables.

Datatable1 = Combobox source (This will display a list of options) Datatable2 = DataGrid (This will display data relevant to the options in combo box) Submit Button (populate datagrid based on combo box selected value)

When i select an item in combo box and click submit, it load up the relevant records in datagrid. If i then change a value in the datagrid and click the submit button, the value i have just changed, dissapears?

How can i make it so that any altered datagrid values amend the datable, so that even if i view different options, i can always return any, an retain any of the changed values?

Here is my code:

    //Load the data grid according to the ComboCAtegory selection
    public void Grid_Load()
    {

        DataSet();
        var Result = from c in DataSet_Main.Tables[2].AsEnumerable()
                     where c.Field<string>("Test_Code").Equals(comboBox_CategorySelect.SelectedValue)
                     select c;

        dataGridView_Main.DataSource = Result.AsDataView();

        dataGridView_Main.Columns["Test_Code"].Visible = false;
        dataGridView_Main.Columns["ID"].Visible = false;
        dataGridView_Main.Columns["Description"].Visible = false;
        dataGridView_Main.Columns["Expected_Result"].Visible = false;


    }

    private void buttonSubmit_Click(object sender, EventArgs e)
    {
        Grid_Load();
    }

    public void Fail()
    {

        DataTable dt = DataSet_Main.Tables[2];

        //dataGridView_Main.SelectedRows[0].Cells["Check"].Value = "Fail";
        dt.Rows[dataGridView_Main.SelectedRows[0].Index]["Check"] = "Fail";

    }



    private void buttonFail_Click(object sender, EventArgs e)
    {
        Fail();
    }

Hope this makes sense?

A: 

I think your DataGrid is already bound to the Data Table. What you need to do is send the changes back to the data source so that they would be reflected in the second data table which is bound to the same data source. To do this, write an event handler for CellChanging event on the DataGrid and in that you can the call Update() method on your Data Adapter (if you are using one, that is) to send changes to the data source. Then, in the same event handler, update the items in the combo box by refreshing the data bind so that the combo box gets latest values from the second data table.

This way, whenever the cell changes its value in the DataGrid, you can check if it is the relevant cell which you want and update the combo box based on the changes in the data grid.

Mamta Dalal
A: 

Apologies my bad..i am a boof head.

Tha datagrid IS bound automatically. Ive just realised i was calling my initial dataset() method - which is calling my database, in my datagrid_load method. thus everytime i was populating the datagrid, it was actually refreshing from the database not the datatable.

Thankyou your repy tho..

Super_Villan