hi all,
i am trying to add check box to my datagrid view and update checked rows to sql server. please help me.

i am using vs 2005 and sql 2000

thanks in advance.

Dani AI

Generated

Useful follow-up to the suggestions from and : placing a CheckBox inside a TemplateField (or using a boolean column in the data source) is the right start. The essential next steps are to associate a stable primary key with each row (DataKeyNames on GridView or store the key in a hidden field), iterate the rows server-side to read each CheckBox.Checked, and run parameterized UPDATEs inside a single open connection and transaction so partial failures can be rolled back. With VS2005 + SQL Server 2000, avoid modern features like table-valued parameters (not available); use safe, parameterized commands instead.

Example server-side pattern (VB.NET WebForms GridView): open one SqlConnection, begin a SqlTransaction, prepare a single SqlCommand with parameters, then loop rows and set parameter values for each checked/unchecked row and ExecuteNonQuery. This keeps the DB round-trips minimal and avoids SQL injection.

Protected Sub btnSave_Click(sender As Object, e As EventArgs)
    ' Open connection once, begin transaction, prepare UPDATE with @IsChecked and @Id parameters
    ' For each GridViewRow: cb = CType(row.FindControl("chkSelect"), CheckBox)
    ' id = Convert.ToInt32(GridView1.DataKeys(row.RowIndex).Value)
    ' cmd.Parameters("@IsChecked").Value = cb.Checked
    ' cmd.Parameters("@Id").Value = id
    ' cmd.ExecuteNonQuery()
    ' Commit or rollback the transaction on completion/error
End Sub

Troubleshooting pointers: confirm the CheckBox ID and that it is in ItemTemplate (not only EditTemplate), set DataKeyNames to the PK, bind boolean fields to the checkbox, log exceptions from the transaction, and test with a small dataset first to verify concurrency and performance.

Recommended Answers

All 3 Replies

What exactly is the help you need? What are you doing now and what problem are you having?
You need to specify your problem, or nobody will answer.

Add a template field in the datagrid edit columns field.
Then in edit template fields add the check box and assign the problem as needed

Add boolean field to your datasource,

Dim dt As New DataTable
        dt.Columns.Add("Test", GetType(Boolean))
        dt.Rows.Add(True)
        dt.Rows.Add(False)
        DataGridView1.DataSource = dt
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.