This is a migrated thread and some comments may be shown as answers.

Rows with differents FormatString

3 Answers 157 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Georges
Top achievements
Rank 1
Georges asked on 27 Jul 2010, 01:51 PM
Hello,

I'm working with GridViewDecimalColumn and I would like that digits shown to have differents numbers of decimals according to their row. I've only found a command to impose the fomatString for all the columns. Before the update, it was possible through the command radGridView.rows[i].cell[j].cellElement.formatString, but won't functionnate anymore. I've searched in the documentations and tryed with GetCellElement function and with the cellFormating, but found nothing.

Does someone have an idea ?

Thank you

Georges DE VOS

3 Answers, 1 is accepted

Sort by
0
Stefan
Telerik team
answered on 30 Jul 2010, 04:09 PM
Hello Georges,

Thank you for writing.

As of Q2 2010 the CellElement property is removed. For additional information, please refer to this blog post. The CellFormatting event is the correct event where you should implement your logic. However, we currently have an issue with setting the FormatString property of the CellElement. Until we address the issue, you can use the following workaround:
void radGridView1_CellFormatting(object sender, CellFormattingEventArgs e)
{
    GridDataCellElement cell = e.CellElement as GridDataCellElement;
    if (((GridViewDataColumn)e.CellElement.ColumnInfo).FieldName == "Id")
    {
        string value = cell.Text;
        if (value != string.Empty)
        {
            decimal decNumber = decimal.Parse(value);
            if (e.CellElement.RowIndex % 2 == 0)
            {
                cell.Text = decNumber.ToString("F4");
            }
            else
            {
                cell.Text = decNumber.ToString("F2");
            }
        }
    }
}

I hope this helps.

Sincerely yours,
Stefan
the Telerik team
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 Public Issue Tracking system and vote to affect the priority of the items
0
Georges
Top achievements
Rank 1
answered on 02 Aug 2010, 09:32 AM
Hello Stefan,

Thank you, it works well.

Just one thing to modify may be.

string value = cell.Text;

By

string value = cell.Value;

Because if number of decimal increase, the precision will stay the same. (for ex. 0.49 -> 0.490 even if the value is 0.492)

Sincerely yours,

Georges DE VOS
0
Stefan
Telerik team
answered on 05 Aug 2010, 01:26 PM
Hi Georges,

Thank you for your reply.

You are correct. So now you can use the following code snippet:

void radGridView1_CellFormatting(object sender, CellFormattingEventArgs e)
{
    GridDataCellElement cell = e.CellElement as GridDataCellElement;
    if (((GridViewDataColumn)e.CellElement.ColumnInfo).UniqueName == "Id")
    {
        if (cell.Value != null)
        {
            string value = cell.Value.ToString();
            if (value != string.Empty)
            {
                decimal decNumber = decimal.Parse(value);
                if (e.CellElement.RowIndex % 2 == 0)
                {
                    cell.Text = decNumber.ToString("F4");
                }
                else
                {
                    cell.Text = decNumber.ToString("F2");
                }
                this.Text = cell.Value.ToString();
            }
        }
    }
}

With this snippet even if you enter 0.492 in a cell that should contain two digits after the decimal point, the cell will display (cell.Text) 0.49. I hope that this approach suits your needs.

Should you need any further assistance, do not hesitate to contact us.

Greetings,
Stefan
the Telerik team
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 Public Issue Tracking system and vote to affect the priority of the items
Tags
GridView
Asked by
Georges
Top achievements
Rank 1
Answers by
Stefan
Telerik team
Georges
Top achievements
Rank 1
Share this question
or