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 6962031
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T15:36:26+00:00 2026-05-27T15:36:26+00:00

I am using the DataGridView component to quickly and easily display read-only SQL query

  • 0

I am using the DataGridView component to quickly and easily display read-only SQL query results to the user. I have it working as desired, but I have to wonder if I’m doing things the “right” way. It’s a complicated component after all, and I am completely new to SQL access and data binding in .NET.

The MSDN help suggests using a BindingSource object as the intermediary, so I’ve come up with the following code (which seems to work just fine):

mBindingSource.DataSource = null;
mBindingSource.Clear();

using (SqlDataReader query = GetQuery())
{
  if ((query != null) && (query.HasRows))
  {
    mBindingSource.DataSource = query;
    CDataGrid.DataSource = mBindingSource;
  }
}

However, I want to reformat some of this “raw” data. For example, some of the values are stored as int or byte types in the underlying tables, but they actually represent various enum values. Currently I am using the following code to perform the desired transformation (inspired by this MSDN page):

private void CDataGrid_CellFormatting(object sender, DataGridViewCellFormattingEventArgs args)
{
  DataGridViewColumn column = CDataGrid.Columns[args.ColumnIndex];
  switch (column.Name)
  {
    case FieldNameProductID:
    case FieldNameVersionID:
      int? x = args.Value as int?;
      ProductCode code = (ProductCode)(x ?? 0);
      args.Value = code.ToString();
      break;

    case FieldNameProductType:
      byte? y = args.Value as byte?;
      ProductType type = (ProductType)(y ?? 0);
      args.Value = type.ToString();
      break;
  }
}

Is this the proper way to do things? The reason I ask is because it seemed as if the BindingSource object is designed partially to perform such types of transformations. The documentation is hard to navigate, however, and I have yet to find a good example of what I’m trying to do.

  • 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-27T15:36:27+00:00Added an answer on May 27, 2026 at 3:36 pm

    This the correct way of doing it. The CellFormatting event captures the data before it is being rendered so it can be altered.

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

Sidebar

Related Questions

I'm using a datagridview to display the records of a database. I also have
I am using a DataGridView control in a Windows Forms application. When a user
I am building a table using the DataGridView where a user can select items
So, I have a DataGridView using as datasource a BindingList DataGridView.DataSource = new BindingList<Car>{...}
I am using DataGridView component in Windows Forms .Net. How to break line into
I am using Datagridview in asp.net. I have used custom buttons of up and
I am using a datagridview to get the IP address from user. Is there
I have a DataGridView using DataTable as my datasource. Now my problem is how
In a C# Windows application, I'm using a DataGridView to display all the data.
I do not have any idea on how to sort data using datagridview in

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.