dataGridView编辑

guyanjun121 2010-09-10 11:26:03
我添加了一个dataGridView1,有三列,第一第二列运行时自己输入,第三行能计算出前两行的乘积。第一次用dataGridView,不太会有,请高手指点。
...全文
135 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
uvvvw 2010-09-10
  • 打赏
  • 举报
回复
学习了
Dream83619 2010-09-10
  • 打赏
  • 举报
回复
public int Result { get { return a * b; } }
改为
public int C { get { return a * b; } }

写错了, 呵呵
Dream83619 2010-09-10
  • 打赏
  • 举报
回复
使用BidingList
添加一个DatagridView, 添加三个列,设置datapropertname分别为A、B、C
UI用绑定的数据来控制,岂不是很漂亮?


public partial class Form1 : Form
{
public Form1() {
InitializeComponent();
dataGridView1.DataSource = new BindingList<Model>();
}

private class Model : INotifyPropertyChanged
{
private int a = 0;
private int b = 0;

public int A { get { return a; } set { a = value; NotifyProerptyChanged("A"); NotifyProerptyChanged("C"); } }
public int B { get { return b; } set { b = value; NotifyProerptyChanged("B"); NotifyProerptyChanged("C"); } }
public int Result { get { return a * b; } }

private void NotifyProerptyChanged(string strPropName) {
if (PropertyChanged != null) {
PropertyChanged(this, new PropertyChangedEventArgs(strPropName));
}
}

#region INotifyPropertyChanged 成员

public event PropertyChangedEventHandler PropertyChanged;

#endregion
}
}
qiuxin425 2010-09-10
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 jointan 的回复:]
C# code
private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
if (e.ColumnIndex == 0 || e.ColumnIndex == 1)
{
……
[/Quote]

正解,最后一句this.dataGridView1[2, e.RowIndex].Value = result.ToString();
jointan 2010-09-10
  • 打赏
  • 举报
回复
        private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
if (e.ColumnIndex == 0 || e.ColumnIndex == 1)
{
int x1 = 0, x2 = 0;
int.TryParse(this.dataGridView1[0, e.RowIndex].FormattedValue.ToString(), out x1);
int.TryParse(this.dataGridView1[1, e.RowIndex].FormattedValue.ToString(), out x2);

int result = x1 * x2;
this.dataGridView1[2, e.RowIndex].Value = result;
}
}
qiuxin425 2010-09-10
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 guyanjun121 的回复:]
我要怎么把乘积放入第三列呢
[/Quote]

你先判断你修改的是哪个行列,把对应的另一个值取出来,当然这里的值是string,你要转成double或者你想要的类型,然后做完乘积sum

dataGridView1[列号,行号].Value = sum.ToString();
就可以了

guyanjun121 2010-09-10
  • 打赏
  • 举报
回复
我要怎么把乘积放入第三列呢
zyxfcb1520 2010-09-10
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 qqrto 的回复:]
试一下响应这个事件CellEndEdit,在这里面处理应该是可以的
[/Quote]
应该这个
qiuxin425 2010-09-10
  • 打赏
  • 举报
回复
设置dataGridView1时将第三列设置为只读,这样第三列就不会被修改了
然后再CellEndEdit消息里处理前两列的输入,大概思路
private void dgvFreq_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
int nRow = dgvFreq.CurrentCell.RowIndex; //当前行
int nCol = dgvFreq.CurrentCell.ColumnIndex; //当前列
}
//根据行和列的索引做你想做的事
qqrto 2010-09-10
  • 打赏
  • 举报
回复
试一下响应这个事件CellEndEdit,在这里面处理应该是可以的
llf94632525 2010-09-10
  • 打赏
  • 举报
回复
记得好像有个cell的失去输入焦点事件,在输入完成后敲回车、tab时候触发,你去找找看,具体什么名字我忘了。
在这个事件里添加计算方法。

111,129

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧