Solved

How to set cells in datagridview to readonly and BackColor upon construction

Posted on 2013-01-07
9
1,487 Views
Last Modified: 2013-01-19
Hi experts
I'd tried to set some cells in a datagridview to readonly and their BackColor to grey upon construction of a UserControl. But they didn't take effect.

I'd done some searches. It was said that it needs to be done inside "CellBeginEdit". My question is how to get that event to fire as part of the construction. Any other suggestion is welcomed.

Thanks in advance.
0
Comment
Question by:dominicwong
  • 6
  • 2
9 Comments
 
LVL 39

Expert Comment

by:Pratima Pharande
ID: 38753637
check this fro readonly

http://forums.asp.net/t/1072584.aspx/1

    Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound
        If e.Row.RowState = DataControlRowState.Edit And (e.Row.RowIndex = 0 Or e.Row.RowIndex = 1) Then
            e.Row.RowState = DataControlRowState.Normal
        End If
0
 
LVL 39

Expert Comment

by:Pratima Pharande
ID: 38753640
0
 

Author Comment

by:dominicwong
ID: 38756969
Thanks pratima_mcs.

I am using C# .NET, and I can't find the equivalent of your suggestion. The two lines I currently have in my code are:
They don't work during construction but can only work during cell edit mode.
dgv.Rows[rowIndex].Cells["columnName"].ReadOnly = true;
dgv.Rows[rowIndex].Cells["columnName"].Style.BackColor = Color.LightGray;
0
 

Author Comment

by:dominicwong
ID: 38761216
By the way, my dgv is bound to a DataSource.
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 96

Expert Comment

by:Bob Learned
ID: 38766690
Can you explain what you mean by "They don't work during construction but can only work during cell edit mode."?
0
 

Author Comment

by:dominicwong
ID: 38769233
What I have is a dgv. This dgv is on a UserControl and is displayed only when a user initiated it (eg. clicking a treenode). ie. at run-time.

This dgv consists of two columns, and is data-binded to a DataSource.
Dependent upon the value on the left-hand side column, the corresponding entry on the right-hand column will be made either editable with a white background, or readonly with a grey background.

What I meant by "don't work during construction" is when user clicks a node to initiate the UserControl construction. It constructs the dgv, creates the data-binding, checks each entry on the left-hand side column and sets the right-hand side column accordingly.
Unfortunately, making them readonly and setting the background color did not take any effect at all.

On the other hand, after it is constructed when I edit a cell on the left-hand side column (eg. changing selection), I could make the corresponding cell on the right-hand side readonly with grey background color.

Your help will be appreciated. Thanks in advance.
0
 

Author Comment

by:dominicwong
ID: 38777024
I'd tried out with a simple example but this one works fine.
Any idea what could cause the setting on the properties BackColor and ReadOnly not to take any effect?
0
 

Accepted Solution

by:
dominicwong earned 0 total points
ID: 38777124
0
 

Author Closing Comment

by:dominicwong
ID: 38796107
It resolved the issue
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

760 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