Archive

Archive for the ‘Win App’ Category

check the same child window form is already opened

May 27, 2011 Leave a comment

int FlagMultiple=0;
                foreach (Form Objform in Application.OpenForms)
                {
                    if (Objform.Text == "FormText")
                    {
                        Objform.BringToFront();
                        FlagMultiple=1;
                    }
                }
                if (FlagMultiple!=1)
                {
                    Form1 objForm = new Form1();
                    objForm.MdiParent = this;
                    objForm.StartPosition = FormStartPosition.CenterScreen;
                    objForm.Show();
                }
Advertisements
Categories: Win App

validate Datagridview column

May 16, 2011 Leave a comment

If you want to validate the column in the datagridview then you can choose “CellValidating”

Sample Codeing is as below

private void dataGridView1_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
{
DataGridViewTextBoxCell cell = dataGridView1[e.ColumnIndex, e.RowIndex] as DataGridViewTextBoxCell;

if (cell != null)
{
char[] chars = e.FormattedValue.ToString().ToCharArray();
foreach (char c in chars)
{
if (char.IsDigit(c) == false)
{
MessageBox.Show(“You have to enter digits only”);

e.Cancel = true;
break;
}
}
}
}

Suppose if you want to validate after pressing enter, then you can use as below
protected override bool ProcessDialogKey(KeyskeyData)
{
if (keyData == Keys.Enter)
{
DataGridViewCell currentCell = CurrentCell;
EndEdit();
CurrentCell = null;
CurrentCell = currentCell;
return true;
}
return base.ProcessDialogKey(keyData);
}
else you can also use EditControlShowing event

void dataGridView1_EditingControlShowing(object sender,

DataGridViewEditingControlShowingEventArgs e)

{

if (this.dataGridView1.CurrentCell.ColumnIndex == 0)

{

if (e.Control is TextBox)

{

TextBox tb = e.Control as TextBox;

tb.KeyPress -= new KeyPressEventHandler(tb_KeyPress);

tb.KeyPress += new KeyPressEventHandler(tb_KeyPress);

}

}

}

void tb_KeyPress(object sender, KeyPressEventArgs e)

{

if (!(char.IsDigit(e.KeyChar)))

{

Keys key = (Keys)e.KeyChar;

if (!(key == Keys.Back || key == Keys.Delete))

{

e.Handled = true;

}

}

}

}

Categories: Win App

Format Data in the Windows Forms DataGridView Control

May 16, 2011 Leave a comment

Useful coding for formation the cells in datagridview in MSDN site

To enable wordwrap in text-based cells

this.dataGridView1.DefaultCellStyle.WrapMode = DataGridViewTriState.True;

To specify the text alignment of DataGridView cells

this.dataGridView1.Columns["CustomerName"].DefaultCellStyle.Alignment 
= DataGridViewContentAlignment.MiddleRight;

To customize the display of null database values

this.dataGridView1.DefaultCellStyle.NullValue = "no entry";

To format currency and date values

this.dataGridView1.Columns["UnitPrice"].DefaultCellStyle.Format = "c";
this.dataGridView1.Columns["ShipDate"].DefaultCellStyle.Format = "d";

And also as below

dataGridView1.Columns[0].DefaultCellStyle.Format = “dd’/’MM’/’yyyy”;

//Get 13/12/2007

dataGridView1.Columns[1].DefaultCellStyle.Format = “dd’/’MM’/’yyyy hh:mm:ss tt”;

//Get 13/12/2007 5:00:11 PM

Categories: Win App
%d bloggers like this: