Solved

C# DataGridView Column showing Text based on an Int value

Posted on 2014-11-30
4
826 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
thanks for pointer
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Let's review the features of new SQL Server 2012 (Denali CTP3). It listed as below: PERCENT_RANK(): PERCENT_RANK() function will returns the percentage value of rank of the values among its group. PERCENT_RANK() function value always in be…
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
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

772 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now