我在asp.net中的Datagrid而不是gridView中显示信息。 datagrid包含一个名为Status的列。状态可以是1或-1。如果状态为1,我显示标记图标,如果状态为-1,则显示取消图标。我已完成编码及其工作但问题是,图标未显示在所有列中,具体取决于条件,如下图所示。根据我的数据库中的值,状态列全部为-1,但如果显示,则跳过列并绑定下一列,依此类推。
这是我的DataGrid标记
<asp:DataGrid ID="VehicleInfo" runat ="server" AutoGenerateColumns="False" GridLines="Horizontal" Width="939px" CssClass=" table table-striped table-bordered" AllowCustomPaging="True" AllowPaging="True" PageSize="50" OnItemDataBound="VehicleInfo_ItemDataBound" OnItemCommand="VehicleInfo_ItemCommand" >
<Columns>
<asp:TemplateColumn HeaderText="#" >
<ItemTemplate>
<%# Container.DataSetIndex + 1 %>
</ItemTemplate>
</asp:TemplateColumn>
<asp:BoundColumn DataField="Type" HeaderText="Veh. Type"></asp:BoundColumn>
<asp:BoundColumn DataField="Make" HeaderText="Make/Model"></asp:BoundColumn>
<asp:TemplateColumn HeaderText="Veh. Reg No">
<ItemTemplate>
<asp:Label ID="RegNo" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.RegistrationNo") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateColumn>
<asp:BoundColumn DataField="EngineNumber" HeaderText="Engine No"></asp:BoundColumn>
<asp:BoundColumn DataField="ChasisNo" HeaderText="Chasis No"></asp:BoundColumn>
<asp:BoundColumn DataField="InjectionYear" HeaderText="Injection Year"></asp:BoundColumn>
<asp:BoundColumn DataField="Size" HeaderText="Veh. Size"></asp:BoundColumn>
<asp:BoundColumn DataField="ServiceTypeName" HeaderText="Service Type"></asp:BoundColumn>
<asp:TemplateColumn HeaderText="Response Status">
<ItemTemplate>
<asp:Label ID="Status" Visible="false" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.registrationstatusResponse") %>'></asp:Label>
<asp:ImageButton ID="cancel" Width="30" Height="30" ImageUrl="/images/cancel.png" Runat=server Visible="false" />
<asp:ImageButton ID="good" Width="30" Height="30" ImageUrl="/images/good.png" Runat=server Visible="false" />
</ItemTemplate>
</asp:TemplateColumn>
<asp:BoundColumn DataField="verificationComment" HeaderText="Remark"></asp:BoundColumn>
</Columns>
<HeaderStyle Font-Bold="True" Font-Names="Verdana" Font-Size="X-Small" CssClass="" ForeColor="White" BackColor="#428BCA" Font-Italic="False" Font-Overline="False" Font-Strikeout="False" Font-Underline="False" />
<ItemStyle Font-Names="Verdana" Font-Size="X-Small" />
</asp:DataGrid>
这是我在itemDataBound事件上的代码,我甚至尝试在ItemDataBound事件上使用循环,但它不起作用。如果有任何遗漏,我需要你的帮助。
protected void VehicleInfo_ItemDataBound(object sender, DataGridItemEventArgs e)
{
if ((e.Item.ItemType == ListItemType.Item))
{
var lab1 = (Label)e.Item.FindControl("status");
var img1 = (ImageButton)e.Item.FindControl("good");
var img2 = (ImageButton)e.Item.FindControl("cancel");
if (lab1.Text == "1")
{
img2.Visible = false;
img1.Visible = true;
}
else if (lab1.Text == "-1")
{
img2.Visible = true;
img1.Visible = false;
}
}
}