如何在 DataGridView 单元格中添加标签(不仅仅是一个)
How to Add Labels(not just one) in DataGridView cell
我需要一个在最后一列中有一些不同颜色标签的网格,如下所示。我知道如何处理一个标签,但我需要 4 个标签,并且需要使用网格上的值 (değer) 使它们可见或不可见。
例如
if value is below 20 the red label will appear,
if value is over 40 the yellow and orange will appear same time,
if value is between 20-40 green label will appear...
任何帮助将不胜感激。
你指的是这样吗?
<asp:TemplateField HeaderText="Select" ItemStyle-HorizontalAlign="Center" ItemStyle-VerticalAlign="Middle">
<ItemTemplate>
<asp:Label runat="server" ID="lblId" Text='<%# Bind("Id") %>'
Visible="false"></asp:Label>
<asp:Label runat="server" ID="lblId" Text='<%# Bind("Id") %>'
Visible="false"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
隐藏代码:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
try
{
Label lbl1 = (e.Row.FindControl("lblFirstEntry") as Label);
//Play with your control
Label lbl2 = (e.Row.FindControl("lblSecondEntry") as Label);
//Play with your control
}
}
您需要一个如下所示的函数:
void UpdateGridColumnLabels(int index){
int width = column.Width;
int height = gridRow.Height;
Bitmap bmp = new Bitmap(width, height, g);
Graphics g = Graphics.FromImage(bmp);
if(value < 20)
g.FillRect(Brushes.Red, 0, 0, width / 3, height);
else if(value >= 20 && value < 40)
g.FillRect(Brushes.Orange, width/3, 0, width / 3, height);
else
g.FillRect(Brushes.Yellow, 2 * width/3, 0, width / 3, height);
gridViewImageColumn[index] = bmp;
}
这里您正在创建适合您的单元格的位图。然后您正在使用 Graphics class 根据您的条件动态添加标签。之后这个带有标签的位图成为单元格的内容。
我需要一个在最后一列中有一些不同颜色标签的网格,如下所示。我知道如何处理一个标签,但我需要 4 个标签,并且需要使用网格上的值 (değer) 使它们可见或不可见。
例如
if value is below 20 the red label will appear,
if value is over 40 the yellow and orange will appear same time,
if value is between 20-40 green label will appear...
任何帮助将不胜感激。
你指的是这样吗?
<asp:TemplateField HeaderText="Select" ItemStyle-HorizontalAlign="Center" ItemStyle-VerticalAlign="Middle">
<ItemTemplate>
<asp:Label runat="server" ID="lblId" Text='<%# Bind("Id") %>'
Visible="false"></asp:Label>
<asp:Label runat="server" ID="lblId" Text='<%# Bind("Id") %>'
Visible="false"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
隐藏代码:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
try
{
Label lbl1 = (e.Row.FindControl("lblFirstEntry") as Label);
//Play with your control
Label lbl2 = (e.Row.FindControl("lblSecondEntry") as Label);
//Play with your control
}
}
您需要一个如下所示的函数:
void UpdateGridColumnLabels(int index){
int width = column.Width;
int height = gridRow.Height;
Bitmap bmp = new Bitmap(width, height, g);
Graphics g = Graphics.FromImage(bmp);
if(value < 20)
g.FillRect(Brushes.Red, 0, 0, width / 3, height);
else if(value >= 20 && value < 40)
g.FillRect(Brushes.Orange, width/3, 0, width / 3, height);
else
g.FillRect(Brushes.Yellow, 2 * width/3, 0, width / 3, height);
gridViewImageColumn[index] = bmp;
}
这里您正在创建适合您的单元格的位图。然后您正在使用 Graphics class 根据您的条件动态添加标签。之后这个带有标签的位图成为单元格的内容。