vb.net 根据文本将图像插入 Gridview 单元格
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22227468/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Insert an image into a Gridview cell depending on text
提问by Silentbob
I use VS 2013 (VB) and asp.net.
我使用 VS 2013 (VB) 和 asp.net。
I have a gridview created programatically using values from an MS SQL 2012 table.
我有一个使用 MS SQL 2012 表中的值以编程方式创建的 gridview。
The asp.net for the gridview is as follows
gridview的asp.net如下
<asp:GridView ID="availableRuns" runat="server" HorizontalAlign="Center" CssClass="Asset_table" HeaderStyle-CssClass="Asset_table_header" ></asp:GridView>
The VB code for the gridview is
gridview的VB代码是
Dim RTORun As New RTO
Dim RTORuns As New List(Of RTO)
RTORuns = RTORun.getRTO
availableRuns.DataSource = RTORuns
availableRuns.DataBind()
I have another sub that I want to use to convert the values in the cells to images which is Currently used to change the text value.
我有另一个子程序,我想用它来将单元格中的值转换为当前用于更改文本值的图像。
Protected Sub changeText()
Dim row As GridViewRow
For Each row In availableRuns.Rows
Dim i As Integer
For i = 0 To row.Cells.Count - 1
If row.Cells(i).Text = "1" Then
row.Cells(i).Text = "Yes"
ElseIf row.Cells(i).Text = "0" Then
row.Cells(i).Text = ""
End If
Next
Next
End Sub
This produces the following gridview
这会产生以下网格视图


I want to be able to insert an image in replacement for 1 (or Yes as it is in the image) and leave it blank if 0. I have no idea how to do this can anyone offer some pointers as googling hasnt helped as they all reference an image control in the asp.net page which I dont have.
我希望能够插入一个图像来替换 1(或者是,因为它在图像中),如果为 0,则将其留空。我不知道如何做到这一点,任何人都可以提供一些指示,因为谷歌搜索没有帮助,因为它们都没有在我没有的 asp.net 页面中引用图像控件。
回答by Yuriy Galanter
You can do it in 2 ways - either direct HTML assignment:
您可以通过两种方式进行 - 直接 HTML 分配:
If row.Cells(i).Text = "1" Then
row.Cells(i).Text = "<img src='MyYesImg.jpg' />"
where "MyYesImg.jpg" is an image that exists in the same folder as your ASPX page. Also you have to make sure that grid columns have their HtmlEncodeproperty set to Falsefor image to propertly display.
其中“MyYesImg.jpg”是与您的 ASPX 页面位于同一文件夹中的图像。此外,您必须确保网格列的HtmlEncode属性设置False为图像才能正确显示。
Or, by programmaticaly adding Image Control (which you dohave - it's a standard ASP.NET control, part of the framework)
或者,通过以编程方式添加图像控件(您确实拥有 - 它是标准的 ASP.NET 控件,框架的一部分)
Dim img As Image
'...loop code
If row.Cells(i).Text = "1" Then
row.Cells(i).Text = ""
img = New Image
img.ImageUrl = "MyYesImg.jpg"
row.Cells(i).Controls.Add(img)
Again "MyYesImg.jpg" is the image file that exists in the same folder as your ASPX page
再次“MyYesImg.jpg”是与您的 ASPX 页面存在于同一文件夹中的图像文件

