简体   繁体   English

如何将更改的datagrid值保存到数据表中?

[英]How do i save altered datagrid value into a datatable?

I have a dataset, and 2 datatables. 我有一个数据集和2个数据表。

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) Datatable1 =组合框源(将显示选项列表)Datatable2 = DataGrid(将显示与组合框中的选项相关的数据)提交按钮(根据组合框选择的值填充datagrid)

When i select an item in combo box and click submit, it load up the relevant records in datagrid. 当我在组合框中选择一个项目并单击提交时,它将在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? 我如何才能使任何更改的datagrid值修改datable,以便即使我查看不同的选项,我也可以始终返回任何值,并保留任何更改后的值?

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? 希望这有意义吗?

I think your DataGrid is already bound to the Data Table. 我认为您的DataGrid已绑定到数据表。 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. 为此,请在DataGrid上为CellChanging事件编写一个事件处理程序,并且可以在数据适配器上调用Update()方法(如果使用的是)将更改发送到数据源。 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. 这样,每当单元格在DataGrid中更改其值时,就可以检查它是否是所需的相关单元格,并根据数据网格中的更改更新组合框。

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. 我刚刚意识到我在用datagrid_load方法调用我的初始dataset()方法-正在调用数据库。 thus everytime i was populating the datagrid, it was actually refreshing from the database not the datatable. 因此,每次我填充数据网格时,实际上是从数据库而不是从数据表中刷新。

Thankyou your repy tho.. 谢谢您的调查。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM