May 072013
 
Share...Tweet about this on TwitterShare on FacebookShare on Google+Share on StumbleUponShare on LinkedInPin on PinterestShare on TumblrShare on RedditDigg this

After quite a long time there is a small update on the Google DataTable .NET Wrapper library.

For those who are new to the library please check the two previous posts:

What is new?

First of all I would like to thank Tom Ziesmer who has substantially contributed the code and ideas (suggestions) of this minor release.

IEnumerable converter to DataTable

Tom was very kind to contribute his code for the new extension method.
The extension method is mainly responsible for transforming any kind of IEnumerable object into the Google.DataTable.Net.Wrapper.DataTable object.
It equally works with anonymous as with strongly typed kind of IEnumerable objects.

The example below is showing how to use the new extension method that is part of the new namespace Google.DataTable.Net.Wrapper.Extension

var list = new[]
                {
                    new {Name = "Dogs", Count = 5},
                    new {Name = "Cats", Count = 2}
                };

var json = list.ToGoogleDataTable()
               .NewColumn(new Column(ColumnType.String, "Name"), x => x.Name)
               .NewColumn(new Column(ColumnType.Number, "Count"), x => x.Count)
               .Build()
               .GetJson();

this generates the following output:

{
"cols": [
	    {"type": "string", "id": "Name"},
	    {"type": "number", "id": "Count"}
	],

"rows": [
	    {"c": [{ "v": "Dogs" }, {"v": 5}]},
	    {"c": [{ "v": "Cats" }, {"v": 2}]}
	]
}

The NewColumn method offers a great way of customizing the output. But in case there is an object that is already “ready” to be used then the same result can be obtained by not calling the “NewColumn” method at all

var list = new[]
                {
                    new {Name = "Dogs", Count = 5},
                    new {Name = "Cats", Count = 2}
                };

var json = list.ToGoogleDataTable()
               //.NewColumn(new Column(ColumnType.String, "Name"), x => x.Name)
               //.NewColumn(new Column(ColumnType.Number, "Count"), x => x.Count)
               .Build()
               .GetJson();

produces the following output, which is substantially the same as the one above.

{
"cols": [
		{"type": "string","id": "Name", "label": "Name" },
		{"type": "number","id": "Count","label": "Count"}
	],
"rows": [
		{ "c": [{"v": "Dogs"},{"v": 5}]},
		{ "c": [{"v": "Cats"},{	"v": 2}]}
	]
}

System.Data.DataTable extension method

As shown in the below example, a new extension method for System.Data.DataTable has been introduced. This is mainly a wrapper to the existing SystemDataTableConverter, but is just handier to use.
This code demonstrate it’s usage:

using (var sysDt = new System.Data.DataTable())
{
    sysDt.Columns.Add("firstcolumn", typeof(string));
    sysDt.Columns.Add("secondcolumn", typeof(int));
    sysDt.Columns.Add("thirdcolumn", typeof(decimal));
    sysDt.Locale = CultureInfo.InvariantCulture;

    var row1 = sysDt.NewRow();
    row1[0] = "Ciao";
    row1[1] = 10;
    row1[2] = 2.2;
    sysDt.Rows.Add(row1);

    var dataTable = sysDt.ToGoogleDataTable();    
    
    var json = dataTable.GetJson();
}

How to install

To download or consult the latest source code please visit the project’s CodePlex page or install it directly from Visual Studio by using Nuget

PM> Install-Package Google.DataTable.Net.Wrapper

Contribute

This project is open for contribution and new ideas, so you are very welcome to contact me!

    Share...Tweet about this on TwitterShare on FacebookShare on Google+Share on StumbleUponShare on LinkedInPin on PinterestShare on TumblrShare on RedditDigg this

    I'm a Software Developer and Solution Architect interested in Software Development, Object-Oriented Design and Software Architecture all this especially bound to the Microsoft.NET platform.Feel free to contact me or know more in the about section

      8 Responses to “What’s new in Google DataTable .NET Wrapper v3.1.0”

    1. G’day Zoran, this .Net wrapper is awesome. Do you have any C# code examples of the use of the Role column in adding it to a DataTable? I want to implement the alternative tooltips that google offer, but they require the ‘role’ column. If I extract a third colmn of data from my database with the column name ‘tooltip’, how to I link it to the role column in your latest revisions of the wrapper?

      • Hi Fendavo,
        1) Documentation about Google DataTable roles: https://developers.google.com/chart/interactive/docs/roles
        2) In order to use a “role”, you can set it up as follows

        var column1 = new Column(ColumnType.String, “Tiers”);
        var column2 = new Column(ColumnType.Number, “Apps”);
        //here you set the role. ColumnRole is an enum that contains predefined roles.
        column2.Role = ColumnRole.Annotation;
        dt.AddColumn(column1);
        dt.AddColumn(column2);

        I hope it helps .
        Zoran

        • Thanks Zoran,
          I read the google notes on roles and I finally solved it on the .aspx side by adding the following line in my JavaScript to set the third column (0,1,2) to have the role of tooltip

          data8.setColumnProperty(2, ‘role’, ‘tooltip’);

          If you are interested I can post the C# code and the datatables and my JavaScript if it helps others.

          I wanted to use your ColumnRole, but I wasn’t sure where to put it, because I am retrieving data using a stored procedure into a C# DataTable then using your wrapper to convert it to json. I was retrieving three columns and the third column contained the tooltip. Everything was falling over until I added the setColumnProperty to tell the json that the third column was the role of tooltip.

          I love your .Net Wrapper!

          Thanks again

          • Hi Fendavo,
            I am glad that you sorted out your problem.
            On codeplex where the code for the .Net wrapper is hosted I have a test project, so It would be good updating it with your code, as to be honest I only covered the simplest examples there.
            So, yes, please send me the code you think is useful and I will integrate it to the test project.

            Best Regards,
            Zoran

    2. Do you have any code examples in c# showing how you implement the ‘role’ column and add it to the data table. I want to use the alternative tooltips tha Google discuss.

    Leave a Reply

    bacurin-alisha@mailxu.com saras.erin@mailxu.com