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

Asked By Rob W on 23-Nov-09 05:10 PM
Greetings,

Well this is really a continuation from a previous thread and I have exhausted
all my resources in finding an answer so posting in it is 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 could not 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




Sergey Poberezovskiy replied to Rob W on 23-Nov-09 09:03 PM
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
Rob W replied to Sergey Poberezovskiy on 24-Nov-09 03:18 PM
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
Sergey Poberezovskiy replied to Rob W on 24-Nov-09 04:55 PM
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
Rob W replied to Sergey Poberezovskiy on 24-Nov-09 05:27 PM
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 have entered the new details
that the row data was not bound until after row validated/leave row thus
could not read the checkbox value.

Thanks again
Rob