DataGridview 某列求和问题.涉及单位换算.对我来说太复杂了,大家帮看下. datagridview 其中某一列如下:数量100克3千克899千克0.87吨500克46千克这只是列的部分数据.请问怎么在最后一行加个 合计多少(千克)!人人有分.谢谢帮助. 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 这个不是应该在SQL中处理吗??SELECT SUM(数量)数量 FROM TB然后绑定DATAGRIDVIE赋给那个列就好了!! 不在sql里面处理.想在datagridview里面处理关键是涉及到的单位换算.这个最难处理.继续上去!! 如果某一列是纯数字int a=0;for (int i=0;i<6;i++){a+=Convert.ToInt32( Datagridview1.Rows[i].Cells["某一列"].Value.Tostring());}合计=a select id ,sum(weight) from table group by id 你在sql里把单位全部转换好,再输出就好办了。不要输出之后再转换, 想过这样做的.但是datagirdview 要显示未转换的数据. 首先一点,数据定义不规范。一个列的数据按照道理应该是一个单位,不应该出现数据单位不同的问题。实在不行只能够做个转换:private void button8_Click(object sender, EventArgs e) { double total = 0; for (int i = 0; i < dataGridView1.Rows.Count; i++) total += ConvertUnit(dataGridView1[1, i].Value.ToString()); MessageBox.Show(total.ToString()); } private float ConvertUnit(string input) { string number=""; float multiplier = 0; if (input.IndexOf("吨")) { number = input.Replace("吨", ""); multiplier = 1000; } else if (input.IndexOf("千克")) { number = input.Replace("千克", ""); multiplier = 1; } else if (input.IndexOf("克")) { number = input.Replace("克", ""); multiplier = 0.001; } else throw new Exception("Unit not found"); try { float returnValue = Convert.ToSingle(number) * multiplier; } catch { throw new Exception("Error Number"); } } private float ConvertUnit(string input) { string number=""; float multiplier = 0; if (input.IndexOf("吨")) { number = input.Replace("吨", ""); multiplier = 1000; } else if (input.IndexOf("千克")) { number = input.Replace("千克", ""); multiplier = 1; } else if (input.IndexOf("克")) { number = input.Replace("克", ""); multiplier = 0.001; } else throw new Exception("Unit not found"); try { float returnValue = Convert.ToSingle(number) * multiplier; return returnValue; } catch { throw new Exception("Error Number"); } } private float ConvertUnit(string input) { string number=""; float multiplier = 0; if (input.IndexOf("吨")!=-1) { number = input.Replace("吨", ""); multiplier = 1000; } else if (input.IndexOf("千克")!=-1) { number = input.Replace("千克", ""); multiplier = 1; } else if (input.IndexOf("克")!=-1) { number = input.Replace("克", ""); multiplier = 0.001f; } else throw new Exception("Unit not found"); try { float returnValue = Convert.ToSingle(number) * multiplier; return returnValue; } catch { throw new Exception("Error Number"); } }前面没有VS2005,直接在NOTPAD上写的 安装部署项目如何增加配置文件? CRC校验里面的几个问题? Drawing 关于c#中spread滚动条设置 用ASP.NET制作登陆页面时 预览登录时提示输入的字符串不正确 请教高手 求serialport 短信收发实例(c#) treeview展开问题 spread 表头排序 c#怎样winform程序读ACCESS文件,路径问题怎么解决????100分,在线等 如何跨线程访问web控件 C# 重载问题 可以用来画图的控件,除了PictureBox外,还有哪些?
SELECT SUM(数量)数量 FROM TB
然后绑定DATAGRIDVIE赋给那个列就好了!!
不在sql里面处理.想在datagridview里面处理
关键是涉及到的单位换算.
这个最难处理.继续上去!!
int a=0;
for (int i=0;i<6;i++)
{
a+=Convert.ToInt32( Datagridview1.Rows[i].Cells["某一列"].Value.Tostring());
}合计=a
private void button8_Click(object sender, EventArgs e)
{
double total = 0;
for (int i = 0; i < dataGridView1.Rows.Count; i++)
total += ConvertUnit(dataGridView1[1, i].Value.ToString());
MessageBox.Show(total.ToString());
}
private float ConvertUnit(string input)
{
string number="";
float multiplier = 0;
if (input.IndexOf("吨"))
{
number = input.Replace("吨", "");
multiplier = 1000;
}
else if (input.IndexOf("千克"))
{
number = input.Replace("千克", "");
multiplier = 1;
}
else if (input.IndexOf("克"))
{
number = input.Replace("克", "");
multiplier = 0.001;
}
else
throw new Exception("Unit not found"); try
{
float returnValue = Convert.ToSingle(number) * multiplier;
}
catch
{
throw new Exception("Error Number");
}
}
private float ConvertUnit(string input)
{
string number="";
float multiplier = 0;
if (input.IndexOf("吨"))
{
number = input.Replace("吨", "");
multiplier = 1000;
}
else if (input.IndexOf("千克"))
{
number = input.Replace("千克", "");
multiplier = 1;
}
else if (input.IndexOf("克"))
{
number = input.Replace("克", "");
multiplier = 0.001;
}
else
throw new Exception("Unit not found"); try
{
float returnValue = Convert.ToSingle(number) * multiplier;
return returnValue;
}
catch
{
throw new Exception("Error Number");
}
}
private float ConvertUnit(string input)
{
string number="";
float multiplier = 0;
if (input.IndexOf("吨")!=-1)
{
number = input.Replace("吨", "");
multiplier = 1000;
}
else if (input.IndexOf("千克")!=-1)
{
number = input.Replace("千克", "");
multiplier = 1;
}
else if (input.IndexOf("克")!=-1)
{
number = input.Replace("克", "");
multiplier = 0.001f;
}
else
throw new Exception("Unit not found"); try
{
float returnValue = Convert.ToSingle(number) * multiplier;
return returnValue;
}
catch
{
throw new Exception("Error Number");
}
}
前面没有VS2005,直接在NOTPAD上写的