Datagridview conditional formatting using separate column

R

Rob Oldfield

Hi,

I want to conditionally format a column in a datagridview according to the
value in a different of my datasource. So if col 1 is true, then display col
2 in a different colour.

I know that I can use the cell formatting event if I want to conditionally
format the cell according to the value in that same column but is that the
best method to use if I'm using a different column for the condition?

Is this the best way of doing it, or is there something better?

In cell formatting event...

if dg.columns(e.columnindex).datapropertyname="ConditionColumn" then
if e.value=conditionvalue then
dim cell as datagridviewcell=dg(hard-coded column number,
e.rowindex)
cell.style.... do whatever formatting required
end if
end if

Would be a bit of a kludge having to hard code the 'target' column number
but I would actually replace that with a find on the datacolumns of the grid.

In my case, I don't actually need the column that is being looked at for the
condition on the grid. Does it HAVE to be there? Problem there is that if I
add it and make it invisible then the formatting event doesn't kick in.
 
L

Linda Liu[MSFT]

Hi Rob,
Is this the best way of doing it, or is there something better?

No. The best way to format a cell is to handle the CellFormatting event of
the DataGridView.
Would be a bit of a kludge having to hard code the 'target' column number
but I would actually replace that with a find on the datacolumns of the grid

You can use the column's name to get the column you want from the
DataGridView's Columns collection. For example:
Dim cell As DataGridViewCell = dg.Rows(e.RowIndex).Cells("columnname")
Does it HAVE to be there? Problem there is that if I add it and make it
invisible then the formatting event doesn't kick in.

No, the column that is being looked at for the condition does not have to
be visible in the DataGridView.

You can format the target cells in the CellFormatting event handler when
DataGridViewCellFormattingEventArgs.ColumnIndex equals to the index of the
target column. So even if the condition column is invisible, the cells
under the target column can be formatted correctly.

Hope this helps.
If you have anything unclear, please feel free to let me know.

Sincerely,
Linda Liu
Microsoft Online Community Support

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

==================================================
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top