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

  • SEARCH
  • Home
  • 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 6238535
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T11:13:59+00:00 2026-05-24T11:13:59+00:00

I am using a DataGridView in a Win Forms app that is bound to

  • 0

I am using a DataGridView in a Win Forms app that is bound to a BindingList and I would like to improve on the “separation” of business-logic and presentation.

In my Form_Load event, I call a routine to build a BindingList and then I set the .DataSource of the DGV to this BindingList:

private void initializeFileList(string rootFolder) // populate grid with .xml filenames to be processed
    {
        String root = rootFolder;
            var result = Directory.GetFiles(root, "*.xml", SearchOption.AllDirectories)
                    .Select(name => new InputFileInfo(name))
                    .ToList();
            _filesToParse =  new BindingList<InputFileInfo>(result.ToList());
            dataGridView1.DataSource = _filesToParse;
            dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;
            dataGridView1.Columns["Rows"].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
            dataGridView1.Columns["Message"].DefaultCellStyle.ForeColor = Color.Red;

It’s the last 2 statements that bother me; as you can see, I want minor cosmetic changes to the columns created from the “Rows” and “Message” properties. It feels smelly that I should be hardcoding the properties of my custom object in these last 2 lines.

Would there be a more elegant way of customizing these 2 columns of the DGV – with the goal of fully exploiting the binding provided by: dataGridView1.DataSource = _filesToParse; In other words, still customize the columns but do so from something in the “business” object rather than the current technique.

Here is my InputFileInfo class (from another project in same solution):

namespace CBMI.Common
{
public class InputFileInfo : INotifyPropertyChanged
{
    private bool processThisFile;
    public bool Process
    {
        get { return processThisFile; }
        set
        {
            Utilities.Set(this, "Process", ref processThisFile, value, PropertyChanged);
        }
    }
    public string FileName { get; set; }
    private long rowsReturned;
    public long Rows
    {
        get { return rowsReturned; }
        set
        {
            Utilities.Set(this, "Rows", ref rowsReturned, value, PropertyChanged);
        }
    }
    private string message;
    public string Message
    {
        get { return message; }
        set
        {
            Utilities.Set(this, "Message", ref message, value, PropertyChanged);
        }
    }
    // constructor
    public InputFileInfo(string fName)
    {
        Process = true;
        FileName = fName;
        Rows = 0;
        Message = String.Empty;
    }
    public event PropertyChangedEventHandler PropertyChanged;
}
public static class Utilities
{
public static void Set<T>(object owner, string propName,
    ref T oldValue, T newValue, PropertyChangedEventHandler eventHandler)
{
    // make sure the property name really exists
    if (owner.GetType().GetProperty(propName) == null)
    {
    throw new ArgumentException("No property named '" + propName + "' on " + owner.GetType().FullName);
    }
    // we only raise an event if the value has changed
    if (!Equals(oldValue, newValue))
    {
        oldValue = newValue;
        if (eventHandler != null)
        {
        eventHandler(owner, new PropertyChangedEventArgs(propName));
        }
    }
}

}

}

  • 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-05-24T11:14:00+00:00Added an answer on May 24, 2026 at 11:14 am

    I don’t really see an alternative solution that is going to be easier than what you have provided. You shouldn’t mix some sort of CellStyle in with your data bound object as that is not good practice.

    The only thing I would suggest is disabling AutoGenerateColumns and define your own styling:

    private static void AdditionalInitialization(DataGridView dgv, object dataSource) {
    
     dgv.AutoGenerateColumns = false;
     dgv.DataSource = dataSource; // Needs to occur after the auto generate columns is set to off
     DataGridViewTextColumn msgCol = new DataGridViewTextColumn();
     msgCol.HeaderText = "Message";
     msgCol.DataPropertyName = "Message";
     msgCol.DefaultCellStyle.ForeColor = Color.Red;
     dgv.Columns.Add(msgCol);
    }
    

    This method could be placed in any of your classes since it is static, i.e. your InputFileInfo.cs

    Then when your form loads do this:

    private void initializeFileList(string rootFolder) // populate grid with .xml filenames to be processed
    {
        String root = rootFolder;
            var result = Directory.GetFiles(root, "*.xml", SearchOption.AllDirectories)
                    .Select(name => new InputFileInfo(name))
                    .ToList();
            _filesToParse =  new BindingList<InputFileInfo>(result.ToList());
            InputFileInfo.AdditionalInitialization(datagridview1,_filesToParse);
    

    }

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

in C# win app i displayed the data's in datagridview, in that i need
I am using a DataGridView control in a Windows Forms application. When a user
So, I have a DataGridView using as datasource a BindingList DataGridView.DataSource = new BindingList<Car>{...}
We'd like to override DataGridView's default behavior when using a mouse wheel with this
I have a DataGridView that I want to query using Linq (C# WinForm). I
My DataGridView control currently sorts using the Sort property of the bound data, but
I have a DataGridView that I'm trying to populate using a For loop: Dim
I have a DataGridView that displays data from a MS Access database. I'm using
I have an app that I wrote using C# .NET 4.0 in Visual Studio
I am building a table using the DataGridView where a user can select items

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.