Hi,

I want to develop a form in which i have a grid like structure(rows and columns). i can enter data in as many lines as i want. And in one cloumn i want to add a checkbox. When i click the Save button on the form then only those rows must be added to database whose checkbox is checked.

Can anybody help me with this. i have tried gridcontrol but i can't find a solution to this.

Regards.

Dani AI

Generated

A clear, safe pattern for the scenario described by : use a grid with a boolean checkbox column, commit any pending edits, iterate rows, and insert only those rows whose checkbox is checked. was correct that a grid is appropriate; ’s VS2003 sample is useful if using the older DataGrid. The snippet below shows a practical VB.NET pattern (DataGridView + SqlClient); the same logic applies for Access (use OleDbCommand and positional ? parameters there).

Commit the edit, skip the new row, handle DBNull/Nothing, reuse a parameterized command inside a transaction, and be sure checkbox edits are committed (see CurrentCellDirtyStateChanged/CommitEdit if live toggles are missing). Example:

DataGridView1.EndEdit()

Using conn As New System.Data.SqlClient.SqlConnection(connString)
    conn.Open()
    Using tran = conn.BeginTransaction()
        Using cmd = New System.Data.SqlClient.SqlCommand("INSERT INTO MyTable (ColA, ColB) VALUES (@a, @b)", conn, tran)
            cmd.Parameters.Add("@a", System.Data.SqlDbType.VarChar, 100)
            cmd.Parameters.Add("@b", System.Data.SqlDbType.Int)
            For Each row As DataGridViewRow In DataGridView1.Rows
                If row.IsNewRow Then Continue For
                Dim isChecked As Boolean = False
                If row.Cells("chk").Value IsNot Nothing AndAlso Not Convert.IsDBNull(row.Cells("chk").Value) Then
                    isChecked = CBool(row.Cells("chk").Value)
                End If
                If isChecked Then
                    Dim v1 = row.Cells("colA").Value
                    Dim v2 = row.Cells("colB").Value
                    cmd.Parameters("@a").Value = If(v1 Is Nothing OrElse Convert.IsDBNull(v1), DBNull.Value, v1)
                    cmd.Parameters("@b").Value = If(v2 Is Nothing OrElse Convert.IsDBNull(v2), DBNull.Value, v2)
                    cmd.ExecuteNonQuery()
                End If
            Next
        End Using
        tran.Commit()
    End Using
End Using

Notes and pitfalls: call DataGridView.CommitEdit(DataGridViewDataErrorContexts.Commit) from CurrentCellDirtyStateChanged when checkboxes change focus immediately; for Access use OleDbCommand with ? placeholders and add parameters in order; for many rows prefer building a DataTable + SqlBulkCopy or a batched insert for performance; always use parameterized commands to avoid SQL injection.

Recommended Answers

All 2 Replies

datagrid will work fine.just find the checkboxes tat r checked n insert the records.for more help just visit my earlier posts.

datagrid will work fine.just find the checkboxes tat r checked n insert the records.for more help just visit my earlier posts.

Hey i hav a sample tht ive done in VS 2003 and i used SQL SERVER for my database.. ive provided for connectivity for MsAccess too.. Make modifications according to your need ..

Hope it helps you.. n the next time post some code for help don blindly ask for the code.. im not tryin to b rude.. Ppl wont help u out thtz y..:cheesy:

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.