Change Image in GridViewImageColumn

7 posts, 1 answers
  1. Raja
    Raja avatar
    15 posts
    Member since:
    Jun 2008

    Posted 03 Feb 2009 Link to this post

    Hi,
         I have 4 columns in RadGridView. 
       
       column0---->ImageColumn   
       column1 ---> ImageColumn
       column2---> textColumn   ---> hidden column
       column3--->textColumn    ----> hidden column

    Based on the value in the text column  (column2) i want to change the image in the (column0).

    Based on the value in the text column  (column3) i want to change the image in the (column1).

    Most probably i have 5 different   values comming into each textColumn.
     
    Currently i implemented this by handling viewRowformatting event .

    I see a drastic sluggish when user scrolls through the Gridview. Please suggest a better way to do this.

    Thank you,
    Raja.
  2. Answer
    Jack
    Admin
    Jack avatar
    2333 posts

    Posted 05 Feb 2009 Link to this post

    Hello Raja,

    Thank you for contacting us.

    The bottleneck when using images is the loading time. I suggest you loading all images before processing the gird. In addition, the CellFormatting event is a better place to customize cells. Please consider the following sample code:

    ImageList imagesList = new ImageList(); 
     
    imagesList.Images.Add(Resources.red); 
    imagesList.Images.Add(Resources.green); 
    imagesList.Images.Add(Resources.blue); 
     
    void radGridView1_CellFormatting(object sender, CellFormattingEventArgs e) 
        if (e.CellElement.ColumnIndex == 1) 
        { 
            if ((decimal)e.CellElement.RowInfo.Cells["Hidden1"].Value % 2 == 0) 
            { 
                e.CellElement.Image = imagesList.Images[0]; 
            } 
            else if ((decimal)e.CellElement.RowInfo.Cells["Hidden1"].Value % 3 == 0) 
            { 
                e.CellElement.Image = imagesList.Images[1]; 
            } 
            else 
            { 
                e.CellElement.Image = imagesList.Images[2]; 
            } 
        } 
     

    Should you have any further questions, feel free to write us back.

    Regards,
    Jack
    the Telerik team

    Check out Telerik Trainer, the state of the art learning tool for Telerik products.
  3. UI for WinForms is Visual Studio 2017 Ready
  4. Raja
    Raja avatar
    15 posts
    Member since:
    Jun 2008

    Posted 05 Feb 2009 Link to this post

    Hi Jack,

                   Thank you for the solution. I missed that imagelist part. Awesome and wonderfull.

    Thank you,
    Raja
  5. Christian
    Christian avatar
    81 posts
    Member since:
    Aug 2008

    Posted 07 Aug 2012 Link to this post

    HI,
    I use the WinForms version 726. The event CellFormatting fires correctly. But the entry of an image in the cell does not work. It does not display the graphic. Is there a bug in the version?
    Here is an excerpt from my code:

    private void gvMitarbeiterBestand_CellFormatting(object sender, CellFormattingEventArgs e)
            {
                try
                {
                    GridDataCellElement dataCell = e.CellElement as GridDataCellElement;
                    if (dataCell != null && dataCell.ColumnInfo.Name == "Ausgang")
                    {
                        GridViewRowInfo zeile = dataCell.RowInfo;
                        if (zeile != null)
                        {
                            clsPersonal mitarbeiterZeile = zeile.DataBoundItem as clsPersonal;
                            if (mitarbeiterZeile != null && !formatierteEintraege.Contains(mitarbeiterZeile))
                            {
                                formatierteEintraege.Add(mitarbeiterZeile);
      
                                if (personallisteVerwendungOffenAbgang != null)
                                {
                                    bool abgang = personallisteVerwendungOffenAbgang.Contains(mitarbeiterZeile);
                                    if (abgang)
                                    {
                                        e.CellElement.DrawText = false;
                                        e.CellElement.Image = Berlin.Polizei.PADUA.Properties.Resources.doorIn;
                                        e.CellElement.BackColor = Color.Blue;
                                    }
                                    else
                                    {
                                        e.CellElement.ResetValue(LightVisualElement.DrawTextProperty, Telerik.WinControls.ValueResetFlags.Local);
                                         
                                    }
                                  }
                                if (personallisteVerwendungAdV != null)
                                {
                                    bool andereADV = (personallisteVerwendungAdV.Contains(mitarbeiterZeile) || mitarbeiterZeile.IstAktuellAbwesend);
                                    AbwesendAnzeigen(zeile, andereADV);
                                }
      
                            }
                        }
                    }
                    }
                catch (Exception ex)
                {
                    Berlin.Polizei.PADUA.GUI.AllgemeineFunktionen.SystemFehlerAnzeigen(ex);
                }
                finally
                {
                    this.Cursor = Cursors.Default;
                }
            }
  6. Stefan
    Admin
    Stefan avatar
    2890 posts

    Posted 10 Aug 2012 Link to this post

    Hi,

    Your question has already been answered in the support thread you've opened. Please, see our answer there for more information.

    We kindly ask you to use just one support channel to contact us. Posting the same questions numerous times slows down our response time because we will need to review and address two or more tickets instead of one. Moreover threads are handled according to license and time of posting, so if it is an urgent problem, we suggest you use a support ticket, which would be handled before a forum thread.

    Thank you for your understanding.Kind regards,
    Stefan
    the Telerik team
    RadControls for WinForms Q2'12 release is now live! Check out what's new or download a free trial >>
  7. fariba
    fariba avatar
    21 posts
    Member since:
    May 2014

    Posted 13 Aug 2015 in reply to Stefan Link to this post

    Hi

    I added GridViewImageColumn to my grid

    and then on  grid_CellFormatting I wrote below code

        
                if (e.Column.Name == "StatusDescription")
                {
                    PreOrders order = (PreOrders)e.Row.DataBoundItem;

                    e.CellElement.ImageLayout = ImageLayout.Center;
                    switch (order.Status)
                    {

                        case PreOrderStatusEnum.NotSend:
                            e.CellElement.Image = null;
                            break;
                        case PreOrderStatusEnum.Send:
                            e.CellElement.Image = Properties.Resources.Send;
                            break;
                        case PreOrderStatusEnum.SendWithError:
                            e.CellElement.Image = Properties.Resources.errorImage;
                            break;
                        case PreOrderStatusEnum.SendSuccesfully:
                            e.CellElement.Image = Properties.Resources.sendSuccesfully;
                            break;
                    }
                    grid.Invalidate();
                }

    which set suitable image depends on condition to cell

    but the image does not shown immediately and need to up-down on row or go out from cell  

    what should I do to solve the problem

    thx

    Fariba

  8. Stefan
    Admin
    Stefan avatar
    2890 posts

    Posted 14 Aug 2015 Link to this post

    Hello Fariba,

    You can try to subscribe to the CellFormatting event at an earlier point of time e.g. at design time, or prior setting/adding the grid's DataSource/Rows.

    Also, the grid.Invalidate() call should not be added in this event, so you can remove it.

    If you still have issues, please open a support ticket and provide us with a project we can reproduce the experienced issue and we will look into it.

    I hope this helps.

    Regards,
    Stefan
    Telerik
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
Back to Top
UI for WinForms is Visual Studio 2017 Ready