Solved

C# DataGridView Column showing Text based on an Int value

Posted on 2014-11-30
4
970 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

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

Suggested Solutions

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Via a live example, show how to shrink a transaction log file down to a reasonable size.
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.

828 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