Vb.net 2008 Datagridview bound/unbound columns update datasource effecting unbound columns

  • Thread starter Thread starter Rob W
  • Start date Start date
R

Rob W

Greetings,

Well this is really a continuation from a previous thread and I've exhausted
all my resources in finding an answer so posting in it's own thread.

A datagridview is created programmatically and then the datasource is set to
a dataset with all but the datagridviewcheckboxcells bound using the
dataproperty option.

If I change the underlying datasoure I.e. the datatable (e.g. removing a
datarow) then the unbound columns if checkbox values are selected i.e. true
are ALL set to false.

Is this usual behaviour? Can I prevent this?

I couldn't find a single item on the Internet to reference anyone else
experiencing the same, apologies if being really stupid but need some
guidance here, can anyone please offer some advice and guidance?

Thanks
Rob
 
Rob,

I might be wrong, but I suspect that because the grid checkbox column is not
bound, then during DataSource change (when you remove the row) it resets
unbound columns to the default values - unchecked.

Is there any reason why you need the checkboxes unbound - you may add a
(boolean) column to the table and then bound the checkboxcolumn to that. This
way grid should know where to get the values for individual rows from.

Hope this helps
 
Thanks for the suggestion I had thought of this but was interested in
alternatives, with nothing forth coming I decided to store the column
though not really data
as is used to select rows for the purpose of deletion.

Ensuring I check the formatted value in a loop to remove the data e.g.
Bookings.Cells("selectColumn").FormattedValue = "True"


I created a new field as a bit to store the value of the
datagridviewcheckboxcell in SQLServer e.g.
Select CAST( 0 as bit) AS selectColumn

All works as intended now :-)

Thanks
Rob
 
Rob,

If you do not need to store the column information - no need to:
Consider the following code:
Dim source As DataTable = getDataFromSqlServer()
source.Columns.Add("Selected", GetType(Boolean))
myDataGridView.DataSource = source

This way you only use the column where you need it.
Also there is no need to use FormattedValue, you can use Value instead:
Bookings.Cells("selectColumn").Value = True

Hope this helps
 
Thanks for the reply.

That's given me some more to think about.

The reason I used .formattedvalue is I read the value of the checkbox for a
routine that runs in the row validation.

When I printed the value for the datagridview cell it would print true or
false in a message box, when using an if statement
it would always fail with Null/DBNull error making me think as the
rowvalidation routine activates after I've entered the new details
that the row data was not bound until after row validated/leave row thus
couldn't read the checkbox value.

Thanks again
Rob
 
Back
Top