首页 新闻 会员 周边

datagridview中回车和tab控制问题

0
悬赏园豆:100 [已解决问题] 解决于 2011-02-16 12:21

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

 

 

 

 

问题补充: 怎么没有同行给的建议 我写的一个测试代码,大家可以帮忙完成。 这里不能发附件,我邮箱wxmwork@126.com,来个邮件,我发给大家,帮帮忙,谢谢
老萝卜的主页 老萝卜 | 初学一级 | 园豆:100
提问于:2011-02-13 23:26
< >
分享
最佳答案
0

去掉这行
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();
}
}

 

收获园豆:100
缤纷夏日 | 菜鸟二级 |园豆:425 | 2011-02-16 11:13
谢谢,是我走入误区了 不应该 将控件与datagridview绑定
老萝卜 | 园豆:100 (初学一级) | 2011-02-16 12:22
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册