By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,805 Members | 1,202 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,805 IT Pros & Developers. It's quick & easy.

C# APP, Assign value from database to checkbox in DataGridView

P: 6
Hi, i am new to windows application.
I have successfully list DataGridView with contain in a table.

Expand|Select|Wrap|Line Numbers
  1. SqlConnection conn = new SqlConnection(map);
  2. string sql = "Select * from Table_Name";
  3. SqlDataAdapter adap = new SqlDataAdapter(sql, conn);
  4. SqlCommandBuilder build = new SqlCommandBuilder(adap);
  5. DataTable dt = new DataTable("Table_Name");
  6. adap.Fill(dt);
  7. dataGridView1.DataSource = dt;
But now i want add a checkbox in DataGridView and assign value in ID field to the checkbox.
How can i do this.... ...Its urgent...

Advance Thanks
Dec 10 '08 #1
Share this Question
Share on Google+
3 Replies


P: 6
Actually what i want to do is, display a table content to the grid view and allow user to delete from the grid view.

For the delete function, i am thinking about having a checkbox and a button outside DataGridView. After the user check the data they want to delete(s), they will click the delete button. When the button click, it will check the checked checkbox and delete. That's why i assign the ID value to the checkbox, so it could know the id to delete.

now i able to display data grid with assign the value, but i dont know how to read the value of the checkbox...

here is the latest code
Expand|Select|Wrap|Line Numbers
  1. SqlConnection conn = new SqlConnection(map);
  2. string sql = "Select * from Table_Name";
  3. SqlDataAdapter adap = new SqlDataAdapter(sql, conn);
  4. SqlCommandBuilder build = new SqlCommandBuilder(adap);
  5. DataTable dt = new DataTable("Table_Name");
  6. adap.Fill(dt);
  7. chkDelete.DataPropertyName = "ID";
  8. dataGridView1.DataSource = dt;
  9.  
on event delete button fired, how to read the value of checkbox

Thank You
Dec 10 '08 #2

nukefusion
Expert 100+
P: 221
As your using a DataAdapter and you're using CommandBuilder to build the update/insert commands, you should be able to just delete the row in the grid, then simply call Update() on the DataAdapter in order to commit the changes to the database.

If that's the case then all you would need is a way to work out which row to delete from the datagrid. You shouldn't need to bind your checkbox column to any fields in your database. Just add the checkbox column as an unbound column. Then, when the user clicks the delete button, loop through the datagrid's rows and build a list of rows to delete, based on whether the checkbox on that row is checked or not. Perform the deletion from the grid, then call Update() to commit the changes. Something like this:

Expand|Select|Wrap|Line Numbers
  1. List<int> rowsToDelete = new List<int>();
  2. for (int i = 0; i < dataGrid.Rows.Count; i++)
  3. {
  4.     if (Convert.ToBoolean(dataGrid["columnCheckBoxDeleteName", i].Value))
  5.     {
  6.         rowsToDelete.Add(i);
  7.     }
  8. }
  9. foreach (int rowIndex in rowsToDelete) dataGrid.Rows.RemoveAt(rowIndex);
  10. adap.Update(dt);         
  11.  
Dec 10 '08 #3

Curtis Rutland
Expert 2.5K+
P: 3,256
Please enclose your posted code in [CODE] [/CODE] tags (See How to Ask a Question). Code tags preserve indention and uses a monospaced font.

This makes it easier for our Experts to read and understand it. Failing to do so creates extra work for the moderators, thus wasting resources, otherwise available to answer the members' questions.

Please use [CODE] [/CODE] tags in future.

MODERATOR
Dec 10 '08 #4

Post your reply

Sign in to post your reply or Sign up for a free account.