Monday, July 21, 2014

Databinding ASP.NET 4.5 GridView with jQuery and Ajax

Hi,

Here's a simple asp.net program that performs adding of data to GridView control through jQuery and ajax. On page load, perform databinding on dropdown list and gridview using server side and code behind. And on client side, that is a selection change occurs in dropdown list, perform binding by adding table rows and columns to GridView.

Page Load (Load All Products):


Selection Change (Load Specific Products):

The sample code is available for download here: Databinding ASP.NET 4.5 GridView with jQuery and Ajax Code

Cheers!

Thursday, July 17, 2014

GeckoFX DocumentText similar to Webbrowser Control Alternative (C#)

In this post GeckoFX DocumentText similar to Webbrowser, I presented a solution on how to retrieve the page source of a website. However, another solution was provided by a .NET guru using TextContent property:
Code:
this.Document.GetElementsByTagName("html")[0].TextContent;

Cheers!

Monday, July 14, 2014

Paint WPF DataGridCell background w/o using IValueConverter

Here's how to paint WPF DatagridCell using extension methods without using IValueConverter.
Extension Method:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
Module DataGridExtensions
    <Extension()>
    Function GetRow(ByVal grid As DataGrid, ByVal index As Integer) As DataGridRow
        Dim row As DataGridRow = DirectCast(grid.ItemContainerGenerator.ContainerFromIndex(index), DataGridRow)
        If row Is Nothing Then
            grid.UpdateLayout()
            grid.ScrollIntoView(grid.Items(index))
            row = DirectCast(grid.ItemContainerGenerator.ContainerFromIndex(index), DataGridRow)
        End If
        Return row
    End Function
End Module

MainWindow.xaml:
1
2
3
4
Dim firstRow As DataGridRow = grid1.GetRow(0) 'grid1 is your DataGrid control
Dim firstColumnInFirstRow As Controls.DataGridCell = DirectCast(grid1.Columns(0).GetCellContent(firstRow).Parent, Controls.DataGridCell)
'set background
firstColumnInFirstRow.Background = Brushes.Red

Wednesday, July 9, 2014

Split a string with multiple space as delimiter or separator (Regex or String.Split)

Given that you have a string that you want to split and the separator is two or more spaces. You can achieve using split() or Regex.
VB.NET
1
2
3
4
5
6
7
8
Dim str As String = "Rule Name:                            WhosThere-176.44.28.203"
Dim whosThereVar As String
Dim whosThereString As String

'using split
whosThereVar = str.Split(New String() {"  "}, StringSplitOptions.RemoveEmptyEntries)(1).Trim()
'using regex
whosThereString = System.Text.RegularExpressions.Regex.Split(str, "\s{2,}")(1)
C#
1
2
3
4
5
string str = "Rule Name:                            WhosThere-176.44.28.203";
//using split
string whosThereVar = str.Split(new string[] { "  " }, StringSplitOptions.RemoveEmptyEntries)[1].Trim();
//using regex
string whosThereString = System.Text.RegularExpressions.Regex.Split(str, @"\s{2,}")[1];

Tuesday, July 1, 2014

Sorting a HashTable object using LINQ in .NET

In .NET, you can't directly sort a Hashtable object. A tip on sorting a Hashtable object is to cast it to Dictionary then apply LINQ OrderBy() method to the Hashtable object. See examples below:
VB.NET
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
Private Sub SortHashTableKey()
 
        hash = New Hashtable()
        hash.Add("B", 1)
        hash.Add("A", 2)
        hash.Add("C", 3)
 
        Dim dict = hash.Cast(Of DictionaryEntry)() _
                        .ToDictionary(Function(d) d.Key, Function(d) d.Value) _
                        .OrderBy(Function(e) e.Key)
 
        Console.WriteLine("Sort by Key")
        For Each item In dict.ToList()
            Console.WriteLine(String.Format("{0}, {1}", item.Key, item.Value))
        Next
 
        Console.WriteLine(Environment.NewLine)
 
    End Sub
 
    Private Sub SortHashTableValue()
        hash = New Hashtable()
        hash.Add("A", 2)
        hash.Add("B", 1)
        hash.Add("C", 3)
 
        Dim dict = hash.Cast(Of DictionaryEntry)() _
                   .ToDictionary(Function(d) d.Key, Function(d) d.Value) _
                   .OrderBy(Function(e) e.Value)
        Console.WriteLine("Sort by Value")
        For Each item In dict.ToList()
            Console.WriteLine(String.Format("{0}, {1}", item.Key, item.Value))
        Next
 
    End Sub

C#
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
private void SortHashTableKey() {
 Hashtable hash = new Hashtable();
 hash.Add("B", 1);
 hash.Add("A", 2);
 hash.Add("C", 3);

 var dict = hash.Cast < DictionaryEntry > ()
  .ToDictionary(d => d.Key, d => d.Value)
  .OrderBy(e => e.Key);

 listBox1.Items.Add("Sort by Key");

 foreach(var item in dict.ToList()) {
  listBox1.Items.Add(String.Format("{0}, {1}",
   item.Key, item.Value));
 }

 listBox1.Items.Add(Environment.NewLine);
}

private void SortHashTableValue() {
 Hashtable hash = new Hashtable();
 hash.Add("B", 1);
 hash.Add("A", 2);
 hash.Add("C", 3);

 var dict = hash.Cast < DictionaryEntry > ()
  .ToDictionary(d => d.Key, d => d.Value)
  .OrderBy(e => e.Value);

 listBox1.Items.Add("Sort by Value");

 foreach(var item in dict.ToList()) {
  listBox1.Items.Add(String.Format("{0}, {1}",
   item.Key, item.Value));
 }
}

Output:

Cheers! :)