0

以下のコードで問題が発生しています:

 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

family_grid.RowCountはゼロの値を返すため、forループは実行されません。

で、次のコードを使用して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"
4

1 に答える 1

0

グリッドにForm_Loadの単一の行が入力されている場合、RowCountをゼロにする方法はありません。RowCount1を返す必要があります。

この問題を回避するには、データテーブルをプライベートフォームレベルの変数に保存し、そのRow.Countを使用します。

private dtCurrentGrid As DataTable

..

dtCurrentGrid = ds.Tables(0)

..

 For x As Integer = 0 To dtCurrentGrid.Rows.Count
于 2013-01-21T04:50:09.470 に答える