如何设置取决于其他列的GridView列的文字这是绑定? [英] How to set a gridView column's text depending on other columns which are binded?

查看:80
本文介绍了如何设置取决于其他列的GridView列的文字这是绑定?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在ASP一个GridView,我用显示产品中的购物车。

I have a GridView in ASP which I use for displaying products in a shopping cart.

我设置了​​ShopDataSet和列名称,价格和数量自动填充的GridView的来源。

I set the source of GridView the ShopDataSet and the columns Name, Price and Quantity are filled automatically.

我想实现的是一个新的专栏,由我添加的,它显示每行的成本,即成本=价格*数量;

What I want to achieve is a new column, added by me, which displays the cost of each row i.e. Cost = price * quantity;

我怎么能做到这一点编程,无需数据库上执行一个新的查询?

How can I do this programatically, without executing a new query on the database?

我不得不说,在价格和数量我设置的格式如$ {0}价格和{0}件( S)的数量。

I have to say that on Price and Quantity I set format like "${0}" for Price and "{0} piece(s)" for Quantity.

推荐答案

获取一个SqlDataReader的结果,并填充SqlDataReader的结果的数据表。

You should populate your datasource programmatically.

Get the results in a SqlDataReader and populate a DataTable with the results of the SqlDataReader.

现在创建匹配要包括在你的案件的成本输出列的新数据表。现在,通过第一DataTable的每一行进行迭代,并添加行与成本计算的新数据表。

Now create a new DataTable with columns that match the output that you want including Cost in your case. Now iterate through each row of the 1st DataTable and add rows to the new DataTable with the cost calculation.

例如,

DataTable oldTable = new DataTable();
oldTable .Load(rdr); // where rdr is your SqlDataReader

现在创建新表

DataTable newTable = new DataTable();
DataColumn noteID = new DataColumn("Cost", typeof(string));
newTable.Columns.Add(noteID);
//Add other columns
foreach (DataRow row in oldTable .Rows)
{
   DataRow newRow = newTable.NewRow();
   newRow["Cost"] = //your calculation
   ...
   ...
   newTable.Add(newRow);
}

这篇关于如何设置取决于其他列的GridView列的文字这是绑定?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆