Non so se è la maniera più veloce ma funziona.
ora il problema è fargli scrivere il secondo datatable creato con le media, in un file txt che ha la stessa struttura di quello che aprivo
Dim nomeFile As String = "E:\_Lavori\_Visual_Basic\07_VarieRFAEM\wetransfer_dirx_slu_zona_unica-txt_2023-09-05_1225\prova_media3.txt"
Dim TXT_Datatable As DataTable = txt_to_data(nomeFile, True, Space(1))
Dim TXTMedia_Datatable As DataTable = TXT_Datatable.Clone
'ImpData(nomeFile)
Dim ListaArea As New List(Of Double)
Dim UniqueAREA As System.Collections.Generic.List(Of String) = ((From T In TXT_Datatable.AsEnumerable() Select T.Field(Of String)("Area")).Distinct()).OrderBy(Function(s) s).ToList()
Dim UniqueCASE As System.Collections.Generic.List(Of String) = ((From T In TXT_Datatable.AsEnumerable() Select T.Field(Of String)("OutputCase")).Distinct()).OrderBy(Function(s) s).ToList()
Dim TXTdataview As DataView = TXT_Datatable.AsDataView
Me.DataGridView1.DataSource = TXTdataview
Me.Update()
TXTdataview.Sort = "Area, Joint, OutputCase"
Me.DataGridView1.DataSource = TXTdataview
Me.Update()
If UniqueAREA.Item(0) Is Nothing Then UniqueAREA.RemoveAt(0)
If UniqueCASE.Item(0) Is Nothing Then UniqueCASE.RemoveAt(0)
UniqueAREA.Sort()
UniqueCASE.Sort()
For Each TXT_AREA As String In UniqueAREA
For Each TXT_OUTPUTCASE As String In UniqueCASE
Dim filterstr As String = "Area = " & "'" & TXT_AREA & "' " & "AND " & "Outputcase = " & "'" & TXT_OUTPUTCASE & "'"
'"Area = '23' AND OutputCase '46'"
TXTdataview.RowFilter = filterstr
Me.DataGridView1.DataSource = TXTdataview
Me.Update()
Dim F11 As Double = 0
Dim F22 As Double = 0
Dim F12 As Double = 0
Dim FMax As Double = 0
Dim FMin As Double = 0
Dim FAngle As Double = 0
Dim FVM As Double = 0
Dim M11 As Double = 0
Dim M22 As Double = 0
Dim M12 As Double = 0
Dim MMax As Double = 0
Dim MMin As Double = 0
Dim MAngle As Double = 0
Dim V13 As Double = 0
Dim V23 As Double = 0
Dim VMax As Double = 0
Dim VAngle As Double = 0
Dim Area_media As String = ""
Dim AreaElem_Media As String = ""
Dim Shell_Media As String = ""
Dim Joint_Media As String = ""
Dim OutputCase_Media As String = ""
Dim CaseType_Media As String = ""
Dim StepType_Media As String = ""
Dim F11_Media As Double = 0
Dim F22_Media As Double = 0
Dim F12_Media As Double = 0
Dim FMax_Media As Double = 0
Dim FMin_Media As Double = 0
Dim FAngle_Media As Double = 0
Dim FVM_Media As Double = 0
Dim M11_Media As Double = 0
Dim M22_Media As Double = 0
Dim M12_Media As Double = 0
Dim MMax_Media As Double = 0
Dim MMin_Media As Double = 0
Dim MAngle_Media As Double = 0
Dim V13_Media As Double = 0
Dim V23_Media As Double = 0
Dim VMax_Media As Double = 0
Dim VAngle_Media As Double = 0
For Each dataRowTMP As DataRowView In TXTdataview
Area_media = dataRowTMP.Item(0)
AreaElem_Media = dataRowTMP.Item(1)
Shell_Media = dataRowTMP.Item(2)
Joint_Media = "CENTER"
OutputCase_Media = dataRowTMP.Item(4)
CaseType_Media = dataRowTMP.Item(5)
StepType_Media = "Min/MAX"
F11 += Double.Parse(dataRowTMP.Item(7).ToString.Replace(".", ","))
F22 += Double.Parse(dataRowTMP.Item(8).ToString.Replace(".", ","))
F12 += Double.Parse(dataRowTMP.Item(9).ToString.Replace(".", ","))
FMax += Double.Parse(dataRowTMP.Item(10).ToString.Replace(".", ","))
FMin += Double.Parse(dataRowTMP.Item(11).ToString.Replace(".", ","))
FAngle += Double.Parse(dataRowTMP.Item(12).ToString.Replace(".", ","))
FVM += Double.Parse(dataRowTMP.Item(13).ToString.Replace(".", ","))
M11 += Double.Parse(dataRowTMP.Item(14).ToString.Replace(".", ","))
M22 += Double.Parse(dataRowTMP.Item(15).ToString.Replace(".", ","))
M12 += Double.Parse(dataRowTMP.Item(16).ToString.Replace(".", ","))
MMax += Double.Parse(dataRowTMP.Item(17).ToString.Replace(".", ","))
MMin += Double.Parse(dataRowTMP.Item(18).ToString.Replace(".", ","))
MAngle += Double.Parse(dataRowTMP.Item(19).ToString.Replace(".", ","))
V13 += Double.Parse(dataRowTMP.Item(20).ToString.Replace(".", ","))
V23 += Double.Parse(dataRowTMP.Item(21).ToString.Replace(".", ","))
VMax += Double.Parse(dataRowTMP.Item(22).ToString.Replace(".", ","))
VAngle += Double.Parse(dataRowTMP.Item(23).ToString.Replace(".", ","))
Next
F11_Media = Math.Round(F11 / TXTdataview.Count, 2)
F22_Media = Math.Round(F22 / TXTdataview.Count, 2)
F12_Media = Math.Round(F12 / TXTdataview.Count, 2)
FMax_Media = Math.Round(FMax / TXTdataview.Count, 2)
FMin_Media = Math.Round(FMin / TXTdataview.Count, 2)
FAngle_Media = Math.Round(FAngle / TXTdataview.Count, 2)
FVM_Media = Math.Round(FVM / TXTdataview.Count, 2)
M11_Media = Math.Round(M11 / TXTdataview.Count, 2)
M22_Media = Math.Round(M22 / TXTdataview.Count, 2)
M12_Media = Math.Round(M12 / TXTdataview.Count, 2)
MMax_Media = Math.Round(MMax / TXTdataview.Count, 2)
MMin_Media = Math.Round(MMin / TXTdataview.Count, 2)
MAngle_Media = Math.Round(MAngle / TXTdataview.Count, 2)
V13_Media = Math.Round(V13 / TXTdataview.Count, 2)
V23_Media = Math.Round(V23 / TXTdataview.Count, 2)
VMax_Media = Math.Round(VMax / TXTdataview.Count, 2)
VAngle_Media = Math.Round(VAngle / TXTdataview.Count, 2)
Dim drNewRow As DataRow = TXTMedia_Datatable.NewRow
drNewRow(0) = Area_media
drNewRow(1) = AreaElem_Media
drNewRow(2) = Shell_Media
drNewRow(3) = Joint_Media
drNewRow(4) = OutputCase_Media
drNewRow(5) = CaseType_Media
drNewRow(6) = StepType_Media
drNewRow(7) = F11_Media
drNewRow(8) = F22_Media
drNewRow(9) = F12_Media
drNewRow(10) = FMax_Media
drNewRow(11) = FMin_Media
drNewRow(12) = FAngle_Media
drNewRow(13) = FVM_Media
drNewRow(14) = M11_Media
drNewRow(15) = M22_Media
drNewRow(16) = M12_Media
drNewRow(17) = MMax_Media
drNewRow(18) = MMin_Media
drNewRow(19) = MAngle_Media
drNewRow(20) = V13_Media
drNewRow(21) = V23_Media
drNewRow(22) = VMax_Media
drNewRow(23) = VAngle_Media
TXTMedia_Datatable.Rows.Add(drNewRow)
Me.DataGridView1.DataSource = TXTdataview
Me.DataGridView2.DataSource = TXTMedia_Datatable
Me.Update()
Next
Next