0
我想計算一個數據表中的值並在其他數據表中顯示。如何計算其他DataTable中存在的行?
例如:
datatable1:源表(Inputdatagrid)
位置|值
001 | 2
002 | 1
003 | 5
001 | 1
datatable2:目標表(Resultdatagrid)
001 | 3 // 2 + 1
002 | 1
003 | 5
所以我希望做一個foreach看每一行的第一個數據表,然後我期待在第二個表中的位置值。如果位置值不存在,則創建一個新行...如果位置值存在,則計算值+。
我該怎麼做?我可以用linq做這個嗎?
這裏我的代碼:
...
int datagrid_input_counter = InputDataGrid.Rows.Count -1; //Anzahl Zeilen wird bestimmt
if (datagrid_input_counter > 0)
{
for (int i = 0; i < datagrid_input_counter; i++)
{
DataGridViewRow input_row = InputDataGrid.Rows[i]; // DataTable wird erstellt
string datagrid_input_position = Convert.ToString(input_row.Cells[0].Value); //Stelle Position
string datagrid_input_description = Convert.ToString(input_row.Cells[1].Value); //Stelle Beschreibung
string datagrid_input_sellvalue = Convert.ToString(input_row.Cells[2].Value); //Stelle ert Verkauf
//Add to Datatable
}
foreach (DataRow item in InputDataTable.Rows)
{
//How I get the result to ResultDatatable ????
}
}
else
{
//Tabelle ist leer
}
'Inputdatagrid'包含兩行'001'。你想按這個列分組並總結所有值嗎?這很令人困惑,因爲你提到了第二個表格(_「我在第二個表格中查找了位置值」_),但是你沒有看到(除了結果表格)。 – 2014-09-24 08:53:27
是的,我想分組這個001,並作出總和:) – Tarasov 2014-09-24 08:54:36
爲什麼你提到第二個表呢? – 2014-09-24 08:55:26