开发者

How to place a different value within a GridView, if the source is a DataTable?

开发者 https://www.devze.com 2023-03-26 09:17 出处:网络
First of all, I\'ve got a DataTable which stores data in a number of fields. One of these fields stores a value, which can either be \'1\', \'0\' or \'-1\'.

First of all, I've got a DataTable which stores data in a number of fields. One of these fields stores a value, which can either be '1', '0' or '-1'.

What I'm trying to accomplish is to fill a GridView with all the data contained in this DataTable, except for the previously mentioned field. Instead of this field, I would like to display a column which shows a different icon depending on the value (either 0, 1 or -1).

In other words, if a row contains a value of '0' in this field within the DataTable, I 开发者_StackOverflowwould like a red circle icon to appear in the equivalent cell within the GridView.

Could anyone kindly explain how this may be accomplished? Below is some code which may help to explain the situation further.

The GridView is filled with the data contained in the DataTable.

source = new DataSource();
TableGridView.DataSource = source.FillTable();
TableGridView.DataBind();

What should be done after this?

EDIT: Solution added as a comment below


Well there are different approaches:

1) you could add a field to your DataTable for the image and fill it with the url of the image that corresponds to the value. Add an image to the GridView and set it's ImageUrl to that field.

2) Add a template field to your GridView and add the 3 images. In the Visible property of the each image you set something like <% (int)Eval("MyNumericValue") == x %> (x being 0, 1 or -1) . That way only 1 image is shown, the others hidden (not rendered even).

from the hip:

<TemplateField>
    <ItemTemplate>
         <asp:image runat="server" imageurl="~/images/0.png" Visible='<%# (int)Eval("MyNumericValue") == 0 %>' />                
         <asp:image runat="server" imageurl="~/images/1.png" Visible='<%# (int)Eval("MyNumericValue") == 1 %>' />
         <asp:image runat="server" imageurl="~/images/-1.png" Visible='<%# (int)Eval("MyNumericValue") == -1 %>' />
    </ItemTemplate>
</TemplateField>


Solved: I created the TemplateField in ASP.NET code, and the other fields programmatically in the .cs class. Next, I added a chunk of code which switches round the order of the columns. Hope someone finds this helpful.

0

精彩评论

暂无评论...
验证码 换一张
取 消