如何在基于数据库显示的文本的Gridview中显示图像 [英] How to display image in Gridview Based on Text displayed from Database
问题描述
hi frnds,
如何在Gridview中显示图像基于从数据库显示的文本(来自图像文件夹)
正在使用asp.net C#和Sqlserver 2005.
目前正在使用Gridview。
在解决方案资源管理器中有图像文件夹
我有一个带TableName(TravelDetails)的数据库,字段为
TrainCode,Train_Desc,Sch_Time,From,To
我的记录在Gridview中按照上面的数据库列显示。
T6932 | Manmad Express | 1100 |海德拉巴|孟买
因此,我需要显示ManmadExpress的徽标,而不是Manmad Express文本。这是在图像文件夹中。
因此,我们需要在RowDataBound中编写代码为
protected void GridView1_RowDataBound( object sender,GridViewRowEventArgs e){
if (e.Row.RowType == DataControlRowType.DataRow)
{
}
请帮我解决一下这个问题?
谢谢。
Hello Hyderabadi,
First您需要在同一个Sql表中存储图像的路径。然后
< < span class =code-leadattribute> asp:TemplateField HeaderText = 训练 >
< ItemTemplate >
< asp:Image ID = trainImage 宽度 = 60px 高度 = 50px runat = < span class =code-keyword> server ImageUrl =' <% #Eval( ImageURL)%> ' / >
< / ItemTemplate >
< / asp:TemplateField >
无需使用GridView1_RowDataBound。
试试这样..
< asp:gridview id = gvTraininfo runat = < span class =code-string> server xmlns:asp = < span class =code-string>#unknown >
AutoGenerateColumns = False
GridLines = 无
AllowPaging = true
CssClass = < span class =code-string> mGrid
AlternatingRowStyle-CssClass = alt >
<列>
< asp:boundfield datafield = TrainCode headertext = TrainCode />
< asp:boundfield datafield = Train_Desc headertext = Train_Desc />
< asp:boundfield datafield = Sch_Time headertext = Sch_Time />
< asp:templatefield headertext = 图像 itemstyle-horizontalalign = 中心 >
< itemtemplate> ;
< asp:imagebutton id = ImageButton1 runat = server >
ImageUrl = ' <%#Eval(fldImageUrl)%>' Width = 50px
高度= 50px样式= cursor:pointer
/ >
< / asp:imagebutton > < / itemtemplate >
< / asp:templatefield >
< / 列 >
< / asp:gridview >
并使用SQL Select Statement这样
选择 TrainCode,Train_Desc,Train_Desc,从,到,'c:/ images /' + ' logo.jpg' as fldImageUrl 来自 table
hi frnds,
How to display image in Gridview Based on Text displayed from Database (From Images Folder)
am working on asp.net C# and Sqlserver 2005.
presently am working on Gridview.
In Solution explorer i have images folder
I have a database with TableName(TravelDetails) with fields as
TrainCode,Train_Desc,Sch_Time,From,To
My record will look like this in Gridview as per database columns as above.
T6932 | Manmad Express | 1100 | Hyderabad | Mumbai
So, Instead of Manmad Express text I need to display logo of ManmadExpress. Which is in Images Folder.
So, we need to write code in RowDataBound as
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) {
if(e.Row.RowType == DataControlRowType.DataRow)
{
}
Please can you help me how to do this?
Thanks.
Hello Hyderabadi,
First you need to store the path of the images in the same Sql Table. Then
<asp:TemplateField HeaderText="Train"> <ItemTemplate> <asp:Image ID="trainImage" Width="60px" Height="50px" runat="server" ImageUrl='<%#Eval("ImageURL") %>' /> </ItemTemplate> </asp:TemplateField>
No need of using GridView1_RowDataBound.
try like this..
<asp:gridview id="gvTraininfo" runat="server" xmlns:asp="#unknown"> AutoGenerateColumns="False" GridLines="None" AllowPaging="true" CssClass="mGrid" AlternatingRowStyle-CssClass="alt"> <columns> <asp:boundfield datafield="TrainCode" headertext="TrainCode" /> <asp:boundfield datafield="Train_Desc" headertext="Train_Desc" /> <asp:boundfield datafield="Sch_Time" headertext="Sch_Time" /> <asp:templatefield headertext="Image" itemstyle-horizontalalign="Center"> <itemtemplate> <asp:imagebutton id="ImageButton1" runat="server"> ImageUrl='<%# Eval("fldImageUrl")%>' Width="50px" Height="50px" Style="cursor: pointer" /> </asp:imagebutton></itemtemplate> </asp:templatefield> </columns> </asp:gridview>
and use SQL Select Statement like this
Select TrainCode,Train_Desc,Train_Desc,From,To,'c:/images/'+'logo.jpg' as fldImageUrl from table
这篇关于如何在基于数据库显示的文本的Gridview中显示图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!