datagridview列计算求和问题。。。

如图所示datagridview,我要如下的操作,请帮我写出代码:新建一个列,名称为TotalCheck这个列的值为OrderID列的值乘以2加上EmployerID列的... 如图所示datagridview,我要如下的操作,请帮我写出代码:

新建一个列,名称为TotalCheck
这个列的值为OrderID列的值乘以2加上EmployerID列的值

新建这个列的值是按行一一对应的计算来的。

比如:
OrderID EmployerID TotalCheck
1 2 1*2+3=5
2 3 2*2+3 =7
.....
很多行内容

不知道表述清楚没有,请帮忙写下.net代码
数据来源是EXCEL,不是数据库,所以请不要写给我数据库操作的代码。。。
展开
 我来答
yt214786974
2012-07-03 · TA获得超过1349个赞
知道答主
回答量:39
采纳率:100%
帮助的人:23.3万
展开全部
上班无聊,第一次写Winform的东西,你看看
using System;
using System.Data;
using System.Windows.Forms;
using System.Data.OleDb;
namespace ExcelTest
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
/// <summary>
/// 选择文件按钮点击事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnSelect_Click(object sender, EventArgs e)
{
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.InitialDirectory = "C:\\";//路径为c:\\而不是c:\
openFileDialog.Filter = "所有文件|*.*|Excel2003|*.xls|Excel|*.xlsx";
openFileDialog.RestoreDirectory = true;
openFileDialog.FilterIndex = 1;
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
this.txtFileName.Text = openFileDialog.FileName;//将文件路径写入到TextBox控件中
}
}
/// <summary>
/// 读取文件按钮点击事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnRead_Click(object sender, EventArgs e)
{
string filenameurl = txtFileName.Text.ToString().Trim();
DataTable dt = ReadExcel(filenameurl).Tables[0];
//绑定grid
this.dataGridView1.DataSource = dt;
}
private void btnCount_Click(object sender, EventArgs e)
{
string filenameurl = txtFileName.Text.ToString().Trim();
DataTable dt = ReadExcel(filenameurl).Tables[0];
dt = GetCount(dt);
//绑定grid
this.dataGridView1.DataSource = dt;
}
/// <summary>
/// 读取Excel文件方法
/// </summary>
/// <param name="fileurl"></param>
/// <returns></returns>
public DataSet ReadExcel(string fileurl)
{
DataSet ds = new DataSet();
string ext = fileurl.Substring(fileurl.LastIndexOf('.'));
string strConn = "";
if (ext == ".xls")
{
strConn = "Provider=Microsoft.Jet.OleDb.4.0;" + "data source=" + fileurl + ";Extended Properties='Excel 8.0; HDR=YES; IMEX=1'";
}
else if (ext == ".xlsx")
{
strConn = string.Format(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source='{0}';Extended Properties='Excel 12.0 Xml;HDR=YES'", fileurl);
}
else
{
MessageBox.Show("请选择Excel文件");
return ds;
}
OleDbConnection conn = new OleDbConnection(strConn);
conn.Open();
DataTable dtExcelSchema = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, new object[] { null, null, null, "TABLE" });
//建立连接Excel的数据表
string SheetName = dtExcelSchema.Rows[0]["TABLE_NAME"].ToString();//取出第一个工作表名称
OleDbDataAdapter odda = new OleDbDataAdapter("select * from [" + SheetName + "]", conn);
odda.Fill(ds);
conn.Close();
return ds;
}
/// <summary>
/// 对所有数据进行计算并加入新列TotalCheck给其赋值
/// </summary>
/// <param name="dt"></param>
/// <returns></returns>
public DataTable GetCount(DataTable dt)
{
dt.Columns.Add("TotalCheck");
for (int i = 0; i < dt.Rows.Count; i++)
{
string o = dt.Rows[i]["OrderID"].ToString().Trim();
string e = dt.Rows[i]["EmployerID"].ToString().Trim();
int id = int.Parse(o);
int eId = int.Parse(e);
dt.Rows[i]["TotalCheck"] = id * 2 + eId;
}
return dt;
}
}
}

设计工具代码:
namespace ExcelTest
{
partial class Form1
{
/// <summary>
/// 设计工具所需的变数。
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// 清除任何使用中的资源。
/// </summary>
/// <param name="disposing">如果应该处置 Managed 资源则为 true,否则为 false。</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form 设计工具产生的程式码
/// <summary>
/// 此为设计工具支援所需的方法 - 请勿使用程式码编辑器修改这个方法的内容。
///
/// </summary>
private void InitializeComponent()
{
this.txtFileName = new System.Windows.Forms.TextBox();
this.btnSelect = new System.Windows.Forms.Button();
this.btnRead = new System.Windows.Forms.Button();
this.dataGridView1 = new System.Windows.Forms.DataGridView();
this.btnCount = new System.Windows.Forms.Button();
((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).BeginInit();
this.SuspendLayout();
//
// txtFileName
//
this.txtFileName.Location = new System.Drawing.Point(28, 12);
this.txtFileName.Name = "txtFileName";
this.txtFileName.ReadOnly = true;
this.txtFileName.Size = new System.Drawing.Size(100, 22);
this.txtFileName.TabIndex = 0;
//
// btnSelect
//
this.btnSelect.Location = new System.Drawing.Point(134, 11);
this.btnSelect.Name = "btnSelect";
this.btnSelect.Size = new System.Drawing.Size(75, 23);
this.btnSelect.TabIndex = 1;
this.btnSelect.Text = "选取文件";
this.btnSelect.UseVisualStyleBackColor = true;
this.btnSelect.Click += new System.EventHandler(this.btnSelect_Click);
//
// btnRead
//
this.btnRead.Location = new System.Drawing.Point(224, 11);
this.btnRead.Name = "btnRead";
this.btnRead.Size = new System.Drawing.Size(50, 23);
this.btnRead.TabIndex = 2;
this.btnRead.Text = "读取";
this.btnRead.UseVisualStyleBackColor = true;
this.btnRead.Click += new System.EventHandler(this.btnRead_Click);
//
// dataGridView1
//
this.dataGridView1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
this.dataGridView1.Location = new System.Drawing.Point(28, 68);
this.dataGridView1.Name = "dataGridView1";
this.dataGridView1.RowTemplate.Height = 24;
this.dataGridView1.Size = new System.Drawing.Size(940, 406);
this.dataGridView1.TabIndex = 3;
//
// btnCount
//
this.btnCount.Location = new System.Drawing.Point(291, 11);
this.btnCount.Name = "btnCount";
this.btnCount.Size = new System.Drawing.Size(50, 23);
this.btnCount.TabIndex = 4;
this.btnCount.Text = "计算";
this.btnCount.UseVisualStyleBackColor = true;
this.btnCount.Click += new System.EventHandler(this.btnCount_Click);
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(1028, 544);
this.Controls.Add(this.btnCount);
this.Controls.Add(this.dataGridView1);
this.Controls.Add(this.btnRead);
this.Controls.Add(this.btnSelect);
this.Controls.Add(this.txtFileName);
this.Name = "Form1";
this.Text = "Form1";
((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).EndInit();
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.TextBox txtFileName;
private System.Windows.Forms.Button btnSelect;
private System.Windows.Forms.Button btnRead;
private System.Windows.Forms.DataGridView dataGridView1;
private System.Windows.Forms.Button btnCount;
}
}
本回答被提问者和网友采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
小斯头9067
2012-07-03 · TA获得超过5.9万个赞
知道大有可为答主
回答量:3.6万
采纳率:0%
帮助的人:5547万
展开全部
rows表示行
cells表示列
value表示他们的值
对所有行采用循环来赋值即可
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
国英001
2012-07-02 · 超过16用户采纳过TA的回答
知道答主
回答量:122
采纳率:0%
帮助的人:63.6万
展开全部
你单独去一下EmployerID 计算以后,再赋给
TotalCheck
追问
请给个代码?要这样文字表述。。。我也能行
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
毛毛嫣然
2012-07-03
知道答主
回答量:32
采纳率:0%
帮助的人:13.1万
展开全部
用下标表示各个列的数据,然后写最普通的那种想加的操作就可以了啊
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 1条折叠回答
收起 更多回答(2)
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式