我遇到以下代码问题:
For x As Integer = 0 To family_grid.RowCount
family_composition = family_grid.Rows(x).Cells(0).Value
age = family_grid.Rows(x).Cells(1).Value
status = family_grid.Rows(x).Cells(2).Value
relation = family_grid.Rows(x).Cells(3).Value
educational_attainment = family_grid.Rows(x).Cells(4).Value
occupation = family_grid.Rows(x).Cells(5).Value
income = family_grid.Rows(x).Cells(6).Value
dbConnect.Open()
Dim str1 As String = "INSERT INTO tbl_general_intake_family(id, family_composition, age, status, relation, educational_attainment, occupation, income)values(id, family_composition, age, status, relation, educational_attainment, occupation, income)"
Dim da As New OleDbDataAdapter()
Dim ds As New DataSet()
Dim com As New OleDb.OleDbCommand(str1, dbConnect)
com.Parameters.AddWithValue("id", var_document_id)
com.Parameters.AddWithValue("family_composition", family_composition)
com.Parameters.AddWithValue("age", age)
com.Parameters.AddWithValue("status", status)
com.Parameters.AddWithValue("relation", relation)
com.Parameters.AddWithValue("educational_attainment", educational_attainment)
com.Parameters.AddWithValue("occupation", occupation)
com.Parameters.AddWithValue("income", income)
com.ExecuteNonQuery()
com.Dispose()
dbConnect.Close()
Next
for循环永远不会执行,因为family_grid.RowCount
返回的值为零。
在form_load event
中,我使用以下代码填充DataGridView
:
Dim mycon As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=" + My.Application.Info.DirectoryPath + "\mswd-rms.mdb")
mycon.Open()
Dim mycomm As New OleDbCommand("SELECT * FROM tbl_general_intake_family WHERE id=" & main_general_intake.document_id, mycon)
Dim da As New OleDbDataAdapter()
da.SelectCommand = mycomm
Dim ds As New DataSet()
da.Fill(ds)
family_grid.Columns.Clear()
family_grid.ReadOnly = true
family_grid.DataSource = ds.Tables(0)
family_grid.Columns(0).HeaderText = "ID"
family_grid.Columns(1).HeaderText = "Name"
family_grid.Columns(2).HeaderText = "Age"
family_grid.Columns(3).HeaderText = "Status"
family_grid.Columns(4).HeaderText = "Relation"
family_grid.Columns(5).HeaderText = "Educational Attainment"
family_grid.Columns(6).HeaderText = "Occupation"
family_grid.Columns(7).HeaderText = "Income"
答案 0 :(得分:0)
如果在Form_Load中使用单行填充网格,则RowCount无法为零。 RowCount 应该返回1.
要解决此问题,请将数据表保存到私有表单级变量并使用其Row.Count。
private dtCurrentGrid As DataTable
..
dtCurrentGrid = ds.Tables(0)
..
For x As Integer = 0 To dtCurrentGrid.Rows.Count