Wednesday, July 30, 2008

ReadOnly ComboBox in .net

http://www.codeproject.com/KB/combobox/ExComboBox.aspx

Tuesday, July 29, 2008

Getting no. of days between two dates in vb.net 2.0

CStr(DateDiff(DateInterval.DayOfYear, startDate, endDate))

Monday, July 14, 2008

Convert String to Date and check Date Validation on Server side in C#.Net.

Convert String to Date and check Date Validation on Server side in C#.Net.

There are lots of functions available in client side for date validation.
Sometime server side date validation function require
It’s very simple but require much time to right. (C#.Net)

SupportedDateFormats : It is a customise function where you can refer the date format whatever you want.

ConvertStringToDate : This will return True or Flase based on date format supported. And give converted Date from String.

public static bool ConvertStringToDate(string dateString, ref DateTime resultDate)
{
try
{
resultDate = DateTime.ParseExact(
dateString,
SupportedDateFormats(),
System.Globalization.CultureInfo.CurrentCulture,
System.Globalization.DateTimeStyles.None
);
return true;
}
catch
{
return false;
}
}

//This is custom defined format function you can add more date format as per your requirement
like "dd/MM/YYYY", etc in the allFormats array
private static string[] SupportedDateFormats()
{
string[] allFormats = new string[] {
"MM/dd/yyyy",
"MM/dd/yy",
"M/d/yy",
"MM/d/yy",
"M/dd/yy",
"M/d/yyyy",
"MM/d/yyyy",
"M/dd/yyyy"};
return allFormats;
}

// Now you can use this function in your code like this
private void button1_Click(object sender, EventArgs e)
{
DateTime dtTemp;
dtTemp =DateTime.Today.Date;
lblIsvalidDate.Text =
ConvertStringToDate(
textBox2.Text,
ref dtTemp
).ToString();
lblCovertedDate = dtTemp.ToString();
}

Friday, July 11, 2008

Crystal Reports, Part I - Winforms

http://www.codeproject.com/KB/dotnet/CrystalWin.aspx

Friday, July 4, 2008

Gridview layout in windows application in .net 2.0

Public Sub gridLayout(ByVal dt As DataTable)

grdNextActioner.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.AutoSize
grdNextActioner.RowHeadersDefaultCellStyle.BackColor = Color.Blue
Dim columnHeaderStyle As DataGridViewCellStyle = New DataGridViewCellStyle
columnHeaderStyle.Font = New Font("Verdana", 8, FontStyle.Bold)
columnHeaderStyle.BackColor = Color.Blue
grdNextActioner.ColumnHeadersDefaultCellStyle = columnHeaderStyle
grdNextActioner.DataSource = dt.DefaultView
'grdNextActioner.Sort(grdNextActioner.Columns(strAssociationBy), ListSortDirection.Descending)
Dim rowCount As Integer = grdNextActioner.Rows.Count
'DataTable dtResultCopy = dt.Clone();

Dim cellwidth As Integer = ((grdNextActioner.Width _
- (grdNextActioner.TopLeftHeaderCell.Size.Width - 20)) _
/ grdNextActioner.Columns.Count)
Dim i As Integer = 0
For Each dtc As DataColumn In dt.Columns
grdNextActioner.Columns(i).Name = dtc.ColumnName
grdNextActioner.Columns(i).HeaderText = dtc.ColumnName
grdNextActioner.Columns(i).DataPropertyName = dtc.ColumnName
grdNextActioner.Columns(i).ReadOnly = True
grdNextActioner.Columns(i).Width = cellwidth
i = (i + 1)
Next
grdNextActioner.TopLeftHeaderCell.Value = Nothing
grdNextActioner.SelectionMode = DataGridViewSelectionMode.FullRowSelect
'grdNextActioner.RowHeadersWidth = (grdNextActioner.Width - grdNextActioner.TopLeftHeaderCell.Size.Width) / grdNextActioner.Columns.Count;
Me.grdNextActioner.RowsDefaultCellStyle.BackColor = Color.Wheat
Me.grdNextActioner.AlternatingRowsDefaultCellStyle.BackColor = Color.MistyRose

' Me.grdNextActioner.Rows(grdNextActioner.Rows.Count - 1).visible = False

End Sub

datagrid view double click event in windows application

Private Sub grdNextActioner_CellDoubleClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles grdNextActioner.CellDoubleClick
Dim id As Integer = 0
If (e.RowIndex > -1 And e.RowIndex <>
id = Convert.ToInt32(grdNextActioner.Rows(e.RowIndex).Cells("ID").Value.ToString())
End If
End Sub

Textbox to accept only numeric value

Private Sub txtTurnover_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtTurnover.KeyPress
e.Handled = NumbersOnly(e.KeyChar, CType(sender, TextBox))
End Sub
Private Function NumbersOnly(ByVal pstrChar As Char, ByVal oTextBox As TextBox) As Boolean

If (Convert.ToString(pstrChar) = "." And InStr(oTextBox.Text, ".")) Then Return True 'accept only one instance of the decimal point
If Convert.ToString(pstrChar) <> "." And pstrChar <> vbBack Then
Return IIf(IsNumeric(pstrChar), False, True) 'check if numeric is returned
End If
Return False 'for backspace
End Function