将计算列添加到Datagrid [英] Add a calulated column to a Datagrid

查看:59
本文介绍了将计算列添加到Datagrid的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在C#中有一个基于SQL查询的绑定数据网格。

结果显示在Sharepoint 2003 Webpart中。

我想添加一个新的列基于datagrid中两个

列的结果。

我不可能在SQL查询中执行此操作

我正在使用的数据提供程序不支持列别名。


例如将这些列的结果放入一个新列。


newColumn = new BoundColumn();

newColumn.DataField =" pt_desc1" ;;

newColumn.HeaderText =" Description";

OrdersGrid.Columns.Add(newColumn);


newColumn = new BoundColumn();

newColumn.DataField =" pt_desc2";

newColumn.HeaderText =" Description" ;;

OrdersGrid.Columns.Add(newColumn) ;

-

GML

I have a bound datagrid in C# based on an an SQL query.
The results are displayed in a Sharepoint 2003 Webpart.
I would like to add a new column based on the results of two
columns in the datagrid.
It is not possible for me to do this within the SQL query as
the data provider I am using does not support column alias''s.

e.g. Put the results of these columns into a new column.

newColumn = new BoundColumn();
newColumn.DataField = "pt_desc1";
newColumn.HeaderText = "Description";
OrdersGrid.Columns.Add(newColumn);

newColumn = new BoundColumn();
newColumn.DataField = "pt_desc2";
newColumn.HeaderText = "Description";
OrdersGrid.Columns.Add(newColumn);
--
GML

推荐答案

您可以使用DataColumn.Expression属性来将计算的

属性添加到DataGrid。


例如。你可以使用

DataColumn resultColumn = new DataColumn();

resultColumn.Expression =" pt_dec1 + pt_desc2" ;;


Sijin Joseph
http://www.indiangeek.net
< a rel =nofollowhref =http://weblogs.asp.net/sjosephtarget =_ blank> http://weblogs.asp.net/sjoseph


GML写道:
You can use the DataColumn.Expression property to add a calculated
property to the DataGrid.

For eg. you can use
DataColumn resultColumn = new DataColumn();
resultColumn.Expression = "pt_dec1+pt_desc2";

Sijin Joseph
http://www.indiangeek.net
http://weblogs.asp.net/sjoseph

GML wrote:
我在C#中有一个基于SQL查询的绑定数据网格。
结果显示在Sharepoint 2003 Webpart中。
我想要根据数据网格中两个
列的结果添加一个新列。
我不可能在SQL查询中执行此操作,因为我使用的数据提供程序没有支持列别名'。

例如将这些列的结果放入一个新列。

newColumn = new BoundColumn();
newColumn.DataField =" pt_desc1";
newColumn.HeaderText =" Description" ;;
OrdersGrid.Columns.Add(newColumn);

newColumn = new BoundColumn();
newColumn.DataField =" pt_desc2";
newColumn.HeaderText ="描述" ;;
OrdersGrid.Columns.Add(newColumn);
I have a bound datagrid in C# based on an an SQL query.
The results are displayed in a Sharepoint 2003 Webpart.
I would like to add a new column based on the results of two
columns in the datagrid.
It is not possible for me to do this within the SQL query as
the data provider I am using does not support column alias''s.

e.g. Put the results of these columns into a new column.

newColumn = new BoundColumn();
newColumn.DataField = "pt_desc1";
newColumn.HeaderText = "Description";
OrdersGrid.Columns.Add(newColumn);

newColumn = new BoundColumn();
newColumn.DataField = "pt_desc2";
newColumn.HeaderText = "Description";
OrdersGrid.Columns.Add(newColumn);



此外,还有一个很好的话题在.net帮助中的DataColumn.Expression

文件。


-

Miha Markic [MVP C#] - RightHand .NET咨询与开发

miha at rthand com
www.rthand.com


" Sijin Joseph" < SI *************** @ hotmail.com>在消息中写道

news:un ************** @ TK2MSFTNGP10.phx.gbl ...
In addition, there is a good topic on DataColumn.Expression in .net help
files.

--
Miha Markic [MVP C#] - RightHand .NET consulting & development
miha at rthand com
www.rthand.com

"Sijin Joseph" <si***************@hotmail.com> wrote in message
news:un**************@TK2MSFTNGP10.phx.gbl...
您可以使用DataColumn .Expression属性将计算的
属性添加到DataGrid。

例如。你可以使用
DataColumn resultColumn = new DataColumn();
resultColumn.Expression =" pt_dec1 + pt_desc2";

Sijin Joseph
http://www.indiangeek.net
http://weblogs.asp.net/sjoseph

GML写道:
You can use the DataColumn.Expression property to add a calculated
property to the DataGrid.

For eg. you can use
DataColumn resultColumn = new DataColumn();
resultColumn.Expression = "pt_dec1+pt_desc2";

Sijin Joseph
http://www.indiangeek.net
http://weblogs.asp.net/sjoseph

GML wrote:
I基于SQL查询在C#中绑定数据网格。
结果显示在Sharepoint 2003 Webpart中。
我想根据两个
列的结果添加一个新列在数据网格中。
我不可能在SQL查询中执行此操作,因为我使用的数据提供程序不支持列别名。

例如将这些列的结果放入一个新列。

newColumn = new BoundColumn();
newColumn.DataField =" pt_desc1";
newColumn.HeaderText =" Description" ;;
OrdersGrid.Columns.Add(newColumn);

newColumn = new BoundColumn();
newColumn.DataField =" pt_desc2";
newColumn.HeaderText =" Description";
OrdersGrid.Columns.Add(newColumn);
I have a bound datagrid in C# based on an an SQL query.
The results are displayed in a Sharepoint 2003 Webpart.
I would like to add a new column based on the results of two
columns in the datagrid.
It is not possible for me to do this within the SQL query as
the data provider I am using does not support column alias''s.

e.g. Put the results of these columns into a new column.

newColumn = new BoundColumn();
newColumn.DataField = "pt_desc1";
newColumn.HeaderText = "Description";
OrdersGrid.Columns.Add(newColumn);

newColumn = new BoundColumn();
newColumn.DataField = "pt_desc2";
newColumn.HeaderText = "Description";
OrdersGrid.Columns.Add(newColumn);






你应该添加列到数据源,在你的情况下是DataTable。


-

Miha Markic [MVP C#] - RightHand .NET咨询&开发

miha at rthand com
www.rthand.com


" GML" < GM*@discussions.microsoft.com>在消息中写道

news:50 ********************************** @ microsof t.com ...
You should add the column to datasource, in your case to DataTable.

--
Miha Markic [MVP C#] - RightHand .NET consulting & development
miha at rthand com
www.rthand.com

"GML" <GM*@discussions.microsoft.com> wrote in message
news:50**********************************@microsof t.com...
嗨Sijin,

谢谢你,但我如何将新的colum添加到我的网格中。

我得到了如果我使用错误: -

DataColumn resultColumn = new DataColumn();
resultColumn.Expression =" pt_dec1 + pt_desc2" ;;
resultColumn.ColumnName =" Part Description" ;;
OrdersGrid.Columns.Add(resultColumn);

" Sijin Joseph"写道:
Hi Sijin,

Thanks for that but how do i add the new colum to my grid.

I get an error if i use :-
DataColumn resultColumn = new DataColumn();
resultColumn.Expression = "pt_dec1+pt_desc2";
resultColumn.ColumnName = "Part Description";
OrdersGrid.Columns.Add(resultColumn);
"Sijin Joseph" wrote:
您可以使用DataColumn.Expression属性将计算的
属性添加到DataGrid。

例如。你可以使用
DataColumn resultColumn = new DataColumn();
resultColumn.Expression =" pt_dec1 + pt_desc2";

Sijin Joseph
http://www.indiangeek.net
http://weblogs.asp.net/sjoseph

GML写道:
You can use the DataColumn.Expression property to add a calculated
property to the DataGrid.

For eg. you can use
DataColumn resultColumn = new DataColumn();
resultColumn.Expression = "pt_dec1+pt_desc2";

Sijin Joseph
http://www.indiangeek.net
http://weblogs.asp.net/sjoseph

GML wrote:
> ;我在C#中有一个基于SQL查询的绑定数据网格。
>结果显示在Sharepoint 2003 Webpart中。
>我想根据两个结果添加一个新列
>数据网格中的列。
>我无法在SQL查询中执行此操作,因为
>我正在使用的数据提供程序不支持列别名'
>
>例如将这些列的结果放入新列。
>
> newColumn = new BoundColumn();
> newColumn.DataField =" pt_desc1";
> newColumn.HeaderText =" Description";
> OrdersGrid.Columns.Add(newColumn);
>
> newColumn = new BoundColumn();
> newColumn.DataField =" pt_desc2";
> newColumn.HeaderText =" Description";
> OrdersGrid.Columns.Add(newColumn);
>
>
> I have a bound datagrid in C# based on an an SQL query.
> The results are displayed in a Sharepoint 2003 Webpart.
> I would like to add a new column based on the results of two
> columns in the datagrid.
> It is not possible for me to do this within the SQL query as
> the data provider I am using does not support column alias''s.
>
> e.g. Put the results of these columns into a new column.
>
> newColumn = new BoundColumn();
> newColumn.DataField = "pt_desc1";
> newColumn.HeaderText = "Description";
> OrdersGrid.Columns.Add(newColumn);
>
> newColumn = new BoundColumn();
> newColumn.DataField = "pt_desc2";
> newColumn.HeaderText = "Description";
> OrdersGrid.Columns.Add(newColumn);
>
>



这篇关于将计算列添加到Datagrid的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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