如何在gridview中添加图像? [英] How to add image inside the gridview?
问题描述
嗨!
我有一个包含清单的网格视图。如果核对表已经有一个里程碑或附加了许多里程碑,它的一列会显示一个检查图标的图像。我不知道怎么做。任何人都可以帮我解决这个问题:)我总是问这里的问题,因为我在这里得到了解决方案,我将它们标记为答案。请帮帮我..
这是我的.cs代码:
Hi!
I have a grid view that contains checklists. One column of it displays an image of a Check Icon if the checklist had already one milestone or many milestones attached to it. I do not know how to do it. Can anyone help me to assist about that :) I am always asking here questions because I got solutions here that I marked them as an answers. Please, help me out here..
Here is my .cs code:
DataTable dt1 = new DataTable();
SqlCommand query = new SqlCommand("SELECT milestones.milestone_code,checklists.checklist_code, checklists.order_no, checklists.checklist_name, checklists.checklist_status, checklists.repeatable, checklists.checklist_type FROM milestones, checklists WHERE milestones.milestone_name=checklists.milestone_name ORDER BY order_no DESC");
SqlDataAdapter sda = new SqlDataAdapter();
query.CommandType = CommandType.Text;
query.Connection = amicassaCon;
try
{
amicassaCon.Open();
sda.SelectCommand = query;
sda.Fill(dt1);
GridView1.DataSource = dt1;
GridView1.DataBind();
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
finally
{
amicassaCon.Close();
sda.Dispose();
amicassaCon.Dispose();
//Response.Write(strQuery);
}
这是我的.aspx代码
Here is my .aspx code
<asp:GridView ID="GridView1" runat="server" DataKeyNames="checklist_code" AllowSorting="True" AutoGenerateColumns="False" CellPadding="4" ForeColor="#333333" GridLines="None" Width="100%" BorderColor="Black" BorderStyle="Solid" BorderWidth="1px" OnSelectedIndexChanged="GridView1_SelectedIndexChanged" DataSourceID="SqlDataSource1">
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:TemplateField>
<ItemTemplate><asp:Image ID="imgs" runat="server" ImageUrl="~/Image/check.png" Height="25" Width="25"/></ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="milestone_code" HeaderText="Milestone Code" ReadOnly="True" SortExpression="milestone_code" >
<ItemStyle HorizontalAlign="Center" />
</asp:BoundField>
<asp:BoundField DataField="checklist_code" HeaderText="Checklist Code" ReadOnly="True" SortExpression="checklist_code" >
<ItemStyle HorizontalAlign="Center" />
</asp:BoundField>
<asp:BoundField DataField="order_no" HeaderText="Checklist Order No." ReadOnly="True" SortExpression="order_no" />
<asp:BoundField DataField="checklist_name" HeaderText="Checklist Name" ReadOnly="True" ItemStyle-HorizontalAlign="Left" SortExpression="checklist_name">
<ItemStyle HorizontalAlign="Left" />
</asp:BoundField>
<asp:BoundField DataField="checklist_status" HeaderText="Status" ItemStyle-HorizontalAlign="Left" ReadOnly="True" >
<ItemStyle HorizontalAlign="Center" />
</asp:BoundField>
<asp:BoundField DataField="repeatable" HeaderText="Repeatable" />
<%-- <asp:TemplateField HeaderText="Repeatable">
<ItemTemplate> <%# (Eval("repeatable").ToString()) == "1" ? "Y" : "N" %></ItemTemplate>
</asp:TemplateField>--%>
<asp:BoundField DataField="checklist_type" HeaderText="Type" />
<asp:CommandField ShowCancelButton="False" ShowEditButton="True" />
</Columns>
<EditRowStyle BackColor="#7C6F57" />
<FooterStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#4A004A" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#666666" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#E3EAEB" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#C5BBAF" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#F8FAFA" />
<SortedAscendingHeaderStyle BackColor="#246B61" />
<SortedDescendingCellStyle BackColor="#D4DFE1" />
<SortedDescendingHeaderStyle BackColor="#15524A" />
</asp:GridView>
推荐答案
其实我它添加图像,但我需要语法来检查清单是否已经有一个 milestone_code
,将出现一个清单图标图像,如果尚未分配,它有没有图像。请将帮助扩展到我:)
Actually I made it to add the image but I need the syntax to check if the checklist is already have a milestone_code
, an image of checklist icon will appear and if not yet assigned, it has no image. Please extend your help to me :)
您可以使用 GridView.RowDataBound事件 [ ^ ]。
You can use GridView.RowDataBound Event[^].
void GridView1_RowDataBound(Object sender, GridViewRowEventArgs e)
{
if(e.Row.RowType == DataControlRowType.DataRow)
{
string milestone_code = DataBinder.Eval(e.Row.DataItem, "milestone_code").ToString();
if(string.IsNullOrEmpty(milestone_code))
{
Image imgs = e.Row.FindControl("imgs") as Image;
imgs.Visible = false;
}
}
同样在 GridView中声明此事件
标记如...
Also declare this Event in GridView
Markup like...
<asp:GridView ID="GridView1" runat="server" OnRowDataBound="GridView1_RowDataBound" ............
谷歌你可以找到很多例子:-)
这里有一些有用的链接可以帮到你
如何让gridview中的一列成为图像?
如何使用C#在asp.net的gridview中添加图像
将图像插入Gridview单元格取决于文本
Google and you can find plenty of examples :-)
Here are some useful links that might help you
how to have one of the column in gridview to be an image?
how to add image in gridview in asp.net using C#
Insert an image into a Gridview cell depending on text
您可以添加eTemplateField
。在里面添加一个图像
。
参考 - Asp.net在gridview中插入,编辑,更新,删除数据 [ ^ ]。
他为编辑,删除和<等按钮添加了ImageButtons
我>取消的。而不是ImageButtons
,您只能添加图像
。
You can add oneTemplateField
. Inside that add oneImage
.
Refer - Asp.net insert, Edit, update, delete data in gridview[^].
He has addedImageButtons
for Buttons like Edit, Delete and Cancel. Instead ofImageButtons
, you can addImage
only.
这篇关于如何在gridview中添加图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!