Solved

Cannot set the value of a cell

Posted on 2006-06-12
22
1,441 Views
Last Modified: 2008-01-09
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?
0
Comment
Question by:se402036se
  • 13
  • 9
22 Comments
 
LVL 12

Expert Comment

by:vb_jonas
ID: 16885030
Your value must be in the combo-list.
0
 

Author Comment

by:se402036se
ID: 16885120
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?
0
 
LVL 12

Expert Comment

by:vb_jonas
ID: 16885185
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)
0
 
LVL 12

Expert Comment

by:vb_jonas
ID: 16885197
Also - when adding / editing data - its better to do it directly in the datatable.
0
 

Author Comment

by:se402036se
ID: 16886293
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?
0
 
LVL 12

Expert Comment

by:vb_jonas
ID: 16886469
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
0
 
LVL 12

Expert Comment

by:vb_jonas
ID: 16886503
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)
0
 

Author Comment

by:se402036se
ID: 16886623
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
0
 
LVL 12

Expert Comment

by:vb_jonas
ID: 16886726
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?
0
 
LVL 12

Accepted Solution

by:
vb_jonas earned 125 total points
ID: 16886791
Where did you create the colActivity, and how does that code look like?

I really think you should replace this:

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

To this:

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

Since the column where you want to store the values is named cloActivityNote, right?
0
 

Author Comment

by:se402036se
ID: 16886812
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?
0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 12

Expert Comment

by:vb_jonas
ID: 16886818
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.
0
 
LVL 12

Expert Comment

by:vb_jonas
ID: 16886830
yes, then I really think is the datapropertyname thats wrong, see my 09:08AM PDT answer.
0
 

Author Comment

by:se402036se
ID: 16886853
wow ..... it bloody worked ... nice one mate .... you are the man!
0
 
LVL 12

Expert Comment

by:vb_jonas
ID: 16886873
:-) happy to help, glad it worked!
0
 

Author Comment

by:se402036se
ID: 16893004
0
 
LVL 12

Expert Comment

by:vb_jonas
ID: 16893317
liked back to this Q. ?
0
 

Author Comment

by:se402036se
ID: 16893959
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
0
 
LVL 12

Expert Comment

by:vb_jonas
ID: 16894191
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.
0
 

Author Comment

by:se402036se
ID: 16895032
sorry about the wrong link here you go:
http://www.experts-exchange.com/Programming/Programming_Languages/Dot_Net/VB_DOT_NET/Q_21884393.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?
0
 

Author Comment

by:se402036se
ID: 16895767
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
0
 
LVL 12

Expert Comment

by:vb_jonas
ID: 16899762
:-)
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now