06
2012
07

asp.net中DataGridView控件中,某个列的单元格输入值改变时触发命令的方法

  public frmDepositEdit()
        {
            InitializeComponent();
            dgvCardType.AutoGenerateColumns
= false;
            dgvCardType.EditingControlShowing
+= new DataGridViewEditingControlShowingEventHandler(dgvCardType_EditingControlShowing);
        }

       
void dgvCardType_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
        {
            e.Control.KeyPress
+= new KeyPressEventHandler(Control_KeyPress);
        }

       
//求实收金额。
        decimal addMoney = 0;
       
void Control_KeyPress(object sender, KeyPressEventArgs e)
        {

      //要处理的事件信息。

           
decimal intNumberBoxes = 0;
           
decimal someAmount = 0;
           
if (char.IsNumber(e.KeyChar))
            {
               
int column = dgvCardType.CurrentCellAddress.X;
               
int row = dgvCardType.CurrentCellAddress.Y;

               
if (column == 2)
                {
                   
string cellValue = Char.ToString(e.KeyChar);
                   
string test = dgvCardType[column, row].EditedFormattedValue.ToString();
                    test
= test + cellValue;
                    intNumberBoxes
= Convert.ToDecimal(test);
                   
if (intNumberBoxes > 999)
                    {
                        e.Handled
= true;
                    }
                    someAmount
= decimal.Parse(dgvCardType.Rows[dgvCardType.CurrentCellAddress.Y].Cells[1].Value.ToString());
                    txt_现金金额.Text
= (addMoney + intNumberBoxes * someAmount).ToString();
                }
            }
        }

« 上一篇下一篇 »

评论列表:

1.injectionmould  2012/7/7 17:04:59 回复该留言
恩,有点深奥,得慢慢消化
2.风机盘管  2012/7/14 17:25:26 回复该留言
有老是这样

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。