Click here to Skip to main content
15,891,607 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,

i'm newbie and still learning.
i'm going to use datagridview checkbox column to check the desired records to insert
into sql server database, the process i want is after checking some of the checkBoxes and then do the insert query on a button click. The values i want to save (insert) into database is from selected row in datagridview.
how to do the code? (with example please).

please kindly help me, I appreciate every answer.

thank you.
Posted
Updated 17-Sep-13 18:10pm
v2

1 solution

You can simply loop the datagridview row using for, and inside the looping, use if to check if the checkbox is true, if it is then execute your insert query.

Improve :

C#
for (int i = 0; i <= this.dataGridView1.Rows.Count - 1; i++)
{
    if ((bool)this.dataGridView1.Rows[i].Cells["ColName"].Value == true)
    {
        //your query execution here
    }
}


not sure but, worth trying.
 
Share this answer
 
v2
Comments
Luvivica 18-Sep-13 0:19am    
thanks for the answer Midnight, but i'm still not get what you telling me. can you show some code for example?
Midnight Ahri 18-Sep-13 0:37am    
I changed the solution, "ColName" is the name of your checkbox column.
Tell me if it works.
Luvivica 18-Sep-13 1:18am    
not working..
the error display "object reference not set to an instance of an object"
Midnight Ahri 18-Sep-13 1:21am    
You had to make sure the cell had value.
Luvivica 18-Sep-13 1:44am    
i fill the datagridview on form load event, so yes it is have the value from database table i load.

i will illustrate my form,
my form have 3 columns, choose, benefit and detail.
when checkbox(choose) checked i want to save the checked row to new database table. at this step i can't do what i want.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900