Asp.net在gridview页脚中显示总列数

时间:2016-10-27 00:19:08

标签: javascript asp.net vb.net gridview

我有一个上传excel文件的程序。上传后,excel文件的值将在gridview中传输。

我想要做的是获得每列的总价值并将其放在页脚中。有人可以帮我这个吗?谢谢。

这是我到目前为止所拥有的。

 Protected Sub Button1_Click(sender As Object, e As System.EventArgs)Handles Button1.Click
        Dim fl As String = FileUpload1.PostedFile.FileName
        GridView1.DataSource = OpenExcelFile(fl)
        GridView1.DataBind()
        savetoDB(OpenExcelFile(fl))
 End Sub

Protected Function OpenExcelFile(ByVal fileName As String) As Object
    Dim dataTable As New System.Data.DataTable()
    Dim exl As String = FileUpload1.PostedFile.FileName
    Dim connectionString As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & exl & ";Extended Properties=" & """Excel 12.0 Xml;HDR=YES;"""
    Dim adapter As New OleDbDataAdapter("SELECT [NAME],[FIL],[ENG],[SCI],[MTH]", connectionString)
    adapter.Fill(dataTable)
    Return dataTable
End Function

Protected Sub savetoDB(dt As System.Data.DataTable)
    Dim cmd As New SqlCommand
    Dim con As New SqlConnection("myconnectionhere")
    Dim dt2 As New System.Data.DataTable
    dt2 = OpenExcelFile(FileUpload1.PostedFile.FileName)

    Using bulk As SqlBulkCopy = New SqlBulkCopy(con)
        bulk.DestinationTableName = "studnt_table"
        bulk.ColumnMappings.Add("[NAME]", "st_name")
        bulk.ColumnMappings.Add("[FIL]", "sbj_fil")
        bulk.ColumnMappings.Add("[ENG]", "sbj_eng")
        bulk.ColumnMappings.Add("[SCI]", "sbj_sci")
        bulk.ColumnMappings.Add("[MTH]", "sbj_mth")
        con.Open()
        bulk.WriteToServer(dt2)
        con.Close()
        con.Close()
    End Using
End Sub

这是我的前端          

这是我想要实现的示例布局。我希望得到列中的总和,并在页脚中显示总数。有谁知道我怎么能做到这一点?任何帮助将非常感激。谢谢你/

enter image description here

1 个答案:

答案 0 :(得分:1)

试试这个。我只为1列添加了样本以获得一个想法。在OpenExcelFile IsNull Check

之后延长adatapter.Fill(dataTable)
adatapter.Fill(dataTable)
Dim sumRow As DataRow
Dim Col1Tot,Col2Tot,Col3Tot,Col4Tot As Decimal 

For Each row1 As DataRow In dataTable.Rows  
   If Not row1.IsNull("FIL") Then 
     Col1Tot =  Col1Tot + Convert.toDecimal(row1("FIL"))
   End If 
   If Not row1.IsNull("ENG") Then 
     Col2Tot =  Col2Tot + Convert.toDecimal(row1("ENG"))
   End If 
   If Not row1.IsNull("SCI") Then 
     Col3Tot =  Col3Tot + Convert.toDecimal(row1("SCI"))
   End If   
   If Not row1.IsNull("MTH") Then 
     Col4Tot =  Col4Tot + Convert.toDecimal(row1("MTH"))
   End If 
Next row1

'Do the remaining columns in same way   
'Finally add it to the Datatable as a new row
sumRow =  dataTable.NewRow()
sumRow("FIL") = Col1Tot 
dataTable.Rows.Add(sumRow)

sumRow =  dataTable.NewRow()
sumRow("ENG") = Col2Tot 
dataTable.Rows.Add(sumRow)

sumRow =  dataTable.NewRow()
sumRow("SCI") = Col3Tot 
dataTable.Rows.Add(sumRow)

sumRow =  dataTable.NewRow()
sumRow("MTH") = Col4Tot 
dataTable.Rows.Add(sumRow)

return dataTable;

让我们知道您的发现。