Disable editing on a row basis on DataGrid (winforms)

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have a datagrid that I want to be able to allow me to edit only the first
two rows and all the other rows to be read-only.

How can I do that? Any insight is highly appreciated.

I have spent couple of days on research but no luck so far.

Thank you
 
Gentian,

AFAIK impossible, the datagrid was greath but in fact very poor in use,
therefore came the datagridview

Cor
 
Thanks. But that is only for Visual Studio 2005 right? I am using 2003 and it
may take a while before I do that transition.

In the meantime what can I do with 2003?
 
Gentian said:
I have a datagrid that I want to be able to allow me to edit only the
first two rows and all the other rows to be read-only.

Assuming the DataGrid in VS2003 works the same as in VS2005 (I don't know
whether it does or not, I only have VS2005 installed), you should be able to
achieve this by:

- declaring a class that inherits from Windows.Forms.DataGridTextBoxColumn

- in the class, override the Edit method. Add code that calls into the base
class if the rowNum is one you want to be editable, or simply returns
without doing anything if you want the column to be read-only.

- when initialising the grid, create a new Windows.Forms.DataGridTableStyle
object.

- for each column in the grid, add a new instance of your class from above
to the DataGridTableStyle object. Make sure you set the MappingName property
of the object to match the underlying column name in your DataTable, and
also set the HeaderText, Width, Alignment and anything else you need.

- once you've added all your columns, add the DataGridTableStyle object to
the DataGrid.TableStyles collection.

- finally, set the DataGrid's DataSource.

Hopefully that will do what you want!
 
Maybe you can set two datagrids above each other. However the problem stays
that read only and things like that with a datagrid in 2003 is done with the
dataview.

Cor
 
Hi:

If I recall correctly, the 2003 DataGrid was originally designed by
componentone, and should have a rowcolchange event, row property, and
AllowUpdate property?

If so, then you check which row you are in in the rowcolchange event, then
set the allowupdate property accorindingly.

hope this helps,
 
Back
Top