Infragistics, Javascript, Jquery

Infragistics igGrid Get Row Data from Javascript

This isn’t well documented (or at  least I had trouble finding the answer to this), so I figured there may be others (or me again down the road) that could use this information.

Essentially I have a grid loaded from a datasource.  I needed to get at the data of a particular row from outside of any grid event, so I had to retrieve the rows from the grid, find the row that I needed and then reference the data.   All in all, once you know the syntax, it’s not that bad.
This line returns an array of the row data:

var dataRow = $("#igDetails").igGrid("dataSourceObject").Records;

Next we want to retrieve a particular row based on a value in a particular column:

var result = $.grep(dataRow, function (e) { return e.Id === id; });

Result will be an array of rows that match your criteria. In our case I just want the first match (ID will be unique, so should be only one row)

Now to retrieve the data:

var description = result[0].Description;

Pretty simple really, but the retrieving the data from the grid took some looking and piecing parts of the documentation together to get what I needed.

.NET, C#, Tips

IEnumerable to Comma Delimited String

A few years ago I blogged about how to take a delimited string and convert it to a IEnumerable.  Today we ran across the opposite of this in that we wanted to take a list and convert it to a delimited string.   This can come in handy for a number of situations so it’s worth taking a look.

Given:

List<int> values = new List<int> {10, 20, 25};

In older versions of .NET (prior to 4.0 I believe) the easiest way was to do something like the following:

string newString = string.Join(",", values.ToArray());

However, starting in 4.0, the Join has some additional overloads that make this just crazy simple:

string newString = string.Join(",", values);

If you have a list of strings, there is still some benefit to converting this to an array. There is another overload of Join that allows you to be more selective in what you are converting.

Given a list of strings like the following:

List<string> strings = new List<string> {"a", "b", "c", "d", "e", "f"};

We can look at the last overload of; String.Join(delimiter, string[], startIndex, count)

This gives us a delimited string, starting at the element in the array at startIndex and going for the number of count.

string delimited = string.Join(",", strings.ToArray(), 2, 2);

will return “c,d”

I love how the framework provides these methods, often with multiple overloads to give us a lot of power with very little code. Enjoy!

.NET, Resharper

Resharper – Navigate to Type Goodness

In my opinion the navigation improvements made by Resharper are where much of my productivity gains come into play.  When I can quickly find and navigate around my project, I can stay more focused on solving the problem at hand rather than trying to fumble through my project structure remembering where things are.

One of my favorites lately is Go To Type of Symbol (Ctrl-Shift-T in Resharper keymap, Ctrl-Shift-F11 in the Visual Studio keymap).  At it’s simplest, put your cursor on a variable and hit the keystroke.

Photo1

If there is only one match, it simply takes you to the definition of the type (in your source or object browser if not).  Simple, but quick and easy.

The fun begins when there is more than one type.   The handiest example of that is when I have a List<> variable.   Then the Go To Type brings up not only the IList<> ( which I realize may be of limited usage), but it also brings up the type in the list (which is normally what I want to get at).

photo2

This get’s me quickly to what I’m looking for.

One more shortcut in my arsenal to move me quickly around my project.  Enjoy!

Resharper

Resharper – Paste from Clipboard History

Sometimes it’s the simple things that make my day when I’m coding.  This is one of those items that once I bothered to learn the keystroke, has made my time coding that much more productive.

I’ve used Resharper for years (since v2) and while I’ve stumbled upon this feature a few times, I’ve never really bothered to get in the habit of using it.

It’s the Pasting Items from Clipboard History  feature.   Shortcut for this is Ctrl+Shift+Alt+V in both keyboard themes (Note:  I did notice that in my VS2015 RC installation the keystroke was Ctrl+Shift+V, so it could be either of those depending on your setup)

When you select the paste (and assuming you’ve pasted some items to the clipboard while in Visual Studio) you’ll be presented with the clipboard screen;

Resharper-Paste-Multiple

From that screen it’s simple to cursor down to the entry you want and hit Enter to paste it into your current location.  You can also select the number or letter that prefixes the selection.  Pretty simple.

In just two days of using this feature, I’d say it’s increased my performance when I’m making multiple similar changes in some code.   Sometimes it really just is the simple things that really make a difference.

.NET, Code

String.Contains looking for an empty string

Here’s another one of those little idiosyncrasies that I run across in coding.  In some ways I’m almost embarrassed to say I never knew this as it’s really quite simple and also well documented, but it’s one of those edge cases that just don’t happen that often and may not be that damaging of a bug if it does sneak through.

The construct I’m talking about is the String.Contains() method.   In .NET it simply returns a true/false on whether the string contains the string parameter passed into it.


string s1 = "This is a nice string";
bool b;
b = s1.Contains("nice"); // returns true;
b = s2.Contains("bad"); // returns false;

All nice and simple. The issue I ran into was when the string I was looking for was an empty string.


b = s1.Contains(""); // returns true

In my application, that requirements were that it should return false. However, this returns true. It is well documented, but not something I really had to deal with before.

Something to keep an eye out for as it could give you some false positives if you’re not careful.

Still learning after all these years.

Code, Links

Links to Code By #4

Another episode of my infrequent Links to Code by. My take on things old and new that are interesting to me and perhaps make my coding life easier, more entertaining or purely stretches my cerebral matter. Enjoy!

Giving Clarity to Linq Queries by Extending Expressions – What a great article. This is something I’ve done plenty of times before, but here is a formal write-up.  in this article, Edward talks about using the Pipe and Filter pattern to simplify and give clarity to your linq statements.  Through the judicious use these custom expressions, our linq queries become more concise and readable and the expressions become easily reusable.   Good stuff here.

The Wounded QR Code – Most everyone knows what a QR code is by now.  But do you know how they work?  Do you know how durable they are and how resistant to damage they can be?  Do you also realize how creative you can become with their design while still maintaining a working code?  Well this article is a great introduction (and goes beyond just introducing them) to the great QR code with a special emphasis on damage and reliability.

NAudio 1.7 – I’ve always been fascinated by audio programming from the days on my Commodore 64 doing MIDI programming (good resource here), but I’ve never had a reason to dive into it before…..I still don’t really, so perhaps this is just an empty promise to myself, but I am going to look at this deeper some day.

WebForms URL Routing – I’ve not done a ton of web programming over the last couple years so I’ve not kept up on the new advancements in ASP.net.   Here is a good example of something I just discovered this morning.  New in 4.0 (I think) the routing capabilities adds a new dimension to the whole web forms arena.  I have to do a lot more research and testing before I pronounce judgement on this, but it’s nice to know that it is here.

When I interview potential new developers, one question I usually ask is, “If given unlimited resources and time, what would you do?”  9 out of 10 developers say some form of “Develop games”.  If was asked that years ago, my answer probably would have been the same (it still may be for that matter).  Game development has always fascinated me even though I realize that is probably one of the most demanding forms of development there is.  Well here is a blog from one Jesse Freeman, who is a technology evangelist for Microsoft that focuses on Windows 8 game development.  Good stuff, worth the read.

Well that’s enough to keep you busy for this Saturday….until next time….

.NET, C#, Code, PetaPoco

PetaPoco Needs a Parameterless Constructor

I’ve been using PetaPoco for a few years as my micro-orm of choice.  It’s lightweight, simple to use, yet powerful enough for most of my needs.  Let me share one thing that I tend to forget and it seems to trip me up all the time.

In short, PetaPoco requires a parameterless constructor on the classes it’s trying to integrate with.

  public class Customer 
  {
    public Customer() 
    {
    }
  }

Or if there is no other constructor defined, the compiler will create that default constructor for you essentially and you don’t have to explicitly code it. This is where I usually run into trouble. I’ll depend on the compiler to give me the default constructor and code as follows.

  public class Customer 
  {
    public string Name {get;set;}
    public string Address {get;set;}
    // more properties and methods
  }

With this PetaPoco works flawlessly. No problems here. Now let’s say I create a constructor for one reason or another. For example, we want to pass the customer’s name in the constructor.

  public class Customer 
  {
    public string Name {get;set;}
    public string Address {get;set;}

    public Customer(string _name)
    {
      Name = _name;
    }
    // more properties and methods
  }

Now if we try to read in a list of customers with PetaPoco, we suddenly get a Null argument exception and it comes from deep inside PetaPoco where it’s not at first obvious what is going on. In fact, every time I get this error, I forget that I’ve seen it before and scratch my head for a few minutes…..thus this post…hopefully I’ll remember now.

Anyway, PetaPoco needs that parameterless constructor, which if we have no other constructor defined, will happily get created by the compiler. However, if we do provide a constructor like in the last example above, the compiler does not create that default constructor and we have to define it explicitly.

  public class Customer 
  {
    public string Name {get;set;}
    public string Address {get;set;}

    public Customer() 
    {
    }

    public Customer(string _name)
    {
      Name = _name;
    }
    // more properties and methods
  }

Once we do that, PetaPoco works beautifully once more.