VIBlend

Introducing Office 2007 style Conditional Formatting in SuperGridView

by viblend 25. August 2008 16:35

With the release of version 1.3, there's no doubt that SuperGridView has the most robust and flexible conditional formatting capabilities when compared to other .NET grid controls on the market. It also looks cool as you can see from the screenshot below.


hierarchical grid with conditional formatting

Here's the simple question: How hard is it implement this?

Well, SuperGridView makes it very very easy. Let's go through the process.

In order, to apply conditional formatting with color scales we must define a group of cells and associate with a specific color scale. This normally takes three easy steps.

Step 1. Enable Conditional Formatting and Create the Conditional Formatting group 

SuperGridView has a class which wraps this entire functionality. The class is defined within the DataFieldArea of the grid and it's name is ConditionalFormattingGroup. You can use it in the following way:

     grid.DataFieldArea.ConditionalFormattingEnabled = true;

     DataFieldArea.ConditionalFormattingGroup cfGroup = new DataFieldArea.ConditionalFormattingGroup();

     cfGroup.SetConditionalFormattingColorScale(GridCellCFColorScale.GREEN_TO_RED);

The SetConditionalFormattingColorScale method allows you to select one of the built-in color scales. If you prefer to create your own you can use the SetCustomConditionalFormattingColorScale method which simply takes an array of one hundered colors which define the scale.

Step 2. Add the group to the grid's DataFieldArea and assign in a name

This is just a single line of code which basically says: "Hey, SuperGridView, this is my ConditionalFormattingGroup and for you this is its name"

     grid.DataFieldArea.ConditionalFormattingGroups.Add("mygroup", cfGroup);

Step 3. Assign grid cells to the conditional formatting group

SuperGridView allows you to add individual cells to a group, as well as entire rows and columns. This is done by calling the methods SetCellConditionalFormattingGroup or SetItemConditionalFormattingGroup for individual cells and for columns/rows respectively.

   grid.DataFieldArea.SetCellConditionalFormattingGroup(gridCell, "mygroup");

   grid.DataFieldArea.SetItemConditionalFormattingGroup(rowItem, "mygroup");

The following code will create a new conditional formatting group and fill it with a the selected cells:

   private void buttonConditionalFormatSelectedCells_Click(object sender, EventArgs e)
   {
        grid.DataFieldArea.ConditionalFormattingGroups.Clear();

        DataFieldArea.ConditionalFormattingGroup cfGroup = new DataFieldArea.ConditionalFormattingGroup();
        cfGroup.SetConditionalFormattingColorScale(GridCellCFColorScale.GREEN_TO_RED);

        grid.DataFieldArea.ConditionalFormattingGroups.Add("mygroup", cfGroup); 

        foreach (GridCell cell in grid.DataFieldArea.SelectedCells)
        {
             grid.DataFieldArea.SetCellConditionalFormattingGroup(cell, "mygroup"); 
         }
   }



Currently rated 4.0 by 5 people

  • Currently 4/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags:

GridView | VIBlend SuperControls for WinForms

Pivot Tables support in VIBlend SuperGridView

by viblend 1. August 2008 15:02

Pivot tables are one of the most powerful features of modern Spreadsheet applications like Microsoft Excel. They provide an easy way to group, aggregate and analyze data. 

In database tables data is represented as records which consist of one or more columns. It is extremely easy to visualize such data via almost any data grid component on the market. However, the value which one can get from a list of records is somewhat limited especially when the number of rows is large. Pivot tables normally consist of Row, Column and Data (or Value) fields. They allow the user to select which columns from the original record set will form each of these three types of fields.

SuperGridView allows developer to bind to a database table, select which table columns will be used in the data binding, as well as which table columns will form the pivot table rows, columns and data (value) fields.

Let's start with a simple scenarios where we have a binding to a databse table. Below is an example of a data bound SuperGridView control which displays the records from the Invoices table of the Northwind database. The sample code uses a data adapter to fill a Dataset, creates a BoundField object for every table column and adds it to the BoundFields collection of SuperGridView. Finally, it sets the DataSource property of SuperGridView and performs the actual data binding.

NwindDataSet_InovicesTableAdapters.InvoicesTableAdapter adapter =  new NwindDataSet_InovicesTableAdapters.InvoicesTableAdapter();

adapter.Fill(nwindDataSet_Inovices.Invoices);

DataTable tbl = this.nwindDataSet_Inovices.Invoices;

foreach (DataColumn col in tbl.Columns)
{


BoundField boundField = new BoundField(col.Caption, col.ColumnName);

superGrid1.BoundFields.Add(boundField);


}

      superGrid1.DataSource = tbl.DefaultView;
      superGrid1.DataBind();

      superGrid1.Refresh();

In this scenario SuperGridView will display all columns and all records from the Invoices table:
 

         

Now let’s use the same code and modify it to build a pivot table. In this example, we will create a pivot table which represents a Sales report. On Rows we will display the Cities grouped by Country, on Columns we will display the Shipping Company Name, and finally we’re interested to see the number of sales, the total dollar amount of the sales and the average sale amount as data fields. If it sounds complicated, the good news is that SuperGridView allows you to do that with just a few extra lines of code:

NwindDataSet_InovicesTableAdapters.InvoicesTableAdapter adapter = new NwindDataSet_InovicesTableAdapters.InvoicesTableAdapter();

adapter.Fill(nwindDataSet_Inovices.Invoices);

DataTable tbl = this.nwindDataSet_Inovices.Invoices;

grid.BoundPivotRows.Add(new BoundField("Country", "Country"));

grid.BoundPivotRows.Add(new BoundField("City", "City"));
grid.BoundPivotColumns.Add(new BoundField("Carrier", "Shippers_CompanyName"));

grid.BoundPivotValues.Add(new BoundValueField("Count of Sales", "ExtendedPrice", PivotFieldFunction.Count));

grid.BoundPivotValues.Add(new BoundValueField("Amount of Sales", "ExtendedPrice", PivotFieldFunction.Sum));

grid.BoundPivotValues.Add(new BoundValueField("Avg. Sale Amount", "ExtendedPrice", PivotFieldFunction.Average));

grid.PivotValuesOnRows = false;

foreach (DataColumn col in tbl.Columns)
{
BoundField boundField = new BoundField(col.Caption, col.ColumnName);

grid.BoundFields.Add(boundField);
}

grid.DataSource = tbl.DefaultView;
grid.DataMember = "";
grid.DataBind();

grid.RowsHierarchy.ExpandAllItems();
grid.ColumnsHierarchy.ExpandAllItems();
grid.ColumnsHierarchy.AutoResize();
grid.RowsHierarchy.AutoResize();
grid.Refresh();

And here is the result:

     

As you can see SuperGridView automatically builds hierarchies on rows and on columns and performs proper data aggregation to calculate the value of each cell. You can slightly modify this sample code and create almost any Pivot table.

We hope you will enjoy this cool feature of SuperGridView and most importantly its power, flexibility and simplicity.


Currently rated 4.0 by 6 people

  • Currently 4/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags: , , , ,

GridView

About the author

Some text that describes me

Recent comments

None

Copyright © 2009 VIBlend  
ALL RIGHTS RESERVED  
 
Terms of Use | Privacy Policy
WinForms Controls Purchase Online About Us
       
DataGrid Navigation Pane Technical Support Blog
ScrollBar TreeView
ListBox ProgressBar Downloads Register
ComboBox Buttons
TabControl Editors Documentation Client Login

VIBlend Blog is powered by BlogEngine.NET