如何在 gridview 中创建标签或文本以在某些条件下覆盖某些单元格?
How to create a label or a text in gridview to overwrite certain cells for certain conditions?
我正在使用 asp.net 并创建了一个网格视图,如下所示。当状态文本设置为特定状态时,我想用警告覆盖该行中除状态文本之外的所有单元格。
数据来自 MQ 字符串,由单独的 class 管理。我认为行数据绑定事件可能是可行的方法。我在想类似下面的代码
网格视图:
<asp:GridView runat="server" ID="gridDisc" GridLines="none" AutoGenerateColumns="false" CellPadding="2" HeaderStyle-backColor="#CCEEFF" OnRowDataBound="gridDisc_RowDataBound" >
<AlternatingRowStyle CssClass="ep1" />
<Columns>
<asp:BoundField DataField="StatusText" />
<asp:TemplateField>
<ItemTemplate>
<asp:Label ID="lblPartDesc" runat="server"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="Qty" />
<asp:BoundField DataField="UOI" />
<asp:TemplateField>
<ItemTemplate>
<asp:Label ID="lblStockDetails" runat="server"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:Label ID="lblDealerInv" runat="server"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="Listprice" />
<asp:BoundField DataField="DiscCode" />
<asp:BoundField DataField="OptiInd" />
<asp:BoundField DataField="Weight" />
<asp:BoundField DataField="ExchangeSurcharge" />
</Columns>
</asp:GridView>
后面的代码:
protected void gridDisc_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
PartEnquiryLine line = (PartEnquiryLine)e.Row.DataItem;
Label lbl = (Label)e.Row.FindControl("lblStatusDetails");
if (line.StatusText == Text["280"])
{
lbl.Text = Text["290"]
}
但我还没有找到任何关于如何创建在触发时覆盖该行中特定单元格的标签的指南。我可能与此相去甚远,但我该怎么做呢?
您可以执行如下操作。我假设您想使用 lblStockDetails
来显示警告消息,因为您的网格视图中没有 lblStatusDetails
列。
如果您希望警告消息跨越多列,请在 if 部分中使用以下代码。
if (e.Row.RowType == DataControlRowType.DataRow)
{
PartEnquiryLine line = (PartEnquiryLine)e.Row.DataItem;
Label lbl = (Label)e.Row.FindControl("lblStockDetails");
if (line.StatusText == Text["280"])
{
lbl.Text = Text["290"]
e.Row.Cells[2].Visible = false;
e.Row.Cells[3].Visible = false;
e.Row.Cells[4].ColumnSpan = 9;
e.Row.Cells[5].Visible = false;;
e.Row.Cells[6].Visible = false;
e.Row.Cells[7].Visible = false;
e.Row.Cells[8].Visible = false;
e.Row.Cells[9].Visible = false;;
e.Row.Cells[10].Visible = false;
}
我正在使用 asp.net 并创建了一个网格视图,如下所示。当状态文本设置为特定状态时,我想用警告覆盖该行中除状态文本之外的所有单元格。
数据来自 MQ 字符串,由单独的 class 管理。我认为行数据绑定事件可能是可行的方法。我在想类似下面的代码
网格视图:
<asp:GridView runat="server" ID="gridDisc" GridLines="none" AutoGenerateColumns="false" CellPadding="2" HeaderStyle-backColor="#CCEEFF" OnRowDataBound="gridDisc_RowDataBound" >
<AlternatingRowStyle CssClass="ep1" />
<Columns>
<asp:BoundField DataField="StatusText" />
<asp:TemplateField>
<ItemTemplate>
<asp:Label ID="lblPartDesc" runat="server"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="Qty" />
<asp:BoundField DataField="UOI" />
<asp:TemplateField>
<ItemTemplate>
<asp:Label ID="lblStockDetails" runat="server"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:Label ID="lblDealerInv" runat="server"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="Listprice" />
<asp:BoundField DataField="DiscCode" />
<asp:BoundField DataField="OptiInd" />
<asp:BoundField DataField="Weight" />
<asp:BoundField DataField="ExchangeSurcharge" />
</Columns>
</asp:GridView>
后面的代码:
protected void gridDisc_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
PartEnquiryLine line = (PartEnquiryLine)e.Row.DataItem;
Label lbl = (Label)e.Row.FindControl("lblStatusDetails");
if (line.StatusText == Text["280"])
{
lbl.Text = Text["290"]
}
但我还没有找到任何关于如何创建在触发时覆盖该行中特定单元格的标签的指南。我可能与此相去甚远,但我该怎么做呢?
您可以执行如下操作。我假设您想使用 lblStockDetails
来显示警告消息,因为您的网格视图中没有 lblStatusDetails
列。
如果您希望警告消息跨越多列,请在 if 部分中使用以下代码。
if (e.Row.RowType == DataControlRowType.DataRow)
{
PartEnquiryLine line = (PartEnquiryLine)e.Row.DataItem;
Label lbl = (Label)e.Row.FindControl("lblStockDetails");
if (line.StatusText == Text["280"])
{
lbl.Text = Text["290"]
e.Row.Cells[2].Visible = false;
e.Row.Cells[3].Visible = false;
e.Row.Cells[4].ColumnSpan = 9;
e.Row.Cells[5].Visible = false;;
e.Row.Cells[6].Visible = false;
e.Row.Cells[7].Visible = false;
e.Row.Cells[8].Visible = false;
e.Row.Cells[9].Visible = false;;
e.Row.Cells[10].Visible = false;
}