Friday, August 17, 2012

Display row number in WinForms DataGridView

In WinForms Applications, to display the row number in the row header, we could use the RowPostPaint event of DataGridView control.


USAGE:
Suppose grid is named as dgvUserDetails

DELEGATE:

this.dgvUserDetails.RowPostPaint += new System.Windows.Forms.DataGridViewRowPostPaintEventHandler(this.dgvUserDetails_RowPostPaint);

CODE:
private void dgvUserDetails_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
{
        using (SolidBrush b = new SolidBrush(dgvUserDetails.RowHeadersDefaultCellStyle.ForeColor))
        {
              e.Graphics.DrawString((e.RowIndex + 1).ToString(), e.InheritedRowStyle.Font, b, e.RowBounds.Location.X + 10, e.RowBounds.Location.Y + 4);
        }
}


Don’t try to manipulate the Code part much because the X and Y are calculated co-ordinates in the row header area or you can customizse test it yourself to see the various results.

Monday, July 9, 2012

Determine whether Java version is 32-Bit or 64-Bit

Sun Java has provided a System property to determine whether installed version supports 32-Bit or 64 Bit named "sun.arch.data.model".


sun.arch.data.model=32 // 32 bit JVM
sun.arch.data.model=64 // 64 bit JVM
Try the following in your Java program: System.out.print(System.getProperty("sun.arch.data.model")) ; Other option is on command line use the following command, it will give you the details about the version and Bits:
java - version





C:\>java -version
java version "1.6.0_31"
Java(TM) SE Runtime Environment (build 1.6.0_31-b05)
Java HotSpot(TM) 64-Bit Server VM (build 20.6-b01, mixed mode)

Wednesday, December 21, 2011

Extract Numbers from a String


public static List extractNumsFromStr(String line) {
List numbers = new ArrayList();
Long no;
String number = null;
Pattern p = Pattern.compile("\\d+");
Matcher m = p.matcher(line);

while (m.find()) {
number = m.group();
no = Long.parseLong(number);
numbers.add(no);
}
return numbers;
}