2016-05-31 89 views
0

我需要一個網格,其最後一列中包含一些不同的顏色標籤,如下所示。我知道如何處理一個標籤,但我需要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... 

任何幫助將不勝感激。

grid

+0

看看這個問題。你可能會有一些想法 http://stackoverflow.com/questions/14014781/how-to-add-a-label-to-a-datagridview-cell?rq=1 – SSJGSS

+0

這些例子只有一個標籤,我需要創建多個標籤並用網格列中的另一個值控制 – CanESER

回答

1

你需要一個函數,它看起來像這樣:

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

0

你是不是指這個樣子?

<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    
     } 
    } 
+0

對不起,我的意思是winforms,而不是asp.net – CanESER

+0

喜歡(這個?)[http://www.c-sharpcorner.com/uploadfile/vasanthks/how- to-add-label-controls-in-datagrid /] – SSJGSS

相關問題