?
Solved

C# DataGridView Column showing Text based on an Int value

Posted on 2014-11-30
4
Medium Priority
?
1,108 Views
Last Modified: 2014-11-30
I have a C# application linked to a SQL Database.  There's a DataGridView in it, with AutoGenerateColumns On.  It's showing some products.

In my SQL Table, i have a column of type Int.  Instead of showing an Int, i would like to show a text in my DataGridView.
0 = Once, 1 = Mensually, 2 = Weekly, 3 = Annually, etc... (These text values are not within the database)

How can i achieve that?  

I know that i must remove the column with this Int because it's AutoGenerated with a command like this:
gridProducts.Columns.RemoveAt(gridProducts.Columns.Count);

Open in new window


But after that, i have no clue.

Thanks!
0
Comment
[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
  • 2
4 Comments
 
LVL 23

Accepted Solution

by:
Michael Fowler earned 2000 total points
ID: 40473013
I believe you can use the the cell formatting event to change the values
http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.cellformatting.aspx

For example
private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
   if (this.dataGridView1.Columns[e.ColumnIndex].Name == "intColumn")
   {
     switch (e.Value)
     {
        case 0:
           e.Value = "Once"
           break;
       case 1:
           e.Value = "Mensually"
           break;
       case 2:
           e.Value = "Weekly"
           break;
      }
}

Open in new window

0
 
LVL 10

Author Comment

by:Christian de Bellefeuille
ID: 40473072
Thanks man!  You saved me a lot of time!  I was trying to achieve this from RowAdded event but it was not firing.  

It work perfectly.  Here's the code i've used.

        private void gridProducts_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            if (gridProducts.Columns[e.ColumnIndex].Name == "ProdFreq")
            {
                string[] wFrequencies = "Once only|Monthly|Yearly|Weekly|Daily".Split('|');

                if (e.Value != null)
                    e.Value = wFrequencies[(int)e.Value];
            }
        }

Open in new window

0
 
LVL 23

Expert Comment

by:Michael Fowler
ID: 40473085
Glad to be of assistance

Just a quick note
You can initialize an array with values using curly braces rather than using the split function
string[] wFrequencies = {"Once only", "Monthly", "Yearly", "Weekly", "Daily"};

Open in new window

http://msdn.microsoft.com/en-AU/library/aa288453(v=vs.71).aspx
0
 
LVL 10

Author Comment

by:Christian de Bellefeuille
ID: 40473087
thanks for pointer
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Suggested Courses

777 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