Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Can I do this with a DataGrid 2

Status
Not open for further replies.

dougconran

Technical User
Sep 26, 2003
89
GB
I want to be able to display data from an Access table in a tabular form using a DataGrid but I also want to include a checkbox for each row which is not a part of the underlying table. Essentially, this is so that the user can multi-select (and see that he has selected) a number of rows.

My experiments so far have not been successful (a) because the datagrid is readonly (which seems to be a condition forced on me by using an SQL query in the OleDBdataAdapter) and (b) because I can't work out a way of including an additional column that is not a part of the table (and I don't want to add another field to the table).

How can I do this (or achieve the same end by other means)?

TIA

Doug
 
Fetch your table from sql server and add a boolean column like this:
Code:
        Dim ds As DataSet = New DataSet
        ds.Tables.Add()
        ds.Tables(0).Columns.Add("Check", GetType(Boolean))
        DataGrid1.DataSource = ds.Tables(0)
 
Thanks for this - but I still have a problem.

I am using a tablestyle so that I can get my column widths etc right and then casting the column type as a boolean column. Here is my code

Code:
DataSet11.Tables(0).Columns.Add("Check", GetType(Boolean))
BoolCol = New DataGridBoolColumn
BoolCol.Width = 80
BoolCol.HeaderText = "Check it"
BoolCol.MappingName = DataSet11.Tables(0).Columns(41).ColumnName
tableStyle.GridColumnStyles.Add(BoolCol)

The problem is that the checkboxes then become readonly, whether or not the datagrid property is set to readonly. I've seen this problem mentioned in other threads but no answer to it.

TIA

Doug
 
Hey Doug. The columns should not make a difference here. Are you pulling the data into a DataSet object from the access database?
Here may be your problem. If you are using a recordset and not a dataset, then it may be read only.

Can you please post your database/query code?
 
I was penning a long reply explaining exactly what I was doing and, in doing so, have (I think) discovered what I was doing wrong.

Essentially I was single clicking on the checkbox which, I guess, was just moving the cursor to that cell. What I needed to do was to double click. I was fooled by the fact that the checkboxes are greyed as if they have been disabled.

This experience accords with my parrot theory which is that, most of the time, you don't need a human to sort out your (coding) problems you just need a parrot. All too often as one explains in detail what you are doing the problem area becomes obvious. - Oh well.

Thanks both Korach and bigfoot for your help.

Doug
 
Going on from the problem that I had earlier. I can now get the boolean column to display and change value but it is initially displayed in an indeterminate state (tristate). I've tried setting boolCol.AllowNull = False but this does not have any effect.

When I test the value of this field I get the following error:-

Cast from type 'DBNull' to type 'Boolean' is not valid

I'm using:-

If DataGrid1.Item(hit.Row, 39) Then

What should I be doing?

TIA

Doug
 
Hi Doug,

Not sure if you found a solution to this problem, and I'm a bit slow in replying, but I've been trying to find a way of determining which rows have been multi selected...

I think the code you need to disable the tristate check box is, when setting up your column styles, for the boolean type column (In this cas called ActiveColumnStyle):

Code:
ActiveColumnStyle.AllowNull = False

This worked for me...
 
Oh, and I guess the values in your table would need to be not nulls too.... I would think your table would be a bit type column, so should be 0 or 1...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top