How to Display an Image in Devexpress XtraGrid Control using C# and VB.NET

To Display an Image in Devexpress XtraGrid Control using C# and VB.NET see the example below.

Sample C#

#region

using System.Data;
using System.Drawing;
using System.Windows.Forms;
using WindowsFormsApplication77.Properties;
using DevExpress.XtraEditors.Repository;

#endregion

namespace de.fesslersoft.XtraGridImageTest
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            gridControl1.DataSource = CreateTable(3);
            gridView1.Columns["Image"].ColumnEdit = new RepositoryItemPictureEdit();
        }

        private DataTable CreateTable(int rowCount)
        {
            var image = Resources.Image1;
            var tbl = new DataTable();
            tbl.Columns.Add("ID", typeof (int));
            tbl.Columns.Add("URL", typeof (string));
            tbl.Columns.Add("Image", typeof (Image));
            for (var i = 0; i < rowCount; i++)
            {
                tbl.Rows.Add(new object[] {i, "http://codesnippets.fesslersoft.de", image});
            }
            return tbl;
        }
    }
}

Sample VB.NET

#Region ""

Imports System.Data
Imports System.Drawing
Imports System.Windows.Forms
Imports WindowsFormsApplication77.Properties
Imports DevExpress.XtraEditors.Repository

#End Region

Namespace de.fesslersoft.XtraGridImageTest
	Public Partial Class Form1
		Inherits Form
		Public Sub New()
			InitializeComponent()
			gridControl1.DataSource = CreateTable(3)
			gridView1.Columns("Image").ColumnEdit = New RepositoryItemPictureEdit()
		End Sub

		Private Function CreateTable(rowCount As Integer) As DataTable
			Dim image = Resources.Image1
			Dim tbl = New DataTable()
			tbl.Columns.Add("ID", GetType(Integer))
			tbl.Columns.Add("URL", GetType(String))
			tbl.Columns.Add("Image", GetType(Image))
			For i As var = 0 To rowCount - 1
				tbl.Rows.Add(New Object() {i, "http://codesnippets.fesslersoft.de", image})
			Next
			Return tbl
		End Function
	End Class
End Namespace

Result

XtraGrid ColumnImages

XtraGrid ColumnImages


Please rate this snippet

How to reload a ViewController in IOS using Objective-C

To reload a UITableViewController in IOS using Objective-C you can simply use the reloadData method of the ViewController.

//MyTablViewController is a UITableViewController
[MyTableViewController.tableView reloadData] 

Please rate this snippet

How to create an alert dialog in Android

To create an alert dialog in Android you can use the following snippet.

Sample Java

new AlertDialog.Builder(this)
    .setTitle("Delete items")
    .setMessage("Are you sure you want to delete this items?")
    .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) { 
            // YES
        }
     })
    .setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) { 
            // NO
        }
     })
    .setIcon(android.R.drawable.ic_dialog_alert)
     .show();

Please rate this snippet

How to format a float to 2 decimal places in Objective-C

To format a float to 2 decimal places in Objective-C you can use the following snippet.

Sample Objective-C

NSString* formattedVariable = [NSString stringWithFormat:@"%.02f", myFloatVariable];

Please rate this snippet

How to query a DataTable using LINQ in C# and VB.NET

To query a DataTable using LINQ in C# and VB.NET you can use the snippet below.
In order to use the snippet, you need to reference System.Data.DataSetExtensions.

Sample C#

var resultData = (from myDataRow in myDataTable.AsEnumerable() where myDataRow.Field<Int32>("Fieldname") == 1 select myRow);

Sample VB.NET

Dim resultData = (From myDataRow In myDataTable.AsEnumerable() Where myDataRow.Field(Of Int32)("Fieldname") = 1myRow)

Please rate this snippet

How to join two lists in C# and VB.NET

To join two lists in C# and VB.NET you can use one of the following snippet.

Samples C#

//INIT
var listOne = new List<int>() { 1, 2, 3, 4, 5 };
var listTwo = new List<int>() { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };

//using Concat, this does not modify listOne. listThree includes all values, even the duplicates.
var listThree = listOne.Concat(listTwo).ToList();

//using Union, this does not modify listOne. listFour includes all values, without duplicates.
var listFour = listOne.Union(listTwo).ToList();

//using AddRange, this modifies listOne. listOne includes all values, even the duplicates.
listOne.AddRange(listTwo);

Samples VB.NET

'INIT
Dim listOne = New List(Of Integer)() From { 1, 2, 3, 4, 5 }
Dim listTwo = New List(Of Integer)() From { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }

'using Concat, this does not modify listOne. listThree includes all values, even the duplicates.
Dim listThree = listOne.Concat(listTwo).ToList()

'using Union, this does not modify listOne. listFour includes all values, without duplicates.
Dim listFour = listOne.Union(listTwo).ToList()

'using AddRange, this modifies listOne. listOne includes all values, even the duplicates.
listOne.AddRange(listTwo)

Please rate this snippet

How to remove an element using Javascript

To remove an element using javascript, you can use one of the following snippets.

Sample Javascript

//usage document.getElementById("ElementId").remove();
Element.prototype.remove = function() {
	this.parentElement.removeChild(this);
}

Sample Javascript

//usage document.getElementsByClassName("Elements").remove();
NodeList.prototype.remove = HTMLCollection.prototype.remove = function() {
	for(var index = 0, length = this.length; index < length; index++) {
		if(this[index] && this[index].parentElement) {
			this[index].parentElement.removeChild(this[index]);
		}
	}
}

removeChild() is the only method supported by the DOM to remove a element in Javascript (see -> Mozilla MDN Node.removeChild()).

Please rate this snippet

How to measure execution time of a Action in C# and VB.NET

To measure execution time of a Action in C# and VB.NET you can use the following Extension Method snippet.

Sample C#

public static TimeSpan Time(this Action @this)
{
	var watch = Stopwatch.StartNew();
	@this();
	watch.Stop();
	return watch.Elapsed;
}

Sample VB.NET

<System.Runtime.CompilerServices.Extension> _
Public Shared Function Time(this As Action) As TimeSpan
	Dim watch = Stopwatch.StartNew()
	this()
	watch.Stop()
	Return watch.Elapsed
End Function

Please rate this snippet

How to count the DOM loading time in Javascript

To count the DOM loading time in Javascript you can use the following snippet.

Sample Javascript

var dateBefore = new Date();

window.onload = function() {
document.getElementById("aDivId").innerHTML = (new Date()).getTime() - dateBefore.getTime(); 	
}

Please rate this snippet