Conditional image in win form..

10 posts, 3 answers
  1. Anoop
    Anoop avatar
    76 posts
    Member since:
    Sep 2006

    Posted 06 Mar 2011 Link to this post

    I want to add a conditional image to my code in order to get the statues of each person every day.

     

    Is that possible???

    See the image that I need to make the same

    I need it ASAP

    Many Thanks

  2. Answer
    Richard Slade
    Richard Slade avatar
    3000 posts
    Member since:
    May 2009

    Posted 06 Mar 2011 Link to this post

    Hello Anoop,

    One way to do this is to use the CellFormatting event. Please see this hep topic for more information
    If you need further help, please let me know
    Thanks
    Richard
  3. UI for WinForms is Visual Studio 2017 Ready
  4. Anoop
    Anoop avatar
    76 posts
    Member since:
    Sep 2006

    Posted 07 Mar 2011 Link to this post

    Thanks a lot Richard

     

    Can’t we use a picture instead of changing color??

  5. Answer
    Emanuel Varga
    Emanuel Varga avatar
    1336 posts
    Member since:
    May 2010

    Posted 07 Mar 2011 Link to this post

    Hello guys,

    The CellFormatting event is the way to go if you want to add an image to the cell, please refer to the following example:
    using System.Collections.Generic;
    using System.Drawing;
    using System.Windows.Forms;
    using Telerik.WinControls.UI;
     
    public partial class Form1 : Form
    {
        private RadGridView radGridView1;
        private List<CustomGridSource> list;
     
        public Form1()
        {
            InitializeComponent();
            this.Size = new Size(800, 600);
     
            this.Controls.Add(radGridView1 = new RadGridView());
            radGridView1.Dock = DockStyle.Fill;
            radGridView1.CellFormatting += new CellFormattingEventHandler(radGridView1_CellFormatting);
            radGridView1.AutoSizeColumnsMode = GridViewAutoSizeColumnsMode.Fill;
     
            var list = new List<CustomGridSource>();
            for (int i = 0; i < 100; i++)
            {
                list.Add(new CustomGridSource { Id = i, Name = "Name" + i, DataType = (i % 3 == 0) ? DataTypeEnum.Data3 : ((i % 2 == 0) ? DataTypeEnum.Data2 : DataTypeEnum.Data1) });
            }
     
            radGridView1.DataSource = list;
        }
     
        void radGridView1_CellFormatting(object sender, CellFormattingEventArgs e)
        {
            if (e.Column.FieldName == "DataType")
            {
                e.CellElement.DrawText = false;
                e.CellElement.Image = GetImageForType(e.CellElement.Value);
            }
            else
            {
                e.CellElement.ResetValue(LightVisualElement.DrawTextProperty, Telerik.WinControls.ValueResetFlags.Local);
                e.CellElement.ResetValue(LightVisualElement.DrawTextProperty, Telerik.WinControls.ValueResetFlags.Local);
            }
        }
     
        private Image GetImageForType(object p)
        {
            Image img = null;
     
            if (p is DataTypeEnum)
            {
                var dataType = (DataTypeEnum)p;
                switch (dataType)
                {
                    case DataTypeEnum.Data1:
                        img = Properties.Resources.image1;
                        break;
                    case DataTypeEnum.Data2:
                        img = Properties.Resources.image2;
                        break;
                    case DataTypeEnum.Data3:
                        img = Properties.Resources.image3;
                        break;
                    default:
                        break;
                }
            }
     
            return img;
        }
    }
     
    public class CustomGridSource
    {
        public int Id { get; set; }
     
        public string Name { get; set; }
     
        public DataTypeEnum DataType { get; set; }
    }
     
    public enum DataTypeEnum
    {
        Data1,
        Data2,
        Data3
    }

    Hope this helps, if you have any other questions or comments, please let me know,

    Best Regards,
    Emanuel Varga
    Telerik WinForms MVP
  6. Anoop
    Anoop avatar
    76 posts
    Member since:
    Sep 2006

    Posted 07 Mar 2011 Link to this post

    Thanks Emanuel

    but iam getting this error

     

     

    RadGridView1.CellFormatting =

    New CellFormattingEventHandler(AddressOf radGridView1_CellFormatting)

     

     

     

    Error 1 'Public Event CellFormatting(sender As Object, e As Telerik.WinControls.UI.CellFormattingEventArgs)' is an event, and cannot be called directly. Use a 'RaiseEvent' statement to raise an event. 

     

     

    Key

     

    .Id = i, _

     

     

     

    Error 2 Name of field or property being initialized in an object initializer must start with '.'. 

     

     

    Properties.Resources.image1

    Error 4 'Telerik.WinControls.UI.Properties.Resources' is not accessible in this context because it is 'Friend'. 

    do you have any idea ??

    thanks a lot
    anoop

  7. Richard Slade
    Richard Slade avatar
    3000 posts
    Member since:
    May 2009

    Posted 07 Mar 2011 Link to this post

    Hello Anoop,

    Are you using vb.net? If so, the conversion would be as follows
    AddHandler RadGridView1.CellFormatting, AddressOf RadGridView1_CellFormatting
    or you can just add the Handles clause on the end of the RadGridView1_CellFormatting method

    Hope that helps
    Richard
  8. Anoop
    Anoop avatar
    76 posts
    Member since:
    Sep 2006

    Posted 07 Mar 2011 Link to this post

    yeah i converted to VB

    However still not working well with me

    this is it after the conversion

    Imports System.Collections.Generic
    Imports System.Drawing
    Imports System.Windows.Forms
    Imports Telerik.WinControls.UI
      
    Partial Public Class Form1
        Inherits Form
      
        Private list As List(Of CustomGridSource)
        AddHandler RadGridView1.CellFormatting, AddressOf RadGridView1_CellFormatting
        Public Sub New()
            InitializeComponent()
            Me.Size = New Size(800, 600)
      
            Me.Controls.Add(InlineAssignHelper(radGridView1, New RadGridView()))
            radGridView1.Dock = DockStyle.Fill
            RadGridView1.CellFormatting = New CellFormattingEventHandler(AddressOf radGridView1_CellFormatting)
            AddHandler RadGridView1.CellFormatting, AddressOf radGridView1_CellFormatting
            RadGridView1.AutoSizeColumnsMode = GridViewAutoSizeColumnsMode.Fill
      
            Dim list = New List(Of CustomGridSource)()
            For i As Integer = 0 To 99
                list.Add(New CustomGridSource() With { _
                    Key .Id = i, _
                    Key .Name = "Name" & i, _
                    Key .DataType = If((i Mod 3 = 0), DataTypeEnum.Data3, (If((i Mod 2 = 0), DataTypeEnum.Data2, DataTypeEnum.Data1))) _
                })
            Next
      
            radGridView1.DataSource = list
        End Sub
      
        Private Sub radGridView1_CellFormatting(ByVal sender As Object, ByVal e As CellFormattingEventArgs)
            If e.Column.FieldName = "DataType" Then
                e.CellElement.DrawText = False
                e.CellElement.Image = GetImageForType(e.CellElement.Value)
            Else
                e.CellElement.ResetValue(LightVisualElement.DrawTextProperty, Telerik.WinControls.ValueResetFlags.Local)
                e.CellElement.ResetValue(LightVisualElement.DrawTextProperty, Telerik.WinControls.ValueResetFlags.Local)
            End If
        End Sub
      
        Private Function GetImageForType(ByVal p As Object) As Image
            Dim img As Image = Nothing
            AddHandler RadGridView1.CellFormatting, AddressOf RadGridView1_CellFormatting
            If TypeOf p Is DataTypeEnum Then
                Dim dataType = CType(p, DataTypeEnum)
                Select Case dataType
                    Case DataTypeEnum.Data1
                        img = Properties.Resources.image1
                        Exit Select
                    Case DataTypeEnum.Data2
                        img = Properties.Resources.image2
                        Exit Select
                    Case DataTypeEnum.Data3
                        img = Properties.Resources.image3
                        Exit Select
                    Case Else
                        Exit Select
                End Select
            End If
      
            Return img
        End Function
        Private Shared Function InlineAssignHelper(Of T)(ByRef target As T, ByVal value As T) As T
            target = value
            Return value
        End Function
      
        Private Sub RadGridView1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadGridView1.Click
      
        End Sub
    End Class
      
    Public Class CustomGridSource
        Public Property Id() As Integer
            Get
                Return m_Id
            End Get
            Set(ByVal value As Integer)
                m_Id = Value
            End Set
        End Property
        Private m_Id As Integer
      
        Public Property Name() As String
            Get
                Return m_Name
            End Get
            Set(ByVal value As String)
                m_Name = Value
            End Set
        End Property
        Private m_Name As String
      
        Public Property DataType() As DataTypeEnum
            Get
                Return m_DataType
            End Get
            Set(ByVal value As DataTypeEnum)
                m_DataType = Value
            End Set
        End Property
        Private m_DataType As DataTypeEnum
    End Class
      
    Public Enum DataTypeEnum
        Data1
        Data2
        Data3
    End Enum
  9. Answer
    Emanuel Varga
    Emanuel Varga avatar
    1336 posts
    Member since:
    May 2010

    Posted 07 Mar 2011 Link to this post

    Hello Annop,

    Please take a look at this vb revised version:
    Imports System.Collections.Generic
    Imports System.Drawing
    Imports System.Windows.Forms
    Imports Telerik.WinControls.UI
     
    Partial Public Class Form1
        Inherits Form
     
        Private list As List(Of CustomGridSource)
     
        Public Sub New()
            InitializeComponent()
            Me.Size = New Size(800, 600)
     
            AddHandler RadGridView1.CellFormatting, AddressOf radGridView1_CellFormatting
     
            RadGridView1.Dock = DockStyle.Fill
     
            AddHandler RadGridView1.CellFormatting, AddressOf radGridView1_CellFormatting
            RadGridView1.AutoSizeColumnsMode = GridViewAutoSizeColumnsMode.Fill
     
            Dim list = New List(Of CustomGridSource)()
            For i As Integer = 0 To 99
                list.Add(New CustomGridSource() With { _
                    .Id = i, _
                    .Name = "Name" & i, _
                    .DataType = If((i Mod 3 = 0), DataTypeEnum.Data3, (If((i Mod 2 = 0), DataTypeEnum.Data2, DataTypeEnum.Data1))) _
                })
            Next
     
            radGridView1.DataSource = list
        End Sub
     
        Private Sub radGridView1_CellFormatting(ByVal sender As Object, ByVal e As CellFormattingEventArgs)
            If e.Column.FieldName = "DataType" Then
                e.CellElement.DrawText = False
                e.CellElement.Image = GetImageForType(e.CellElement.Value)
            Else
                e.CellElement.ResetValue(LightVisualElement.DrawTextProperty, Telerik.WinControls.ValueResetFlags.Local)
                e.CellElement.ResetValue(LightVisualElement.DrawTextProperty, Telerik.WinControls.ValueResetFlags.Local)
            End If
        End Sub
     
        Private Function GetImageForType(ByVal p As Object) As Image
            Dim img As Image = Nothing
            If (p Is Nothing) Then
                Return img
            End If
     
            If TypeOf p Is DataTypeEnum Then
                Dim dataType = CType(p, DataTypeEnum)
                Select Case dataType
                    Case DataTypeEnum.Data1
                        img = My.Resources.Resource1.image1
                        Exit Select
                    Case DataTypeEnum.Data2
                        img = My.Resources.Resource1.image2
                        Exit Select
                    Case DataTypeEnum.Data3
                        img = My.Resources.Resource1.image3
                        Exit Select
                    Case Else
                        Exit Select
                End Select
            End If
     
            Return img
        End Function
     
    End Class
     
    Public Class CustomGridSource
        Public Property Id() As Integer
            Get
                Return m_Id
            End Get
            Set(ByVal value As Integer)
                m_Id = Value
            End Set
        End Property
        Private m_Id As Integer
     
        Public Property Name() As String
            Get
                Return m_Name
            End Get
            Set(ByVal value As String)
                m_Name = Value
            End Set
        End Property
        Private m_Name As String
     
        Public Property DataType() As DataTypeEnum
            Get
                Return m_DataType
            End Get
            Set(ByVal value As DataTypeEnum)
                m_DataType = Value
            End Set
        End Property
        Private m_DataType As DataTypeEnum
    End Class
     
    Public Enum DataTypeEnum
        Data1
        Data2
        Data3
    End Enum

    BTW: This assumes that you have added the grid from the designer, in the C# version you did not have to add a grid, from what i saw in this code this is the path you decided to move on.

    Hope this helps, if you have any other questions or comments, please let me know,

    Best Regards,
    Emanuel Varga
    Telerik WinForms MVP
  10. Anoop
    Anoop avatar
    76 posts
    Member since:
    Sep 2006

    Posted 07 Mar 2011 Link to this post

    Hi
    am getting this[Error 1 'Resource1' is not a member of 'Resources'] error
    with this line
    img = My.Resources.Resource1.image1
  11. Anoop
    Anoop avatar
    76 posts
    Member since:
    Sep 2006

    Posted 08 Mar 2011 Link to this post

    Thanks Emanuel  
    Thanks Richard

    its working now
Back to Top
UI for WinForms is Visual Studio 2017 Ready