关于Windows应用程序的datagridview的问题 [英] Question Regarding datagridview of windows Application
问题描述
如何计算datagridview中的行数?
假设有两列n这样的值,
产品名称|价格
笔| 10.00
纸| 12.00
笔| 10.00
ColorBrush | 5.00
我必须显示产品总数..像,
pen(2)+ paper + ColorBrush = 3个项目.这3个项目应在文本框中显示为"3"..
how to count no of item of rows in a datagridview?
suppose have two columns n such values,
ProductName| Price
Pen | 10.00
Paper | 12.00
Pen | 10.00
ColorBrush | 5.00
I have to show the total no of product..like as,
pen(2)+paper+ColorBrush= 3 items.This 3 items should display in a textbox as ''3''..
推荐答案
您好,
使用您的数据源查找解决方案.试试这个:
Hi,
Use your datasource to find the solution. Try this:
DataTable tblTest= new DataTable();
tblTest.Columns.Add("ProductName", typeof(string));
tblTest.Columns.Add("Price ", typeof(double));
//insert your data
tblTest.Rows.Add("Pen", 10.00);
tblTest.Rows.Add("Paper", 12.00);
tblTest.Rows.Add("Pen", 10.00);
tblTest.Rows.Add("ColorBrush", 5.00);
// query with LINQ
var query = from row in tblTest.AsEnumerable()
group row by row.Field<string>("ProductName") into Products
orderby Products.Key
select new
{
ProductName = Products.Key,
ProductCount = Products.Count()
};
此后,您可以打印记录
Hereafter you can print your records
foreach (var Prod in query)
{
Console.WriteLine("{0}\t{1}", Prod.ProductName, Prod.ProductCount);
}
您的输出将类似于:
Your output will be like:
Pen 2
Paper 1
ColorBrush 1
--Amit
--Amit
List<string> rows = new List<string>();
foreach (DataGridViewRow row in this.GridView.Rows)
{
if (rows.Contains(row.Cells["ProductName"].Value.ToString()))
continue;
rows.Add(row.Cells["ProductName"].Value.ToString())
// if you need just count so rows.Count is your answer
}
这篇关于关于Windows应用程序的datagridview的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!