473,407 Members | 2,676 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

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

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
3 10386
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
221 Expert 100+
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
3,256 Expert 2GB
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

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

Similar topics

4
by: Shufen | last post by:
Hi, I'm a newbie that just started to learn python, html and etc. I have some questions to ask and hope that someone can help me on. I'm trying to code a python script (with HTML) to get...
4
by: Terry | last post by:
I have a number of input boxes used to display totals based on selected items for each row in a table. There are more than a few rows that are identical, except for the form field name. I have...
4
by: Jack | last post by:
Hi, I have a checkbox the value which goes to a database via a asp page that builds the sql string. In the front end asp page, the checkbox code is written as follows: <i><input...
1
by: annbb | last post by:
I have a piece of code which resets all the control on a form ..but if I also put an option group on the form and the relevant code in the routine to reset it I get an error message saying that...
6
by: Daz | last post by:
Hi everyone. Firstly, I apologise if this i not what you would call a PHP problem. I get quite confused as to what lives in which realm, so if this shouldn't be posted here, please suggest where...
0
by: sk27ahmed | last post by:
Hi Any one can show me how to access datagridview column value on column checked unchecked. I create one column in datagridview of type checkbox,and on button click i write code to select all...
4
by: Benysh | last post by:
Hi everyone. I'm having a really annoying problem I can't solve and couldn't find an answer to anywhere, let's see if you got what it takes. I have a dataGridView on a C# Windows Form. Unbound....
1
Plater
by: Plater | last post by:
I feel like this question has been asked before, but I couldn't find it. I think I am missing something obvious. Basically I am going to have a DataGridView with two columns. One textbox, one...
0
by: hydro123 | last post by:
I am using VC++2008 and am trying to read data enetered in unbound datagridview to implement in function. Under button_click event I entered the following: // initialize varaibles from...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.