Wednesday, May 23, 2012

Pivot (crosstab) table in C#

How to create pivot table in C#


It exists well-know solution for pre-sorted and pre-grouped values.
What if we would like to pivot free set? Below you will find my solution.

     public DataTable Pivot(
            DataTable src,
            string VerticalColumnName,
            string HorizontalColumnName,
            string ValueColumnName ) 
        {

                DataTable dst = new DataTable();
                if (src == null || src.Rows.Count == 0)
                    return dst;

                // find all distinct names for column and row
                ArrayList ColumnValues  = new ArrayList();
                ArrayList RowValues     = new ArrayList();
                foreach (DataRow dr in src.Rows)
                {
                    // find all column values
                    object column = dr[VerticalColumnName];
                    if (!ColumnValues.Contains(column))
                        ColumnValues.Add(column);

                    //find all row values
                    object row = dr[HorizontalColumnName];
                    if (!RowValues.Contains(row))
                        RowValues.Add(row);
                }

                ColumnValues.Sort();
                RowValues.Sort();

                //create columns
                dst = new DataTable();
                dst.Columns.Add(VerticalColumnName, src.Columns[VerticalColumnName].DataType);
                Type t = src.Columns[ValueColumnName].DataType;
                foreach (object ColumnNameInRow in RowValues) {
                    dst.Columns.Add(ColumnNameInRow.ToString(), t);
                }

                //create destination rows
                foreach (object RowName in ColumnValues) {
                    DataRow NewRow = dst.NewRow();
                    NewRow[VerticalColumnName] = RowName.ToString();
                    dst.Rows.Add(NewRow);
                }

                //fill out pivot table
                foreach (DataRow drSource in src.Rows) {
                    object key = drSource[VerticalColumnName];
                    string ColumnNameInRow = Convert.ToString(drSource[HorizontalColumnName]);
                    int index = ColumnValues.IndexOf(key);
                    dst.Rows[index][ColumnNameInRow] = sum(dst.Rows[index][ColumnNameInRow], drSource[ValueColumnName]);
                }

                return dst;
        }

        dynamic sum(dynamic a, dynamic b) {
            if (a is DBNull && b is DBNull)
                return DBNull.Value;
            else if (a is DBNull && !(b is DBNull))
                return b;
            else if (!(a is DBNull) && b is DBNull)
                return a;
            else
                return a + b;
        }
Proof:
            object DBNULL = DBNull.Value;
            DataTable src = new DataTable();
            DataTable dst = null;

            src.Columns.Add("City", typeof(string));
            src.Columns.Add("Product", typeof(string));
            src.Columns.Add("Value", typeof(float));
            
            src.Rows.Add("City A", "Product 1", 1.2f);
            src.Rows.Add("City C", "Product 2", 0.3f);
            src.Rows.Add("City A", "Product 1", 1.0f);
            src.Rows.Add("City B", "Product 3", 2.2f);
            src.Rows.Add("City B", "Product 1", 1.5f);
            src.Rows.Add("City A", "Product 2", 0.8f);
            src.Rows.Add("City A", "Product 4", 1.1f);

            PivotTable test = new PivotTable();
            dst = test.Pivot(src, "City", "Product", "Value");

            CollectionAssert.AreEqual(new object[] { "City A", 2.2f,    0.8f,   DBNULL, 1.1f   }, dst.Rows[0].ItemArray);
            CollectionAssert.AreEqual(new object[] { "City B", 1.5f,    DBNULL, 2.2f,   DBNULL }, dst.Rows[1].ItemArray);
            CollectionAssert.AreEqual(new object[] { "City C", DBNULL,  0.3f,   DBNULL, DBNULL }, dst.Rows[2].ItemArray);


Enjoy!

Tuesday, April 17, 2012

C# (c sharp) function list


C# has no functions in the ordinary sense. So you if you are looking for absolute value you can't call abs() any more and should use static method from Math class!

Math.Abs(foo_bar);

So you can forget right now about function and find some useful methods of class which grouped in namespaces.

Namespace overview you will find here. Base classes are collected in System namespace. And useful list and hashes are located in System.Collections namespace.

Enjoy!

Sunday, April 1, 2012

how to change proxy in samsung galaxy android



changing proxy settings on android smartphones (samsung, galaxy, htc, etc)


EN: Wireless & networks Wi-Fi settings Proxy Settings and  Manual

DE: WLAN Einstellungen > Menütaste: Erweitert > Proxy/Port


RU: Беспроводные сети Настройки WiF  >  Настройка сети  >  Настройки прокси-сервера и Ручные




Wednesday, March 28, 2012

get asp.net sources


ASP.NET is a free web framework for building great web sites and applications.
This site is the home of the ASP.NET MVCWeb API, and Web Pages source code. If you want to use the released versions of these products to develop your applications, visit http://www.asp.net to find official installers, documentation, tutorials, samples, and videos.
These products are actively developed and supported by the Microsoft ASP.NET team in collaboration with a community of open source developers. Together we are dedicated to creating the best possible platform for web development.

Enjoy!

Monday, March 26, 2012

FunctionList Plugin is compatible with Notepad++ 6.0

Recently was released Notepad++ 6.1. You can take FunctionList plugin  v2.1 here. And enjoy with list of functions.


Enjoy it!

Wednesday, March 7, 2012

notepad++ contoh pemograman or plugin example

You would like to create own plugin for notepad++ and looking for some examples?
Just take existing sources and good guide.

What also you need? Right, Visual Studio to write/compile/build/debug. Look here.
or buy it

Friday, March 2, 2012

otrs Cc and Bcc autocomplete


If you miss autocomplete in Cc or Bcc fields just add piece of code into AgentCustomerSearch.dtl template

        
    Core.Agent.CustomerSearch.Init($("#Cc"), $QData{"ActiveAutoComplete"});
    Core.Agent.CustomerSearch.Init($("#Bcc"), $QData{"ActiveAutoComplete"});

Enjoy!