将DataSet转换为DataGridView帮助 [英] DataSet to DataGridView Help
问题描述
如何遍历数据集并填充DataGridView-这是我的代码.希望有人可以指出我出了什么问题...没有得到特定的错误,只是出现了错误的消息框.
How do I loop through a dataset and populate a DataGridView - heres my code. Hopefully someone can point out where Im going wrong... Not getting a specific error, just failed message box.
当我取出try/catch函数时,我没有给出任何错误,但DataGridView没有填充.
when i take out the try/catch function then Im given no error but the DataGridView does not populate.
Select Case Trim(ComboBox1.SelectedValue).ToLower
Case "january"
val1 = "01"
Case "february"
val1 = "02"
Case "march"
val1 = "03"
Case "april"
val1 = "04"
Case "may"
val1 = "05"
Case "june"
val1 = "06"
Case "july"
val1 = "07"
Case "august"
val1 = "08"
Case "september"
val1 = "09"
Case "october"
val1 = "10"
Case "november"
val1 = "11"
Case "december"
val1 = "12"
End Select
Dim newRecordDate As String = ComboBox2.SelectedValue & "-" & val1
MsgBox(newRecordDate)
MsgBox(Form1.Label4.Text)
Dim val As String = ComboBox1.SelectedValue & " " & ComboBox2.SelectedValue
Dim con As New SqlClient.SqlConnection, cmd As New SqlClient.SqlCommand, adp As New SqlClient.SqlDataAdapter, ds As New DataSet, dt As New DataTable
Dim sql As String = "Select PUID, CatA, MarketingCode, CatC, Cost, Currency, CatB , Comment, RecordDate from vw_tblP_Usage_Details where empid = '" & Form1.Label4.Text & "' AND left(RecordDate,7) = '" & newRecordDate & "'"
cmd.Connection = con
cmd.CommandText = sql
adp.SelectCommand = cmd
cmd.Connection.Open()
adp.Fill(ds)
cmd.Connection.Close()
dt = ds.Tables(0)
'DataGridView1.DataSource = ds.Tables(0)
Try
For i As Integer = 0 To dt.Rows.Count - 1
DataGridView1.Rows(i).Cells(payment.Name).Value = dt.Rows(0)("CatA")
DataGridView1.Rows(i).Cells(Column1.Name).Value = dt.Rows(0)("CatB")
DataGridView1.Rows(i).Cells(Column2.Name).Value = dt.Rows(0)("CatC")
DataGridView1.Rows(i).Cells(markCode.Name).Value = dt.Rows(0)("MarketingCode")
DataGridView1.Rows(i).Cells(lineComment.Name).Value = dt.Rows(0)("Comment")
Next
MsgBox("failed")
Catch ex As Exception
End Try
推荐答案
Hello Brianm888,
Hello Brianm888,
如何遍历数据集并填充DataGridView-这是我的代码.希望有人可以指出我出了什么问题...没有得到特定的错误,只是出现了错误的消息框.
How do I loop through a dataset and populate a DataGridView - heres my code. Hopefully someone can point out where Im going wrong... Not getting a specific error, just failed message box.
当我取出try/catch函数时,我没有给出任何错误,但DataGridView没有填充.
when i take out the try/catch function then Im given no error but the DataGridView does not populate.
Select Case Trim(ComboBox1.SelectedValue).ToLower
Case "january"
val1 = "01"
Case "february"
val1 = "02"
Case "march"
val1 = "03"
Case "april"
val1 = "04"
Case "may"
val1 = "05"
Case "june"
val1 = "06"
Case "july"
val1 = "07"
Case "august"
val1 = "08"
Case "september"
val1 = "09"
Case "october"
val1 = "10"
Case "november"
val1 = "11"
Case "december"
val1 = "12"
End Select
Dim newRecordDate As String = ComboBox2.SelectedValue & "-" & val1
MsgBox(newRecordDate)
MsgBox(Form1.Label4.Text)
Dim val As String = ComboBox1.SelectedValue & " " & ComboBox2.SelectedValue
Dim con As New SqlClient.SqlConnection, cmd As New SqlClient.SqlCommand, adp As New SqlClient.SqlDataAdapter, ds As New DataSet, dt As New DataTable
Dim sql As String = "Select PUID, CatA, MarketingCode, CatC, Cost, Currency, CatB , Comment, RecordDate from vw_tblP_Usage_Details where empid = '" & Form1.Label4.Text & "' AND left(RecordDate,7) = '" & newRecordDate & "'"
cmd.Connection = con
cmd.CommandText = sql
adp.SelectCommand = cmd
cmd.Connection.Open()
adp.Fill(ds)
cmd.Connection.Close()
dt = ds.Tables(0)
'DataGridView1.DataSource = ds.Tables(0)
Try
For i As Integer = 0 To dt.Rows.Count - 1
DataGridView1.Rows(i).Cells(payment.Name).Value = dt.Rows(0)("CatA")
DataGridView1.Rows(i).Cells(Column1.Name).Value = dt.Rows(0)("CatB")
DataGridView1.Rows(i).Cells(Column2.Name).Value = dt.Rows(0)("CatC")
DataGridView1.Rows(i).Cells(markCode.Name).Value = dt.Rows(0)("MarketingCode")
DataGridView1.Rows(i).Cells(lineComment.Name).Value = dt.Rows(0)("Comment")
Next
MsgBox("failed")
Catch ex As Exception
End Try
这篇关于将DataSet转换为DataGridView帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!