Solved

C# DataGridView Column showing Text based on an Int value

Posted on 2014-11-30
4
921 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
Question by:cdebel
  • 2
  • 2
4 Comments
 
LVL 23

Accepted Solution

by:
Michael74 earned 500 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:cdebel
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:Michael74
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:cdebel
ID: 40473087
thanks for pointer
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

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.
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
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

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