Click here to Skip to main content
16,019,435 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a datagridview which contains two cells. Out of it, second cell contains real value.
I want that when user enters value in first row then on each key press event the value is displayed in textbox, i.e.when user enters 1 in first row then 1 is display in textbox when user enter 2 then 12 is displayed and so on.
When user enters 2 in second row then addition of first row and 2 is displayed in textbox if user enters another 2
In same row then 12(first row value)+22 is displayed(i.e.34) and so on so user can enters multiple rows when user press backslash then textbox value also updated plz help me.

I am sending some lines of code:
C#
private void dgvDescription_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
    {
        //It assigns datagridview cell as textbox and calls keypress event
        if (dgvDescription.CurrentCell.ColumnIndex == 1)
        {
            txtAmount = e.Control as TextBox;
            txtAmount.KeyPress += new KeyPressEventHandler(txtAmount_KeyPress);
        }
    }

    //For validation of amount cell
    private void txtAmount_KeyPress(object sender, KeyPressEventArgs e)
    {
        if (columnindex == 1)
        {
            //To allow 0-9 digits,single . enables backslash key to change amount for user
            if ("0123456789".IndexOf(e.KeyChar) != -1 || ".".IndexOf(e.KeyChar) != -1 && !txtAmount.Text.Contains(".") || "\b".IndexOf(e.KeyChar) != -1)
            {
                e.Handled = false;//or !char.IsDigit(e.KeyChar)&& e.Char!= '\b';

                txttotalamount.Text += Convert.ToString(e.KeyChar.ToString());
            }
            else
            {
                e.Handled = true;
            }
        }
    }

private void txtAmount_KeyUp(object sender, KeyEventArgs e)
    {
        try
        {

            b=txtAmount.Text;


            if (b == "")
            {
                b = "0";
            }
             txttotalamount.Text = Convert.ToString(Convert.ToDouble(c) + Convert.ToDouble(b));

        }
        catch
        {
        }
    }

    private void dgvDescription_CellLeave(object sender, DataGridViewCellEventArgs e)
    {

    }

    private void dgvDescription_CellEndEdit(object sender, DataGridViewCellEventArgs e)
    {
        c=txttotalamount.Text;
    }
Posted
Updated 10-Feb-11 6:29am
v2
Comments
Sandeep Mewara 10-Feb-11 12:29pm    
Use PRE tags to format code part. It makes the question readable.
Sonje Girish 11-Feb-11 12:19pm    
plz help me on my question
YangQy 10-Feb-11 15:11pm    
Pose your html tag as well
Sonje Girish 11-Feb-11 12:20pm    
plz send me answer as early as possible

1 solution

Well, you seem to be on the right track by adding event handlers to the control that is used for editing. Ok, lets walk through it, you may need to change the variable names and function names to work in your project:

1. You have two events that you want to control: KeyPress and TextChanged.
C#
private void dataGridView1_EditingControlShowing ( object sender, DataGridViewEditingControlShowingEventArgs e )
{
    if ( this.dataGridView1.CurrentCell.ColumnIndex == 1 )
    {
        e.Control.TextChanged += new EventHandler ( Control_TextChanged );
        e.Control.KeyPress += new KeyPressEventHandler ( Control_KeyPress );
    }
}

2. Next, put the code in to control the keys used to force numeric value. You only need to address the situations that you do not want to process the keys for.
C#
void Control_KeyPress ( object sender, KeyPressEventArgs e )
{
    if ( "0123456789".IndexOf ( e.KeyChar ) == -1 && ".".IndexOf ( e.KeyChar ) == -1 && "\b".IndexOf ( e.KeyChar ) == -1 )
    {
        e.Handled = true; // prevent the event from being handled by any other code
    }
}

3. Now, put the code in to control what happens when the text changes. The sender here is the TextBox being used to edit the DataGridViewCell.
C#
void Control_TextChanged ( object sender, EventArgs e )
{
	TextBox tbx = ( TextBox )sender;
	List<double> templist = new List<double> ();
	double temp;

	foreach ( DataGridViewRow row in this.dataGridView1.Rows )
	{
		temp = 0;

                // check to see if this is the row being editted. 
                // If so, try to get the value from the TextBox.Text property. 
                // Otherwise, try to get the value from the DataGridViewCell.Value property.
		if ( this.dataGridView1.CurrentCellAddress.Y == this.dataGridView1.Rows.IndexOf ( row ) )
		{
			if ( double.TryParse ( tbx.Text.ToString (), out temp ) )
			{
				templist.Add ( temp );
			}
		}
		else
		{
			if ( double.TryParse ( row.Cells[1].Value.ToString (), out temp ) )
			{
				templist.Add ( temp );
			}
		}
	}

	this.textBox1.Text = templist.Sum ().ToString ();
}


Let me know if you have any questions about this.
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900