The blog has moved to a new address. The blog is now located at http://devintelligence.com

Adsense

Wednesday, February 28, 2007

How To Print a DataGridView in C# and .NET

The DataGridView control in .NET 2.0 is an incredible data representation control, and contains many features that you could benefit from. The only thing that is not supported by this control is the printing feature.Salan Al-Ani created the class for this feature and share it with others.

PowerShell Editor and Analyzer

PowerShell Analyzer is a rich interactive environment for Windows PowerShell. Its goal is to be the PowerShell host of choice for IT professionals and system administrators. It has all the typical editor and IDE functionality that you would expect when working on a modern language, but it focuses on the real time interactive experience as if you were at the console, helping you compose the commands you want to use, and also giving you rich graphical visualization of the results. PowerShell may seem to just return text like the average Unix shell, but in reality, it is returning rich self describing Dotnet objects. PowerShell Analyzer not only helps you with the INPUT, but also with the output. It shows you the properties of the rich objects that the commands return, as well as specific visualizers to help you interact with output such as XML, HTML, charts, tables, and hierarchical data structures.

 

Download PowerShell Analyzer

Video Demos

Author's Blog

Sunday, February 18, 2007

Change font style for specific cell in datagridview

The way to change font for a single cell in an unbound datagridview.


 



int columnIndex = 1;
int rowIndex = 2;
// create bold font based on the default font
Font newFont = new Font(dataGridView1.Font, FontStyle.Bold);
dataGridView1[columnIndex, rowIndex].Style.Font
= newFont;


 


Using this simple technique you can change foreground or background color ( and many more style related properties ) for specific cell .

Monday, February 05, 2007

Propertygrid: How to show and edit "aggregated" property

Sometimes we need to edit  aggregated property - I mean the property that represents object with few properties .The simple way to do this - we can use TypeConverter with ExpandableObjectConverter as shown in the example

 

 

[TypeConverter(typeof(ExpandableObjectConverter))]
public class FullName
{
private string _FirstName;
private string _LastName;


public FullName(string _FirstName, string _LastName)
{
this._FirstName = _FirstName;
this._LastName = _LastName;
}

[DisplayName(
"First Name")]
[Description(
"First name of the person")]
public string FirstName
{
get
{
return _FirstName;
}
set
{
_FirstName
= value;
}
}

[DisplayName(
"Last Name")]
[Description(
"Last name of the person")]
public string LastName
{
get
{
return _LastName;
}
set
{
_LastName
= value;
}
}


///<summary>
///Returns a <see cref="T:System.String"></see>
/// that represents the current <see cref="T:System.Object"></see>.
///</summary>
///
///<returns>
///A <see cref="T:System.String"></see>
/// that represents the current <see cref="T:System.Object"></see>.
///</returns>
///<filterpriority>2</filterpriority>
public override string ToString()
{
return _FirstName + " " + _LastName;
}
}


 


 



[DisplayName("Full Name")]
[Description(
"Full name of the person")]
public FullName FullName
{
get
{
return _FullName;
}
set
{
_FullName
= value;
}
}


Result - the editor for aggregated object



Source( russian )

Propertygrid: How to display combo with icons for enum type?

We need to cretate new type inhereted from UITypeEditor with custom painting( in our case the images located in resource file - where each image name equals to memeber of the enum ) as shown below

 

public class CountryEditor : UITypeEditor
{
///<summary>
///Indicates whether the specified context supports
/// painting a representation of an object's value within the specified context.
///</summary>
///
///<returns>
///true if
/// <see cref="M:System.Drawing.Design.UITypeEditor.PaintValue(System.Object,System.Drawing.Graphics,System.Drawing.Rectangle)">
/// </see> is implemented; otherwise, false.
///</returns>
///
///<param name="context">An <see cref="T:System.ComponentModel.ITypeDescriptorContext">
/// </see> that can be used to gain additional context information. </param>
public override bool GetPaintValueSupported(ITypeDescriptorContext context)
{
return true;
}

///<summary>
///Paints a representation of the value of an
/// object using the specified <see cref="T:System.Drawing.Design.PaintValueEventArgs"></see>.
///</summary>
///
///<param name="e">A <see cref="T:System.Drawing.Design.PaintValueEventArgs">
/// </see> that indicates what to paint and where to paint it. </param>
public override void PaintValue(PaintValueEventArgs e)
{
string resourcename = Enum.GetName(typeof (Country), e.Value);

// retrive image from resource file
Bitmap countryImage =
(Bitmap)Resources.ResourceManager.GetObject(resourcename);
Rectangle destRect
= e.Bounds;
countryImage.MakeTransparent();

// paint
e.Graphics.DrawImage(countryImage, destRect);

}
}



 


Create new EnumTypeConverter


public class EnumTypeConverter : EnumConverter
{
private Type _enumType;
public EnumTypeConverter(Type type)
:
base(type)
{
_enumType
= type;
}

public override bool CanConvertTo(ITypeDescriptorContext context,
Type destType)
{
return destType == typeof(string);
}

public override object ConvertTo(ITypeDescriptorContext context,
CultureInfo culture,
object value, Type destType)
{
FieldInfo fi
= _enumType.GetField(Enum.GetName(_enumType, value));
DescriptionAttribute dna
=
(DescriptionAttribute)Attribute.GetCustomAttribute(
fi,
typeof(DescriptionAttribute));

if (dna != null)
return dna.Description;
else
return value.ToString();
}

public override bool CanConvertFrom(ITypeDescriptorContext context,
Type srcType)
{
return srcType == typeof(string);
}

public override object ConvertFrom(ITypeDescriptorContext context,
CultureInfo culture,
object value)
{
foreach (FieldInfo fi in _enumType.GetFields())
{
DescriptionAttribute dna
=
(DescriptionAttribute)Attribute.GetCustomAttribute(
fi,
typeof(DescriptionAttribute));

if ((dna != null) && ((string)value == dna.Description))
return Enum.Parse(_enumType, fi.Name);
}

return Enum.Parse(_enumType, (string)value);
}


}

 


 


Then use attribute Editor


 



[DisplayName("Country")]
[Description(
"Country")]
[TypeConverter(
typeof(EnumTypeConverter))]
[Editor(
typeof(CountryEditor), typeof(UITypeEditor))]
public Country Country
{
get
{
return _Country;
}
set
{
_Country
= value;
}
}


 


 Now we can see the combo box as editor for our Enum type


 


Source ( russian )