vb.net中如何计算鼠标选取 datagridview的多个单元格的和

wangxin5576 2009-08-29 11:07:38
vb.net中如何计算鼠标选取 datagridview的多个单元格的和 就像excel的那样。
...全文
213 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
wangxin5576 2009-08-30
  • 打赏
  • 举报
回复
问题解决了 谢谢各位
以下是代码程序:
' ************************************************************
' 以下为计算所选单元格值的和并传入父窗体
' 需在formload是加入“T1 = Me.ParentForm”
' ************************************************************

Public T1 As mainfrm

Private Sub DataGridView1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles DataGridView1.MouseUp
Dim count1 As Decimal = 0.0
For Each a In DataGridView1.SelectedCells
If IsNumeric(a.value) Then
count1 = count1 + a.Value
Else
count1 = Nothing
T1.ShuZiXianShiZhaungTaiLan.Text = ""
Exit Sub
End If
Next
T1.ShuZiXianShiZhaungTaiLan.Text = count1
End Sub
水哥阿乐 2009-08-30
  • 打赏
  • 举报
回复
只有C#的供参考
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace WindowsApplication35
{
public partial class Form1 : Form
{
DataGridView DGV = new DataGridView();
TextBox TB = new TextBox();

public Form1()
{
InitializeComponent();

DGV.Columns.Add("c1", "c1");
DGV.Columns.Add("c2", "c2");
DGV.Rows.Add(new Object[] { 1, 2 });
DGV.Rows.Add(new Object[] { 3, 4 });
DGV.Parent = this;
DGV.SelectionChanged += new EventHandler(DGV_SelectionChanged);

TB.Location = new Point(0, 200);
TB.Parent = this;
}

void DGV_SelectionChanged(object sender, EventArgs e)
{
int Total = 0;
foreach (DataGridViewCell Cell in DGV.SelectedCells)
Total += (int)Cell.Value;
TB.Text = Total.ToString();
}
}
}
wangxin5576 2009-08-30
  • 打赏
  • 举报
回复
这样的程序又该在什么事件中呢?
wangxin5576 2009-08-29
  • 打赏
  • 举报
回复
没有高手指教吗?
古今多少事 2009-08-29
  • 打赏
  • 举报
回复
类似这样:
For Each a In DataGridView1.SelectedCells
count1=count1+a.Value
Next
wangxin5576 2009-08-29
  • 打赏
  • 举报
回复
能给出代码例子吗?
古今多少事 2009-08-29
  • 打赏
  • 举报
回复
遍历选择的单元格,然后相加……
legentang 2009-08-29
  • 打赏
  • 举报
回复
我怎么就不明白了呢
wangxin5576 2009-08-29
  • 打赏
  • 举报
回复
还是不明白,该在那个事件中发生呢
gba8jing 2009-08-29
  • 打赏
  • 举报
回复
楼主请上 MSDN 上查找 datagridview 类里的方法,我记得有根据鼠标位置获取选取单元格的方法。

16,554

社区成员

发帖
与我相关
我的任务
社区描述
VB技术相关讨论,主要为经典vb,即VB6.0
社区管理员
  • VB.NET
  • 水哥阿乐
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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