Solved

DataGridViewRow Default Cell Style Applying to All Rows

Posted on 2009-05-20
3
1,254 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…

751 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