Kishan
Kishan

Reputation: 1190

Add decimal point & set cursor position to DataGridView cell text on KeyPress event

The following code is used for adding decimal point after 5 digits in DataGridView cell and to set cursor positions to last, but this code work only for the first DataGridView cell.
Thanks in advance.

Result Image

Grid EditingControlShowing event code:

private void grdCharges_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
    DataGridView grd = sender as DataGridView;
    e.Control.KeyPress -= new KeyPressEventHandler(grdCharges_row0_Column2_KeyPress);
    e.Control.KeyPress -= new KeyPressEventHandler(grdCharges_row1_Column2_KeyPress);
    e.Control.KeyPress -= new KeyPressEventHandler(grdCharges_Column3_KeyPress);
    if (grdCharges.CurrentRow.Index == 0)
    {
        if (grdCharges.CurrentCell.ColumnIndex == 2) //Desired Column
        {
            TextBox r0c2 = e.Control as TextBox;
            if (r0c2 != null)
            {
                r0c2.KeyPress += new KeyPressEventHandler(grdCharges_row0_Column2_KeyPress);
            }
        }
    }
    if (grdCharges.CurrentRow.Index == 1)
    {
        if (grdCharges.CurrentCell.ColumnIndex == 2) //Desired Column
        {
            TextBox r1c2 = e.Control as TextBox;
            if (r1c2 != null)
            {
                r1c2.KeyPress += new KeyPressEventHandler(grdCharges_row1_Column2_KeyPress);
            }
        }
    }
    if (grdCharges.CurrentCell.ColumnIndex == 3) //Desired Column
    {
        TextBox tb3 = e.Control as TextBox;
        if (tb3 != null)
        {
            tb3.KeyPress += new KeyPressEventHandler(grdCharges_Column3_KeyPress);
        }
    }
}

Add decimal point to gridview data value:

private void grdCharges_row0_Column2_KeyPress(object sender, KeyPressEventArgs e)
{

    VaildationGrdCharges(sender, e, grdCharges.CurrentRow.Index, grdCharges.CurrentCell.ColumnIndex);
    TextBox textBox = (TextBox)sender;
    if (textBox.Text.Length.ToString() == "5")
    {
        grdCharges.Rows[grdCharges.CurrentRow.Index].Cells[grdCharges.CurrentCell.ColumnIndex].Value = string.Concat(textBox.Text, ".");
        if (grdCharges.CurrentCell.EditType == typeof(DataGridViewTextBoxEditingControl))
        {
            ((TextBox)this.grdCharges.EditingControl).SelectionStart = textBox.Text.Length + 2; // add some logic if length is 0
           // ((TextBox)this.grdCharges.EditingControl).SelectionLength = 0;
        }
    }
}

private void grdCharges_row1_Column2_KeyPress(object sender, KeyPressEventArgs e)
{
    VaildationGrdCharges(sender, e, grdCharges.CurrentRow.Index, grdCharges.CurrentCell.ColumnIndex);                
    TextBox textBox = (TextBox)sender;
    if (textBox.Text.Length.ToString() == "5")
    {
        grdCharges.Rows[grdCharges.CurrentRow.Index].Cells[grdCharges.CurrentCell.ColumnIndex].Value = string.Concat(textBox.Text, ".");
        if (grdCharges.CurrentCell.EditType == typeof(DataGridViewTextBoxEditingControl))
        {
            ((TextBox)this.grdCharges.EditingControl).SelectionStart = textBox.Text.Length + 2; // add some logic if length is 0
            //((TextBox)this.grdCharges.EditingControl).SelectionLength = 0;
        }
    }
}

Upvotes: 0

Views: 587

Answers (1)

Jeremy Thompson
Jeremy Thompson

Reputation: 65712

this code only works for the first grid-view cell?

I just need a clue or answer or suggesstion.

Its because you remove all Event Handlers:

e.Control.KeyPress -= new KeyPressEventHandler(grdCharges_row0_Column2_KeyPress);
e.Control.KeyPress -= new KeyPressEventHandler(grdCharges_row1_Column2_KeyPress);
e.Control.KeyPress -= new KeyPressEventHandler(grdCharges_Column3_KeyPress);

But then you only add one back depending on if statement conditions checking the CurrentCell Row and Columnm indexes, eg:

r0c2.KeyPress += new KeyPressEventHandler(grdCharges_row0_Column2_KeyPress);

You can see the events lost using Debug.WriteLine, an off the top of my head example:

System.Diagnostics.Debug.WriteLine("SomeEvent has " + SomeEvent.GetInvocationList().Length + " event handlers");

So that's the problem to fix it take Hans advice and use the CellValidating event to set the cursor.

Upvotes: 0

Related Questions