Link to home
Start Free TrialLog in
Avatar of se402036se
se402036se

asked on

Cannot set the value of a cell

Hi there

I have a dataGridView and a table as a datasource like so:
Me.dgvLocations.DataSource = table

I then set the DataPropertyName of one of my column to a field in my table like so:
Me.colActivity.DataPropertyName = "cloActivityNote"

The column is a dataGridViewComboBoxColumn.

I then want to set the value of this column to something. So lets set it to the first value in my table (which is the data source):
me.dgvLocations.Rows(0).Cells("colActivity").Value = table.Rows(0).Item("cloActivityNote")

I then good at:
me.dgvLocations.Rows(0).Cells("colActivity").Value
and it seemed to be set fine

But when the form opens/draws itself or whatever the value is not set and I get an error in the event dgvLocations.DataError:
DatagridViewComboBoxCell is not valid

any ideas why?
Avatar of vb_jonas
vb_jonas
Flag of Sweden image

Your value must be in the combo-list.
Avatar of se402036se
se402036se

ASKER

well if I set the DataPropertyName of the column to a column in my datatable (i.e. datasource)...

Then set the value = a value from that column of my datatable (datasource), does that not mean that I am setting it to a value in the combo list? i.e.

Me.dgvLocations.DataSource = table
Me.colActivity.DataPropertyName = "cloActivityNote" '''''"cloActivityNote" is a col in the table
'At this point is my combo not set to the fileld in my table hence i can set the value to a value in the table like so:
me.dgvLocations.Rows(0).Cells("colActivity").Value = table.Rows(0).Item("cloActivityNote")

what am I doing wrong here?
Im not sure I follow completely,

You like to have a grid with one column, named colActivity, yes?
And the column should display a combo, with the values in the combo-list from which column, in which table?

when you add your column to the dgw you should also set the datasource from where it should get the list of values:

            col = New DataGridViewComboBoxColumn()
            With col
                .Name = "Object Type"
                .DataPropertyName = "ColInMyDataTable"
                .DataSource = MyDataTableWithListValues
                .DisplayMember = "ColInMyListValuesTable"
                .ValueMember = "ColInMyListValuesTable"
            End With
            DatagridView1.Columns.Add(col)
Also - when adding / editing data - its better to do it directly in the datatable.
Ok so here is were I've go so far:

I have a datagridView (dgvLocations) which let's say has 10 col's. One of them is called "cloActivityNote" and is a dataGridViewComboBoxColumn. So the combo column already exists in my datagridView.

In a dataTable (Me.controller.table) i have a col called "cloActivityNote" and I want the values of this col to be in my combo.

So I create a table with one column comprising of the column values in my original table then set this new table as the datasource of my comboBox column lke this:
Dim table As New DataTable
Dim column As New DataColumn("NewActivityCol")
table.Columns.Add(column)

For Each datarow As DataRow In Me.controller.table.Rows
                Dim row As DataRow = table.NewRow
                row.Item(0) = datarow.Item("cloActivityNote")
                table.Rows.Add(row)
Next

            With Me.colActivity 'The name of my comboBox column
                .DataSource = table
                .DataPropertyName = "NewActivityCol"
                .ValueMember = "NewActivityCol"
                .DisplayMember = "NewActivityCol"
            End With

all seems fine but when I then do:
Me.dgvLocations("colActivity", 0).Value = CType(Me.dgvLocations("colActivity", 0), DataGridViewComboBoxCell).Items(0)
 
the value does not get set ... any ideas?
Me.dgvLocations("colActivity", 0).Value = CType(Me.dgvLocations("colActivity", 0), DataGridViewComboBoxCell).Items(0)(0)


the item is a datarowview and you have to index once more to get the actual value
Here's how it would look like when working against the datatables: (if I got your tables right...)

        Dim dr As DataRow
        dr = Me.controller.table.NewRow

        dr("NewActivityCol") = table.Rows(0)("NewActivityCol")

        Me.controller.table.Rows.Add(dr)
that actually works if I do one thing...

not set the data source of my dataGridView, like this:
Me.dgvLocations.DataSource = Me.controller.table

So the only difference is that I set the datasource of my dgv to a dataTable and set some DataPropertyName connecting the columns of my view with the columns of my datatable.

I need to do this i.e. set the whole dgv view to a table and then seperatly set my combo col to a different table.

Can I not to the two things? so when I comment out the line:
Me.dgvLocations.DataSource = Me.controller.table
this line works fine:
Me.dgvLocations("colActivity", 0).Value = CType(Me.dgvLocations("colActivity", 0), DataGridViewComboBoxCell).Items(0)(0)

else
the above line does not work.

Thanks a million for sticking with me with this, i've been at it ages
Hi again,
if you have it unbound it works you say?

But if you set the datasource, what error do you get, not DataError, no?
ASKER CERTIFIED SOLUTION
Avatar of vb_jonas
vb_jonas
Flag of Sweden image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
yip if I have the whole dataGridView UNbound and then bound the comboBoxCol then it works fine.

But when I do BIND the whole dataGridView and then the comboBoxCol then, yea it dosen't work

...No it's not DataError. I think that the value gets set to Nothing just after the dgvLocations.RowValidating event

because when I but a break point on that event and run
Me.dgvLocations("colActivity", 0).Value
I get a value

but then I step out of that event it lands on the event:
dgvLocations.RowEnter
I again run the line
Me.dgvLocations("colActivity", 0).Value
and the result is Nothing

So the value gets set back to nothing for some reason

So maybe the rowValidation fails and it gets reset ..... but why?
If you would like to try something else, you could actually use the same table as you have for the DataGridView in the ComboBox:

            With Me.colActivity 'The name of my comboBox column

                ' the list items comes from:
                .DataSource = Me.controller.table ' use the same table
                .ValueMember = "cloActivityNote" ' and the same column
                .DisplayMember = "cloActivityNote"
               
                ' and the values should be stored in:
                .DataPropertyName = "cloActivityNote"

            End With


And get rid of the other table "table". If I have understood what you are trying to achieve correctly.
yes, then I really think is the datapropertyname thats wrong, see my 09:08AM PDT answer.
wow ..... it bloody worked ... nice one mate .... you are the man!
:-) happy to help, glad it worked!
liked back to this Q. ?
yea now I am using an array of classes as my datasource and it dosen't seem to work for me ... i've been playing around with it for ages.

Can you tell me exactly what the different between these are?
DataPropertyName
Displaymember
Valuemember

Would I be correct to say:
DataPropertyName = The column/field or in my case the property of a class that will be bound to the control
So I should do:
.DataPropertyName = "locationActivityName" .... as locationActivityName is the property of my class that I want to be displayed in my dropdown
I would like this property in each of the classes in my array to be displayed inside my comboBox???
so if I have 10 classes in my array I want my comboBox to display 10 items... is this the way to do it?

Displaymember = In my case the property of my class to be displayed to the user
Valuemember = The value corresponding to the display member, in my case it should be the same as the display member

I'm actually getting a little confused as I write this!

Any help would be great and i'll give you the point from the other Q
Your link was back to this question Q_21882916.html... But I assumed it would link to another question.

DataPropertyName, the field in your datatable thats bound to the control. Yes, just like a regular TextBoxColumn. When the user sets the value, and updates, the value is put in that field in your datatable.

And to populate your comboBox you use the two other, Displaymember & Valuemember and they can point to the same field. Yep.

This sentence I do not understand:

"I would like this property in each of the classes in my array to be displayed inside my comboBox???"

yes, if you set the combobox' display&valuemember to be classfieldname AND combobox' datasource to be the classes array (I've never done that - I always use the datatable object which is easy to use for these kinds of things).

But where do you like to store the data the user puts into your grid? To what datasource is the grid bound?

You can use the same datasource to populate both the grid and the combo, but thats not common. Usually you have one table with selectable values (to the combo) and one table which stores data from the grid.
sorry about the wrong link here you go:
https://www.experts-exchange.com/questions/21884393/cannot-set-the-value-of-a-comboBoxCell.html

So bascially what I would like to do is:
I have say 10 rows in the dgv. One column is a comboBoxColumn.

And I have an array of classes, just think that they are empty except for on property called locationActivityName (a string)

so in each of these 10 rows of my dgv I want my combo (10 rows hence 10 combos) to contain the locationActivityName of each of my classes in my array?

i tryed:

With Me.colActivity
                .DataSource = Me.activities
                .ValueMember = "locationActivityName"
                .DisplayMember = "locationActivityName"

                .DataPropertyName = "locationActivityName"
End With

I then set the value of the combos to the first item in each like so:
For i As Integer = 0 To Me.dgvLocations.Rows.Count - 1
                Me.dgvLocations("colActivity", i).Value = CType(CType(Me.dgvLocations("colActivity", i), DataGridViewComboBoxCell).Items(0), TlkpCompanyLocation).locationActivityName
            Next

But i'm sure i'm doing something wrong here?
jes' i think I know what's wrong ...

The property in my class has to be the same name as the ValueMember in my datatable... and it's not ahhhhh

Thanks again for all your help, really appreciate it