[英]getting row value from datatable
Dim標志為Boolean = True
昏暗的FileName作為字符串= Path.GetFileName(FileUpload1.PostedFile.FileName)
昏暗的fileExtension為字符串= Path.GetExtension(FileUpload1.PostedFile.FileName)
昏暗的fileLocation為字符串= Server.MapPath(“〜/ Upload /”&fileName)
'Check whether file extension is xls or xslx
If fileExtension = ".xls" Then
connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & fileLocation & ";Extended Properties=""Excel 8.0;HDR=Yes;IMEX=2"""
ElseIf fileExtension = ".xlsx" Then
connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & fileLocation & ";Extended Properties=""Excel 12.0;HDR=Yes;IMEX=2"""
End If
'Create OleDB Connection and OleDb Command
Dim con As New OleDbConnection(connectionString)
Dim cmd As New OleDbCommand()
cmd.CommandType = System.Data.CommandType.Text
cmd.Connection = con
Dim dAdapter As New OleDbDataAdapter(cmd)
Dim dtExcelRecords As New DataTable()
con.Open()
Dim dtExcelSheetName As DataTable = con.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, Nothing)
Dim getExcelSheetName As String = dtExcelSheetName.Rows(0)("Table_Name").ToString()
cmd.CommandText = "SELECT * FROM [" & getExcelSheetName & "]"
dAdapter.SelectCommand = cmd
dAdapter.Fill(dtExcelRecords)
con.Close()
Dim row As DataRow
For Each row In dtExcelRecords.Rows
If Include.ConvertDbNullToEmpyString(row(2)) = "" Then
MsgBox("Error in saving!")
Exit For
End If
MsgBox(row(0) & " " & row(1) & " " & row(2) & " " & row(3))
Next
GridView1.DataSource = dtExcelRecords
GridView1.DataBind()
ViewState("file") = dtExcelRecords
Dim dataSet As DataSet = New DataSet("dataSet")
dataSet.Tables.Add(dtExcelRecords)
'' Display the DataSet contents as XML.
Console.WriteLine(dataSet.Tables(0).DataSet.GetXml())
我有用於將文件excel上載到數據表並在gridview中顯示的代碼。
在gridview中顯示后,已轉換為datatable並在gridview中顯示的excel值將保存到數據庫中。
我的問題 :
Dim row As DataRow For Each row In dtExcelRecords.Rows If Include.ConvertDbNullToEmpyString(row(2)) = "" Then MsgBox("Error in saving!") Exit For End If MsgBox(row(0) & " " & row(1) & " " & row(2) & " " & row(3)) Next
那是獲取行值的代碼。
但是如何設置帶有新數據行的數據表呢?
如果我在第2行中具有空值,則我希望將proses輸入到datatable的數據停止! 並將新值從數據行值顯示到gridView中
謝謝
--------------------------------------編輯----------- -----------------------
例如:
上傳文件excel:
col1 ---- col2 ---- col3 ---- col4
tes1 ---- test1 ---- test1 ---- test1
tes2 ---- test2 ---- ---- test2
tes3 ---- test3 ---- test3 ---- test3
tes4 ---- test4 ---- ---- test4
並且gridview的結果必須是這樣的:
col1 ---- col2 ---- col3 ---- col4
tes1 ---- test1 ---- test1 ---- test1
tes3 ---- test3 ---- test3 ---- test3
如果將Exit For更改為Exit Sub,則此時將停止處理
嘗試,
Dim dtData As DataTable
dtData = New DataTable
dtData.Columns.Add("Row0")
dtData.Columns.Add("Row1")
dtData.Columns.Add("Row2")
dtData.Columns.Add("Row3")
Dim row As DataRow
For Each row In dtExcelRecords.Rows
If Include.ConvertDbNullToEmpyString(row(2)) = "" Then
MsgBox("Error in saving!")
Exit For 'or exit Sub
End If
Dim dr As DataRow = dtData.NewRow
dr("Row0") = row(0)
dr("Row1") = row(1)
dr("Row2") = row(2)
dr("Row3") = row(3)
dtData.Rows.Add(dr) 'Add this line
Next
'Bind dtData to gridview
'Save data in dtData to database
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.