Datagridview单元格附加值 [英] Datagridview cell value addition

查看:80
本文介绍了Datagridview单元格附加值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个datagridview,其中包含两个单元格.在其中,第二个单元格包含实际值.
我希望当用户在第一行中输入值时,在每次按键事件时,该值都显示在文本框中,即,当用户在第一行中输入1时,当用户输入2时则在文本框中显示1,然后显示12,依此类推. br/> 当用户在第二行中输入2时,如果用户输入另外2个,则文本框中将显示第一行和2的加法
在同一行中会显示12(第一行值)+22(即34),依此类推,这样当用户按下反斜杠时,用户可以输入多行,然后文本框值也会更新,请帮助.

我正在发送一些代码行:

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:

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;
    }

推荐答案

好吧,通过将事件处理程序添加到用于编辑的控件中,您似乎处在正确的轨道上.好的,让我们逐步进行一下,您可能需要更改变量名称和函数名称才能在您的项目中工作:

1.您要控制两个事件:KeyPress和TextChanged.
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.
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.接下来,放入代码以控制用于强制输入数值的键.您只需要解决不需要处理密钥的情况.


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.

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.现在,放入代码以控制文本更改时发生的情况.发件人是用于编辑DataGridViewCell的TextBox.


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.

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.


这篇关于Datagridview单元格附加值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆