Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7857273
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T20:59:19+00:00 2026-06-02T20:59:19+00:00

Background When I hit a button on a WinForm, I am loading data into

  • 0

Background

When I hit a button on a WinForm, I am loading data into a BindingSource that serves as the data source for a DataGridView. Once the data is loaded, I go through and make some modifications to the DataGridView; in particular, I 1) set any cells that are valued as DBNull to the string value “NULL”, 2) italicize the same cells, and 3) highlight some rows.

Simple example of what I’m doing:

private void btnFetch_Click(object sender, EventArgs e)
{
    // If there's already a DataSource, Dispose of it.
    if (bsMessageTracking.DataSource != null)
    {
        (bsMessageTracking.DataSource as DataTable).Dispose();
    }

    // Get new DataSource.
    bsMessageTracking.DataSource = GetDataTable(); // Details not relevant.

    // Show NULL values.
    foreach (DataGridViewRow row in dgv.Rows)
    {
        foreach (DataGridViewCell cell in row.Cells)
        {
            if (cell.Value is DBNull)
            {
                cell.Value = "NULL";
                cell.Style.Font = new Font(dgv.DefaultCellStyle.Font, FontStyle.Italic);
            }
        }
    }

    // Apply highlighting.
    foreach (DataGridViewRow row in dgvMessageTracking.Rows)
    {
        if (HighlightRow(row)) // Details not relevant.
        {
            row.DefaultCellStyle.BackColor = Color.LightYellow;
        }
    }
}

The data gets loaded based on input into a TextBox on the form.

Situation

Everything works great if this occurs on the button click. However, in order to provide some convenience to the user, I am allowing this form to get loaded with data prepopulated – the main form will instantiate this form with the data to put into the TextBox, and this btnFetch_Click handler will get called from the constructor:

internal MessageTracking(string ID)
{
    InitializeComponent();

    // Setup data source.
    dgvMessageTracking.DataSource = bsMessageTracking;

    // Set ID and run query.
    if (ID != null)
    {
        // Set ID.
        txtlID.Text = ID;

        // Run!
        btnFetch_Click(null, null);
    }
}

The value of the cells gets changed (so I see NULL), but the font and the highlighting don’t stick.

What I’ve tried

If I replicate the highlighting code in the OnShown method, the highlighting sticks. However, replicating the font code there doesn’t work. I can make the font stick if I put it in CellFormatting, but that seems like overkill to me, because I only need this to be run once when the form is loaded – it works fine if the process is run after the form is visible.

Plea

If anyone has any suggestions, I would appreciate them. Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T20:59:21+00:00Added an answer on June 2, 2026 at 8:59 pm

    I guess, from the lack of responses, that there is no better way if you want the formatting shown when the form first becomes visible. I have removed the Show NULL values portion of the btnFetch_Click handler and added this dgvMessageTracking_CellFormatting handler to handle this functionality at all times:

    private void dgvMessageTracking_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
    {
        if (e.Value is DBNull)
        {
            e.Value = "NULL";
            e.CellStyle.Font = new Font(dgvMessageTracking.DefaultCellStyle.Font, FontStyle.Italic);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My iOS app doesn't run in background. When home button is hit the app
For a bit of background to know what i am doing: using hit highlighter
Background: Our web app uses the jquery.constrain.js plugin to handle data entry in some
Background I have a dimension table that has a single record for each day.
Background, Part 1 I have a form that collects both frequency and duration from
background: I work on an asp.net web application that is on a company intranet.
BACKGROUND I've successfully embedded IronPython in my WinForm apps using techniques like the one
I have a custom button in an activity. It works fine. I hit back,
I'm trying to invalidate a timer when my app goes into background. The timer
I have a login page that has 2 tags that have a background image

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.