Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim sum As Double = 0 'หาผมรวม
Try
DataGridView1.Rows.Clear()
Dim objDataAdapter As New OleDbDataAdapter()
objDataAdapter.SelectCommand = ExcelConnection()
Dim objDataSet As New DataSet()
objDataAdapter.Fill(objDataSet)
Dim countcol As String = 0
countcol = objDataSet.Tables(0).Columns.Count
Dim anydata(countcol + 2) As String
Dim x As Integer = 0
For i As Integer = 0 To objDataSet.Tables(0).Rows.Count - 1
sum = 0
anydata.SetValue((i + 1).ToString, 0) 'number of No.
For index As Integer = 0 To countcol - 1 'วนลำดับ Index ไปเรื่อยๆ ที่-1 เพราะมันมี No ไปแล้ว
anydata.SetValue(objDataSet.Tables(0).Rows(i).Item(index).ToString, index + 1) 'วนจำนวนคอลัมภ์ที่เหลือที่ต้องการ
If index > 1 Then
sum = sum + (objDataSet.Tables(0).Rows(i).Item(index))
End If
x = index
Next
anydata.SetValue(sum.ToString, x + 2)
anydata.SetValue("-", x + 3)
DataGridView1.Rows.Add(anydata)
Next
objDataSet.Clear()
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Tag : .NET, Ms SQL Server 2008, VB.NET, VS 2008 (.NET 3.x), Windows