简体   繁体   English

如何从我的DataGridView获取每个组合框的所有值并将其添加到DataTable?

[英]How to get all values of every combobox from my DataGridView and Add them to DataTable?

I added a ComboBox to my DataGridView 我在我的DataGridView中添加了一个ComboBox

DBAccess dba = new DBAccess();
DataTable dt = dba.spGetASTMTable(); // Getting Data from DataBase for Grid
DataTable dtOrders = dba.spGetOrdersList(); / Getting Data from DataBase for ComboBox
// Create ComboBox
var cboDbTableColumn = new DataGridViewComboBoxColumn();
cboDbTableColumn.DataSource = dtOrders;
cboDbTableColumn.DataPropertyName = "OrdersID";
cboDbTableColumn.DisplayMember = "Orders";
cboDbTableColumn.HeaderText = "DbTableColumn";

gvASTMOrderMessage.Columns.Add(cboDbTableColumn);

gvASTMOrderMessage.DataSource = dt;

Then I run my app and change couple ComboBoxes. 然后,我运行我的应用程序并更改几个组合框。 I Click button "Save" and I want to get DataTable with all DataGridView Data. 我单击按钮“保存”,我想获取包含所有DataGridView数据的DataTable。 As I understand, because of my ComboBox is on a part of DataSource then I need to loop through whole Grid and manually add values of ComboBox to my DataTable. 据我了解,由于ComboBox位于DataSource的一部分上,因此我需要遍历整个Grid并手动将ComboBox的值添加到DataTable中。 I'm trying to do this step here: 我正在尝试在这里执行此步骤:

private void btnSave_Click(object sender, EventArgs e)
{
     DataTable data = (DataTable)(gvASTMOrderMessage.DataSource);
     data.Columns.Add("DbTableColumn");
     for (int i = 0; i < gvASTMOrderMessage.Rows.Count; i++)
     {
         data.Rows[i]["DbTableColumn"] = Convert.ToString((gvASTMOrderMessage.Rows[i].Cells[0] as DataGridViewComboBoxCell).FormattedValue.ToString());
     }
} 

But this doesn't work. 但这是行不通的。 I've tried to get 我试图得到

gvASTMOrderMessage.Rows[i].Cells[0].FormattedValue.ToString()
gvASTMOrderMessage.Rows[i].Cells[0].Value.ToString()
gvASTMOrderMessage.Rows[i].Cells[0].EditedFormattedValue.ToString()

But nothing of this works... 但是这些都不起作用...

Help please, how to get all values of every combobox from my DataGridView and Add them to DataTable? 请帮忙,如何从我的DataGridView获取每个组合框的所有值并将其添加到DataTable?

DataSource and DataGridView are two different objects which are somehow coordinated. DataSourceDataGridView是两个不同的对象,它们以某种方式进行了协调。 But this coordination is not perfect and, actually, can easily drive to problems (deleting elements from the DataSource and accessing those indices from the DataGridView ). 但是这种协调并不是完美的,实际上,很容易引发问题(从DataSource删除元素并从DataGridView访问那些索引)。 That's why, I always prefer to affect both objects at the same time ( DataSource and DataGridView ). 这就是为什么,我总是喜欢同时影响两个对象( DataSourceDataGridView )。

In any case, bear in mind that quite a few changes you perform in the DataGridView (eg, adding/removing columns) are not automatically reflected in the DataSource (although it can happen inversely). 无论如何,请记住,您在DataGridView执行的许多更改(例如,添加/删除列)不会自动反映在DataSource (尽管它可以相反地发生)。 In your specific situation, this behaviour makes still more sense as far as automating the inclusion of a DataGridViewComboBoxColumn type column into a DataSource (perhaps a DataTable , perhaps other object) would be virtually impossible (many alternatives, no one objectively better than the others). 在您的特定情况下,从自动将DataGridViewComboBoxColumn类型的列自动包含到DataSource (也许是一个DataTable ,也许是其他对象)到实际上将是不可能的(许多替代方法,在客观上没有一个优于其他方法) 。

You should come up with a way to fit the comboboxes into your DataTable (for example: populate in the given column as many rows as items in the combobox and "mark" this column in some way) and implement a code performing the corresponding updating actions when required. 您应该想出一种方法来使组合框适合您的DataTable (例如:在给定列中填充与组合框中的项目一样多的行,并以某种方式“标记”该列)并实现执行相应更新操作的代码在需要的时候。

Well, you should check the 好吧,你应该检查

DataTable data = (DataTable)(gvASTMOrderMessage.DataSource);

check if the datatable is null (mine was). 检查数据表是否为空(我的是)。 The rest of the code seems ok, also check 其余代码似乎还可以,也请检查

data.Rows[i]["DbTableColumn"] = Convert.ToString((gvASTMOrderMessage.Rows[i].Cells[0] as DataGridViewComboBoxCell).FormattedValue.ToString()); data.Rows [i] [“ DbTableColumn”] = Convert.ToString((gvASTMOrderMessage.Rows [i] .Cells [0] as DataGridViewComboBoxCell).FormattedValue.ToString()); } }

i would do something like 我会做类似的事情

DataGridViewComboBoxCell cell = (DataGridViewComboBoxCell)dataGridView1.Rows[i].Cells[0];
                data.Rows[i]["DbTableColumn"] = cell.FormattedValue.ToString());

which is a more readable version. 这是更具可读性的版本。

Hope this helps! 希望这可以帮助!

Maybe I don't understand something but look the following code: I have added the extra "ComboID" column, and after you have selected items in the gridview and clicked the "Save" button the datatable will contain the IDs from the combo. 也许我不明白,但是看下面的代码:我添加了额外的“ ComboID”列,并且在gridview中选择了项目并单击“保存”按钮后,数据表将包含组合中的ID。

    private void Form1_Load(object sender, EventArgs e)
    {
        var dt = new DataTable();
        dt.Columns.Add("Id");
        dt.Columns.Add("Value");
        dt.Columns.Add("ComboID");

        var drow = dt.NewRow();
        drow[0] = 10;
        drow[1] = "abc";
        dt.Rows.Add(drow);

        drow = dt.NewRow();
        drow[0] = 20;
        drow[1] = "xy";
        dt.Rows.Add(drow);

        dataGridView1.AutoGenerateColumns = false;
        dataGridView1.Columns.Add(new DataGridViewTextBoxColumn { DataPropertyName = "Id" });
        dataGridView1.Columns.Add(new DataGridViewTextBoxColumn { DataPropertyName = "Value" });

        var dtOrders = new DataTable();
        dtOrders.Columns.Add("OrdersID");
        dtOrders.Columns.Add("Orders");

        drow = dtOrders.NewRow();
        drow[0] = 1;
        drow[1] = "Order1";
        dtOrders.Rows.Add(drow);

        drow = dtOrders.NewRow();
        drow[0] = 2;
        drow[1] = "Order2";
        dtOrders.Rows.Add(drow);

        var combo = new DataGridViewComboBoxColumn();
        combo.DataSource = dtOrders;
        combo.DataPropertyName = "ComboID";
        combo.ValueMember = "OrdersID";
        combo.DisplayMember = "Orders";
        dataGridView1.Columns.Add(combo);

        dataGridView1.DataSource = dt;
    }

    private void button1_Click(object sender, EventArgs e)
    {
        var dt = dataGridView1.DataSource as DataTable;
    }

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

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