SET VALUE OF A CELL RADGRIDVIEW

7 posts, 1 answers
  1. Diogo Marins
    Diogo Marins avatar
    8 posts
    Member since:
    Mar 2012

    Posted 27 Mar 2012 Link to this post

    Does anyone know how do I set a particular value in a cell radgridview?

    For example: 

    if ((bool)e.CellElement.RowInfo.Cells["Arquivado"].Value == true)
                            {
                                e.CellElement.Text = "Sim";
    or
        e.CellElement.Value = "Sim";
    or
        e.CellElement.RowInfo.Cells["Arquivado"].Value = "Sim";
                            }

    Thanks;
  2. Ivan Petrov
    Admin
    Ivan Petrov avatar
    705 posts

    Posted 30 Mar 2012 Link to this post

    Hello Diogo,

    Thank you for writing.

    I believe that the code you have copied is from a CellFormatting event handler. You should not set the cell value from that event as it is intended for visual formatting of the cells. You can set the CellElement Text, which will be visualized by the cell, but his will not update the value. I would recommend reading the following help article which explains the difference between the logical and visual grid layers - Logical vs Visual structure.

    To set the value of a cell you can use code similar to the following:
    this.radGridView1.Rows[0].Cells[0].Value = newValue;

    I hope this will be useful for you. If further questions arise, do not hesitate to write back.

    All the best,
    Ivan Petrov
    the Telerik team
    RadControls for WinForms Q1'12 release is now live! Check out what's new or download a free trial >>
  3. Diogo Marins
    Diogo Marins avatar
    8 posts
    Member since:
    Mar 2012

    Posted 04 Apr 2012 Link to this post

    Ivan

    Do not quite understand. What I do is manipulate the contents of some cells contained in some columns. The only event that came very close to it was the CellFormatting.
    If there is some event like the RowDataBound of the GridView (asp.net), please tell me ..

    Follow all the code snippet with the CellFormatting event:


    private void Grid_CellFormatting (object sender, CellFormattingEventArgs e)
    {
    Image imagem = atendimentoGUI.Properties.Resources.attach_pq;

    try

    {
    if (e.CellElement.ColumnInfo is GridViewDataColumn)
    {                         if(e.CellElement.RowInfo.Cells ["Prazo"]. Value!e.CellElement.RowInfo.Cells = DBNull.Value && ["Status"]. Value! = DBNull.Value)
    { 
    DateTime strPrazo = Convert.ToDateTime(e.CellElement.RowInfo.Cells["Prazo"].Value);
                            string strStatus = (string)e.CellElement.RowInfo.Cells["Status"].Value;
                           

    if ((!strStatus.Equals("Aberto")) && Convert.ToDateTime(strPrazo.ToString("dd/MM/yyyy")) < Convert.ToDateTime(DateTime.Now.ToString("dd/MM/yyyy")))
                            {
                                totalRegistroAtrasado++;
                            }
                            else if (Convert.ToDateTime(strPrazo.ToString("dd/MM/yyyy")) == Convert.ToDateTime(DateTime.Now.ToString("dd/MM/yyyy")))
                            {
                                totalRegistroHoje++;
                            }
                            else if ((funcoesGerais.NumeroDiasEntreDatas(DateTime.Now, strPrazo) <= 5) && (strPrazo > null))
                            {
                                totalRegistroFaltandoCincoDias++;
                            }
                            else if (Convert.ToDateTime(strPrazo.ToString("dd/MM/yyyy")) > Convert.ToDateTime(DateTime.Now.ToString("dd/MM/yyyy")))
                            {
                                totalRegistroFaltandoVariosDias++;
                            }
                            else
                            {
                                totalRegistroNeutro++;
                            }

                            if (e.CellElement.RowInfo.Cells["Arquivado"].Value != DBNull.Value)
                            {
                                if ((bool)e.CellElement.RowInfo.Cells["Arquivado"].Value)
                                {
                                    totalRegistroArquivado++;
                                }
                                else
                                {
                                    totalRegistroNeutro++;
                                }
                            }

                            if ((bool)e.CellElement.RowInfo.Cells["Arquivado"].Value == true)
                            {
                                this.grdSolicitacao.Rows[0].Cells["Arquivado"].Value = "Sim";
                            }
                            else
                            {
                                this.grdSolicitacao.Rows[0].Cells["Arquivado"].Value = "Não";
                            }
                           

    if ((bool)e.CellElement.RowInfo.Cells["PossuiAnexo"].Value == true)
                            {
                                this.grdSolicitacao.Rows[0].Cells["Arquivado"].Value = imagem;
                            }
                            else
                            {
                                this.grdSolicitacao.Rows[0].Cells["Arquivado"].Value = imagem;
                            }                    
                        }
                    }
    }
    catch (Exception ex)
    {
    ExibiErro(ex);
    }
    finally
    {
    funcoesGerais = null;
    }



  4. Ivan Petrov
    Admin
    Ivan Petrov avatar
    705 posts

    Posted 09 Apr 2012 Link to this post

    Hello Diogo,

    Thank you for your reply.

    I am not sure if I understand your case completely, but if what you want is to increase some counters based on values and conditions in the grid after it is data bound, you can simply iterate over the rows and change the values in the DataBindingComplete event. To iterate over the rows, you can use RadGridView.Rows collection.

    The CellFormatting event is fired for every cell. This means that you are executing your logic for each row as many times as columns you have. This will become increasingly slower when you increase the number of columns in your grid. This would also cause your calculations to be incorrect as the values in the cells are always the same, hence your conditions will have the same result and the counters will be increased as many times as columns you have. Therefore, I would kindly ask you to send me a detailed explanation of your case, so I can give you the best approach to achieve it.

    I am looking forward to your reply.

    Kind regards,
    Ivan Petrov
    the Telerik team
    RadControls for WinForms Q1'12 release is now live! Check out what's new or download a free trial >>
  5. Diogo Marins
    Diogo Marins avatar
    8 posts
    Member since:
    Mar 2012

    Posted 10 Apr 2012 Link to this post

    Ivan

    first of all, thank you for your attention and patience.

    What I need is to change the contents of a column if a certain condition is true, for example:

    In my grid (attached), the column "Arquivado" is filled with Boolean data (obtained from the database). If the contents of this column is "True" want to change it to "Sim" and if "False" want to change it to "Não".

    The other column "Anexo" shall have the same condition, and if the content is "True" wantto replace it with an image and if it is "False" I leave it blank or without the image.

    And I do not what your use for this event and not how do I change the content.


    The event advised "DataBindingComplete" solve a problem that was part of the count of my indicators in the legend of my grid (attached). Now the count is correct, since inpreviously CellFormatting event count was done more than once.


    Well, that's it!

    I'm waiting for your answer.

    Thanks; 
  6. Answer
    Svett
    Admin
    Svett avatar
    728 posts

    Posted 13 Apr 2012 Link to this post

    Hi Diogo,

    In this case, the best way to achieve the desired functionality is to use the CellFormatting event in the following manner:
    private void OnRadGridViewCellFormatting(object sender, CellFormattingEventArgs e)
    {
        string columnName = e.Column.Name;
        object value = e.Row.Cells[columnName].Value;
     
        e.CellElement.ResetValue(LightVisualElement.ImageLayoutProperty, ValueResetFlags.Local);
        e.CellElement.ResetValue(LightVisualElement.ImageProperty, ValueResetFlags.Local);
        e.CellElement.ResetValue(LightVisualElement.DrawTextProperty, ValueResetFlags.Local);
     
        if (columnName == "Arquivado")
        {
            e.CellElement.Text = object.Equals(value, true) ? "Sim" : "Nao";
        }
        else if (columnName == "Anexo")
        {
            e.CellElement.DrawText = false;
     
            if (object.Equals(value, true))
            {
                e.CellElement.ImageLayout = ImageLayout.Center;
                e.CellElement.Image = Image.FromFile(@"C:\photo.png");
            }
            else
            {
                e.CellElement.Image = null;
            }
        }
    }

    I hope that you find the provided information useful. 

    Regards,
    Svett
    the Telerik team
    RadControls for WinForms Q1'12 release is now live! Check out what's new or download a free trial >>
  7. Diogo Marins
    Diogo Marins avatar
    8 posts
    Member since:
    Mar 2012

    Posted 13 Apr 2012 Link to this post

    Svett,

    you're the best!


    Thanks, Svett and Ivan Petrov.

Back to Top