1

I need a grid which has some different color labels in the last column as below. I know how to do with one label but i need 4 and need to make them visible or invisible with the value(değer) on the grid.

for example

  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... 

any help will be appreciated.

grid

Ruban J
  • 622
  • 1
  • 7
  • 31
CanESER
  • 301
  • 1
  • 3
  • 19
  • Have a look at this Question. You may get some ideas http://stackoverflow.com/questions/14014781/how-to-add-a-label-to-a-datagridview-cell?rq=1 – Ruban J May 31 '16 at 08:37
  • Those examples have just one label, I need to create more than one label and control with another value in the grid column – CanESER May 31 '16 at 08:40

2 Answers2

2

You need a function which looks like this:

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;
}

Here you are creating a bitmap that fits into your cell. Then you are using Graphics class to add labels dynamically depending on your conditions. Afterwards this bitmap with labels becomes the content of the cell.

Blablablaster
  • 3,238
  • 3
  • 31
  • 33
0

Are you referring like this ?

<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>

Code Behind:

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            
        }
    }
Ruban J
  • 622
  • 1
  • 7
  • 31