Click here to Skip to main content
15,894,896 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I have requirement to add a checkbox column in gridview and we can check and uncheck these checkbox and according to that can update in database.
Please help in out. I am stuck in this portion.
Thanks in advance.
Posted
Comments
Sergey Alexandrovich Kryukov 9-Apr-14 15:59pm    
What do you mean by "window application"? Which application type?
—SA
ZurdoDev 9-Apr-14 16:02pm    
Where are you stuck?
saber rezaii magham 9-Apr-14 16:23pm    
Do you need a sample of a datagrid and data fill algorithm?
SumantaDash 10-Apr-14 12:55pm    
yes.please

Add DataGridViewCheckBoxColumn column to gridview and handle CellClick event

C#
if (dataGridRow.Cells["YourCheckboxColumn"].Value != null &&    (bool)dataGridRow.Cells["YourCheckboxColumn"].Value)
{
       // Checked
}
else if (dataGridRow.Cells["YourCheckboxColumn"].Value == null)
{
       // Unchecked
}



Call your db update method then.
 
Share this answer
 
You question is about DataGridView. GridView is for asp.net.
Refer:
1. How-create-check-box-column-datagridview[^]
2. insert-checked-rows-records-of-datagridview-into-database.aspx[^]
For 2, you should change the sql code to Parameterized queries to prevent SQL Injection Attacks in SQL Server[^]
 
Share this answer
 
v2
C#
//it will executed on click of any cell in gridview
        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
//it will check the specific column
            if (e.ColumnIndex == dataGridView1.Columns["status"].Index)
            {
                dataGridView1.Rows[e.RowIndex].Cells["status"].Value = !
                                     (bool)dataGridView1.Rows[e.RowIndex].Cells["status"].Value;
                SqlConnection con = new  SqlConnection(ConfigurationManager.ConnectionStrings["newConnectionString"].ConnectionString);
                
                String str = "update DemoTable set Status=" + status +" where Sln=" +    
                               dataGridView1.Rows[e.RowIndex].Cells["ID"].Value;
                con.Open();

                SqlCommand cmd = new SqlCommand(str, con);
                cmd.ExecuteNonQuery();
                
                con.Close();
               
            }
        }

//it is tested one.If it will help you ,then click on the accept solution button
let me know.
 
Share this answer
 
Comments
SumantaDash 10-Apr-14 13:53pm    
thank ,it is what i wanted. its working fine

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