2016-05-31 100 views
0

以下のように最後の列にいくつかの異なる色のラベルを持つグリッドが必要です。私は1つのラベルで行う方法を知っていますが、私は4が必要で、グリッド上の値(değer)で表示または非表示にする必要があります。例えばDataGridViewセルにラベル(1つではない)を追加する方法

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

これらの例にはラベルが1つしかありません。複数のラベルを作成し、グリッド列内の別の値で制御する – 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

申し訳ありませんが、asp.netではなくwinformsを意味します – CanESER

+0

ようこそ(これ?)[http://www.c-sharpcorner.com/uploadfile/vasanthks/how- to-add-label-controls-in-dataagrid /] – SSJGSS

関連する問題