Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Windows Forms / WinForm Controls / February 2007

Tip: Looking for answers? Try searching our database.

Set readonly property on cell level in a datagridview

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Patrik - 15 Feb 2007 12:27 GMT
Hi,

I have a datagridview column that will contain both editable and
non-editable values. I have set the columns readonly property to true. I
then use the AddRows event to check the value of an hidden column and if
that is false i set a specific cells readonly property in the column to
false.

My problems is that this do not open a specific cell for editing but seems
to open all cells in the column. Is it not possible to have some cells in a
column to override the columns readonly property?

/Patrik
ClayB - 15 Feb 2007 14:19 GMT
One soltuion to this problem is to handle the CellBeginEdit event adn
cancel things if this particluar cell should be readonly.

//ReadOnlyColName0 is the name of the column you may want to be
readonly
//boolColName is the name of the column where you have your boolean
test value to check for readonly
void dataGridView1_CellBeginEdit(object sender,
DataGridViewCellCancelEventArgs e)
       {
           if (dataGridView1.Columns[e.ColumnIndex].Name ==
"ReadOnlyColName0")
           {
               object o = dataGridView1["boolColName",
e.RowIndex].Value;
               e.Cancel = o != null &&
Convert.ToBoolean(o);
           }
       }

==================
Clay Burch
Syncfusion, Inc.
RobinS - 15 Feb 2007 19:11 GMT
> One soltuion to this problem is to handle the CellBeginEdit event adn
> cancel things if this particluar cell should be readonly.
[quoted text clipped - 19 lines]
> Clay Burch
> Syncfusion, Inc.

You must have done a lot of work with the DataGridView control. You always
know the answers to these questions. That's cool.  :-)

Robin S.
Linda Liu [MSFT] - 16 Feb 2007 03:58 GMT
Hi Patrik,

Based on my understanding, you'd like to make some cells in a column
editable and others non-editable and the problem is this does not open a
specific cell for editing but seems to open all cells in the column. If I'm
off base, please feel free to let me know.

It is possible to have some cells in a column to override the columns
ReadOnly property. As you have mentioned, we could set the ReadOnly
property of the cell to make it editable or non-editable.

You mentioned the AddRows event. Do you mean the RowsAdded event of the
DataGridView? The RowsAdded event is propriate when the user adds a new row
using the row for new records in the DataGridView. The
DataGridViewRowsAddedEventArgs.RowIndex value in the handler for this event
is equal to the index of the new location of the row for new records, which
is one greater than the row just added. However, when you add rows
programmatically, the RowInex value is the index of the first row added.

I suggest that you subscribe the RowPostPaint event of the DataGridView and
set the ReadOnly property of the cells you want in the event handler.

The following is a sample.

void dataGridView1_RowPostPaint(object sender,
DataGridViewRowPostPaintEventArgs e)
       {
           if (this.dataGridView1.Rows[e.RowIndex].Cells[0].Value != null)
           {
               if
(((int)this.dataGridView1.Rows[e.RowIndex].Cells[0].Value) % 2 == 0)
               {
                   this.dataGridView1.Rows[e.RowIndex].Cells[1].ReadOnly =
false;
               }
           }
       }

Hope this helps.
If you have any question, please feel free to let us know.

BTW, I will be on a long vacation from the next Monday to Friday. During my
leave, my team mates will follow up to you and it may not in time. Sorry
for the inconvenience it may bring to you!

Sincerely,
Linda Liu
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
Patrik - 20 Feb 2007 07:26 GMT
Thank you. I went for the solution that Clay presented. Seems to work well.
I do not understand though why it should not work to set the cells readonly
property in the RowsAdded when it seems to work in RowPostPaint.

/Patrik
> Hi Patrik,
>
[quoted text clipped - 71 lines]
> This posting is provided "AS IS" with no warranties, and confers no
> rights.
Jeffrey Tan[MSFT] - 22 Feb 2007 08:06 GMT
Hi Patrik,

Sorry for the late response, I am out of office yesterday.

Glad to see your problem is resolved. Regarding RowsAdded event enables the
editing of all the cells in the column, I suspect because the RowsAdded
event is called multiple times as we discussed in another thread, so
RowIndex is not accurate to identify the single cell in the cell. Can you
show me the code you are using in RowsAdded event? This will be helpful to
the understanding. Thanks.

Best regards,
Jeffrey Tan
Microsoft Online Community Support
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
Linda Liu [MSFT] - 26 Feb 2007 11:43 GMT
Hi Patrik,

I'm back from my vocation.

I agree with Jeffrey. The RowsAdded event is called every time a row is
added in the DataGridView. However, if the DataGridView is populated with a
data source, the DataGridViewRowsAddedEventArgs.RowIndex value in the
RowsAdded event handler is not equal to the actual index of the row added.

In this case, we'd better handle the RowPostPaint event of the DataGridView
instead. The RowPostPaint event occurs after a DataGridViewRow is painted
and the DataGridViewRowPostPaintEventArgs.RowIndex in the event handler is
accurate.

BTW, Clay's solution does work but it does this through canceling editing
rather than set the cell readonly directly.

Hope this helps.
If you have any concerns, please feel free to let me know.

Sincerely,
Linda Liu
Microsoft Online Community Support

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.