Solved

C# DataGridView Column showing Text based on an Int value

Posted on 2014-11-30
4
1,042 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
[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 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: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:cdebel
ID: 40473087
thanks for pointer
0

Featured Post

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

707 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