在winform中,需要控制datagridview的回车和tal处理
用户要求在datagridveiw中实现,在改变金额时侯,记录下金额变更量
我在金额单元格绑定了一个panel,在panel上放了三个输入框(原金额、变更金额、变更后金额:其中原金额和变更后金额只读)和两个按钮(确定和放弃)。
现在的问题是在panel上textbox中输入金额后,按回车自动跳到下一行同列单元格中了,按tab跳到下一单元络中了。想要达到效果是,按回车或tab跳到panel上的button上。
注:datagridveiw中有4列:项目id、项目名称、金额、变更金额(不可见,保存数据用)
form1.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace WindowsApplication9
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
panel1.Visible = false;
dataGridView1.Controls.Add(panel1);
dataGridView1.Rows.Clear();
dataGridView1.Rows.Add("1", "计算机", "100000", "0");
dataGridView1.Rows.Add("2", "打印机", "40000", "0");
}
private void setpaneltextbox(int X)
{
string str0 = ret_string(dataGridView1.Rows[X].Cells[2].Value.ToString(), "0");
string str1 = ret_string(dataGridView1.Rows[X].Cells[3].Value.ToString(), "0");
double d0, d1, d2;
try
{
d0 = double.Parse(str0);
d1 = double.Parse(str1);
d2 = d0 - d1;
this.textBox3.Text = d0.ToString();
this.textBox1.Text = d2.ToString();
this.textBox2.Text = d1.ToString();
}
catch (Exception ex)
{
MessageBox.Show("金额计算出错");
}
}
private void dataGridView1_CurrentCellChanged(object sender, EventArgs e)
{
if (this.dataGridView1.CurrentCell == null)
{
return;
}
try
{
int X = dataGridView1.CurrentCell.RowIndex;
if (this.dataGridView1.CurrentCell.ColumnIndex == 2) //金额
{
this.panel1.Visible = false;
this.panel1.Left = this.dataGridView1.GetCellDisplayRectangle(this.dataGridView1.CurrentCell.ColumnIndex, this.dataGridView1.CurrentCell.RowIndex, true).Left;
this.panel1.Top = this.dataGridView1.GetCellDisplayRectangle(this.dataGridView1.CurrentCell.ColumnIndex, this.dataGridView1.CurrentCell.RowIndex, true).Top;
setpaneltextbox(X);
this.panel1.Visible = true;
}
else
{
this.panel1.Visible = false;
}
}
catch (Exception)
{
}
}
private void button2_Click(object sender, EventArgs e)
{
this.panel1.Visible = false;
//MessageBox.Show(dataGridView1.CurrentCell.RowIndex.ToString() + "|" + dataGridView1.CurrentCell.ColumnIndex.ToString());
this.dataGridView1.CurrentCell.Value = ret_string(this.textBox3.Text.Trim(), "0");
this.dataGridView1.Rows[dataGridView1.CurrentCell.RowIndex].Cells[3].Value = ret_string(this.textBox2.Text.Trim(), "0");
}
private void textBox2_TextChanged(object sender, EventArgs e)
{
string str0, str1, str2;
str0 = ret_string(this.textBox1.Text.Trim(),"0");
str1 = ret_string(this.textBox2.Text.Trim(), "0");
double d0 = double.Parse(str0) + double.Parse(str1);
this.textBox3.Text = d0.ToString();
}
private string ret_string(string str0,string defaultstr)
{
string ret_str = defaultstr;
if (str0.Trim() != "")
{
ret_str = str0.Trim();
}
return ret_str;
}
private void button3_Click(object sender, EventArgs e)
{
this.panel1.Visible = false;
}
}
}
去掉这行
dataGridView1.Controls.Add(panel1);
更改这两行(利用的是两个坐标)
this.panel1.Left = this.dataGridView1.GetCellDisplayRectangle(this.dataGridView1.CurrentCell.ColumnIndex, this.dataGridView1.CurrentCell.RowIndex, true).Left + this.dataGridView1.Left;
this.panel1.Top = this.dataGridView1.GetCellDisplayRectangle(this.dataGridView1.CurrentCell.ColumnIndex, this.dataGridView1.CurrentCell.RowIndex, true).Top + this.dataGridView1.Top;
再加入这两行代码,使回车和tab自动跳到确定按钮
private void textBox2_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter || e.KeyCode == Keys.Tab)
{
button2.Focus();
}
}