如何在DataGridView单元格中添加标签(不只是一个)
问题描述:
我需要一个网格,其最后一列中包含一些不同的颜色标签,如下所示。我知道如何处理一个标签,但我需要4个,并且需要使用网格上的值(değer)使它们可见或不可见。如何在DataGridView单元格中添加标签(不只是一个)
例如
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...
任何帮助将不胜感激。
答
你需要一个函数,它看起来像这样:
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类根据您的条件动态添加标签。之后,带有标签的位图成为单元格的内容。
+0
这正是我需要感谢的@Blablablaster – CanESER
答
你是不是指这个样子?
<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
}
}
看看这个问题。你可能会有一些想法 http://stackoverflow.com/questions/14014781/how-to-add-a-label-to-a-datagridview-cell?rq=1 – SSJGSS
这些例子只有一个标签,我需要创建多个标签并用网格列中的另一个值控制 – CanESER