Solved

DataGridViewRow Default Cell Style Applying to All Rows

Posted on 2009-05-20
3
1,245 Views
Last Modified: 2012-05-07
I have a DataGridView that is bound to a bindingsource that has a DataView as its datasource.  The DataGridView shows data read from a MSSQL 2005 database, plus one other row I call the dummy row that is added to the datatable dynamically.  This is a row where the user can select and enter data to create a new row of data in the database table.  

The dummy row is formatted differently from the other rows to make it stand out, with a yellow backcolor and italicized font.

I have an object, _dummyRowStyle of type DataGridViewCellStyle, that is assigned to the default cell style of the dummy row, like this:

    Private Sub markDummyRow()

        Debug.WriteLine("MarkDummy")

        Dim dgvr As DataGridViewRow = GetDataGridRowFromDataRow(DataGridView_DataTable, _dummyRow)

        If Not dgvr Is Nothing AndAlso Not dgvr.HasDefaultCellStyle Then
            dgvr.DefaultCellStyle = _dummyRowStyle
        End If

    End Sub

From tracing the code and looking at debug output, I know this sub only gets called once and only sets the defaultcellstyle for one row.

The problem is that the entire DataGridView gets set with this style instead of just one row.  If I then sort the DataGridView by clicking on a columnheader, the DataGridView then displays the formatting I want:only the dummy row is formatted differently and everything looks fine.

Any ideas why when I set the style for one DataGridViewRow, it is affecting the entire DataGridView?  Am I missing something with style inheritance?

If I can't get this to work, I plan on redoing the logic to use cell paint events to handle my special formatting needs instead of using cell styles.


0
Comment
Question by:jedwar26
  • 2
3 Comments
 
LVL 15

Accepted Solution

by:
JackOfPH earned 500 total points
ID: 24438511
Where did you call the sub routine MarkDummyRow?

0
 

Author Comment

by:jedwar26
ID: 24438797
It was getting called from inside a few different event handlers -- and it appears setting this style inside the event handlers was the root of the problem.

I moved the call to markDummyRow to a few other places -- it needs to be called after any change (change to data source, change to sorting, etc.)  and I no longer have the issue.



0
 

Author Closing Comment

by:jedwar26
ID: 31583617
The expert's comment was helpful in resolving the issue but wasn't the final solution.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
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…
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.

707 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

12 Experts available now in Live!

Get 1:1 Help Now