how to get a handle to a cell in a data grid view

birddseedd

Member
Joined
Nov 18, 2008
Messages
6
Programming Experience
Beginner
I have a data grid view with columns created when it was filled from a sql table. some of the data types are "bit" (meaning 0 or 1 and automatically shows a check box in the column of in the data grid view).

I need to get a handle of the cell that has a check box in it so that when a check box in one column is checked or unchecked, i can change the status of the rest of the columns (only one column can be checked at a time) and then update the entire row in the sql server.

code will look like ...

when column 3 in the data grid view is checked,
app will uncheck column 2,4,5
run sql statement to update changes

...basicially

last part to figure out before i can finish this app. thanks for your help
 
you mean you want to implement some kind of radio button?
Youre better off doing this in the datatable to which the grid is bound. The grid was never filled with data; the data is still in the datatable. DataTable supports some events: DataTable Events (System.Data)

Hook into the RowChanging or hook into multiple columnChanging events. Remember to include some sort of protection such that your changing the other values programmatically doesnt cause the events to fire. I normally do this by:

VB.NET:
Partial Class MyDataTable
  Private tableIdle as Boolean = True

  Private Sub OnRowChanged(...)
    Try
     If tableIdle AndAlso <column_being_changed_is_of_interest> Then
      tableIdle = False
      'change other column values here
     End If
    Finally
      tableIdle = True
    End Try
  End Sub
Other people remove and add the event handler programmatically.. ive always preferred to use a boolean or similar test
 
I have learned that i shoudl be working with the data in the datatable, not the datagridview. but im still not sure exactly how to do that. or how to save the data table to the sql database when im done.

based on which peice of data is changed in the datagridview, I can get the data and know what to change, but how do i get a handle to the actual row and cell in the datatable the datagridview is being changed in? how do i actually change the data

once i do figure out how to change the data in the datatable, how do i go about modifying the data in the sql database?

thanks for your help. as i said above, once i get over these 2 hurtles, the rest of the app will be a synch.
 
You get a cell reference in a DGV by column index and row index. In all event handlers, the 'e' parameter contains the data for the event. In handlers from DGV cell events, the 'e' parameter has ColumnIndex and RowIndex properties. You can use the indices to get a row and the a cell in that row, e.g.
VB.NET:
Dim cell As DataGridViewCell = myDataGridView.Rows(e.RowIndex).Cells(e.ColumnIndex)
More simply, you can just index the grid itself:
VB.NET:
Dim cell As DataGridViewCell = myDataGridView(e.ColumnIndex, e.RowIndex)
As suggested though, you should generally work with the data source in code rather than the grid if possible. There are various ways of getting access to the data source and the best way depends on the circumstances. Generally speaking though, you'll want to work with the row currently selected in the grid. To facilitate that, start by binding your DataTable to a BindingSource and then bind that to the grid. You can then use the Current property of the BindingSource to get a DataRowView object that represents the current record.

In your case, you should probably handle the CellValueChanged event of the grid. That will give you the index of the column containing the cell whose value changed. If it's in your column of interest then proceed. You can then use the row index provided by the event to get the appropriate row. Note that should NOT get a grid row. Again, use the data source. Index the BindingSource to get a DataRowView that represents the record in which the cell value changed. You can then edit the other fields in that same DataRowView as appropriate.

One point to note is that a check box cell may not behave as you expect. Generally speaking, editing a cell in a DGV does not immediately change its Value property and a check box cell is no different. When you edit a cell the changes are pending. You can press the Esc key to rollback that pending change. When you navigate away from the cell, that's when the change is committed. As such, writing code to update other fields based on checking or unchecking a check box will not affect those other cells immediately by default. For more information on this, read the documentation for the DataGridViewCheckBoxCell class and the CellClick and CellContentClick events of the grid.
 
Back
Top