DataGridView, DataSource, Columns and Datatable.

G

Guest

Hi,

I have been trying to see if I can do the following:

1. Create a DataGridView
2. Create 2 columns in the DataGridView (1 textbox and the other combobox)
3. Create a DataTable containing my data. (AutoGenerateColumns is true).
The data is 2 columns of System.String cell.

4. If I assign the DataTable to DataGridView.DataSource, I get 4 columns. 2
column
from (2) and 2 columns from (3).

I am not certain is there a way to use DataTable in (3) to populate the 2
columns I created in (2). E.g. through proper column naming or other technic.

Any help is appreciated.
 
H

Hector Obregon [eMVP] [MSDN RD]

Hi George,

Maybe I do not understand your question but....maybe you could first assign
the DataTable to your Grid and then modify the autogenerated columns to
achieve what you need.

Regards,

Hector Obregon
http://msdnfan.blogspot.com
 
J

Jeffrey Tan[MSFT]

Hi George,

Thanks for your post!

To get what you want, we should set AutoGenerateColumns property to false.
This tells the DataGridView do not generate extra default columns for the
datasource, and you take care of the column generating.

Then we should set the 2 added columns' DataProperyName property to
corresponding DataColumn.ColumnName.

Hope this helps

Best regards,
Jeffrey Tan
Microsoft Online Community Support
==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
G

Guest

Hi Jefferey,

Thanks. I got it to work. However....

I notice that the DataGridView is "readonly". I am not able to modify
contents, even if I set the DataGridView ReadOnly to false.

Is this the normal by-product of DataSource binding?

Or is there a way to make DataGridView cells modifiable? Even better, can
these modification update Bound-DataSource, e.g. a DataTable? That will be
cool.

Thanks for any help.
 
J

Jeffrey Tan[MSFT]

Hi Fabian,

Thanks for your feedback!

By default, users can edit the contents of the current DataGridView text
box cell by typing in it or pressing F2. This puts the cell in edit mode if
all of the following conditions are met:

1. The underlying data source supports editing.
2. The DataGridView control is enabled.
3. The EditMode property value is not EditProgrammatically.
4. The ReadOnly properties of the cell, row, column, and control are all
set to false.

So your symptom is not the default behavior. You may check all the above 4
conditions to get the default editing behavior.

Hope it helps!

Best regards,
Jeffrey Tan
Microsoft Online Community Support
==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
G

Guest

Hi Jeffrey,

I verified point (2) to (4) of your comments. I have all the settings
correct.

Regarding comment (1), I have further questions on it.

**
1. The underlying data source supports editing.
**

(a) How do I know whether the underlying data source supports editing? In
specific, does "class DataTable" support editing?

(b) If "calss DataTable" supports editing, are there any properites needed
to be set? (I know for sure that the settings I have make my DataGridView
not editable.)

(c) After some investigation, I found on the web, that a class needs to
implement "interface IEditable". However, I was not able to find any
documentation on this interface. It seems like it is a Java thing. Any
comments?

Thanks for your help again.
 
L

Linda Liu [MSFT]

Hi George,

Please check whether the ReadOnly property of the columns in your DataTable
is true. If yes, you could not edit the cell content in the corresponding
DataGridViewColumns in the DataGridView.

The collection used for a DataSource needn't implement the interface
IEditableObject to support editing. The collection supports editing only if
the properties in it are not readonly.

Hope this is helpful to you.

If you have any other concerns or need anything else, please don't hesitate
to let me know.


Sincerely,
Linda Liu
Microsoft Online Community Support

====================================================
When responding to posts,please "Reply to Group" via
your newsreader so that others may learn and benefit
from your issue.
====================================================
 
J

Jeffrey Tan[MSFT]

I am glad we finally resolved your problem. If you need further help,
please feel free to post. Thanks.

Best regards,
Jeffrey Tan
Microsoft Online Community Support
==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top