We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

databound grid: sorting strings (percentage) as floats

Hi,

I have a databound grid. One of the columns displays percentages. I keep the DataColumn.DataType as a float so that I can sort it. However, I'd like the display to have a '%' after the number. I can't figure out a way to do this without storing the value as a string. If I do this, the sorting is broken.

Any ideas?

Thanks,
Nick

4 Replies

AD Administrator Syncfusion Team January 18, 2007 10:35 PM UTC

I think I figured this out. In the CellDrawn event handler I do the following:

float val =(float) mygrid[e.RowIndex, e.ColIndex].CellValue;
Brush brush = new SolidBrush(e.Style.BackColor);
try
{
e.Graphics.FillRectangle(brush, e.Bounds);
e.Graphics.DrawString(val.ToString("F1") + "%", e.Style.Font.GdipFont, new SolidBrush(e.Style.TextColor), e.Bounds.Location);
e.Cancel = true;
}

finally
{
brush.Dispose();
}

hope that formatted okay.

Anyway, this seems to work. If there is a better way, please let me know

Thanks,
Nick


AD Administrator Syncfusion Team January 18, 2007 10:36 PM UTC

Hi Nick,

If you want to format the cell, then use StyleInfo.Format property to set percentage format in the grid. Here is a code snippet.

this.gridDataBoundGrid1.Binder.InternalColumns[ColumnName].StyleInfo.CellValueType = typeof(float);
this.gridDataBoundGrid1.Binder.InternalColumns[ColumnName].StyleInfo.Format = "P";

Do you want to display the percentage at the end of the cell content without storing it? If yes, you can use DrawCellDisplayText event and set the e.DispalyText to some new value to want to display. Here is a code snippet

private void gridDrawCellDisplayText(object sender, GridDrawCellDisplayTextEventArgs e)
{
if( e.ColIndex == 3 && e.RowIndex >0)
e.DisplayText = e.Style.Text +"%";
}

Best Regards,
Haneef


DH dhana January 19, 2007 06:24 AM UTC

Hi haneef!

I dont have ColIndex property in the DrawcellDisplaytext event.then how to make the value in the cell display with the % symbol.


pls help.

Regards

dhana


AD Administrator Syncfusion Team January 19, 2007 03:19 PM UTC

Hi Dhana,

Try this code snippet to get the ColIndex / RowIndex from the GridDrawCellDisplayTextEventArgs.

int ColIndex = e.Style.CellIndentity.ColIndex;
int ColIndex = e.Style.CellIndentity.RowIndex;

Best Regards,
Haneef

Loader.
Live Chat Icon For mobile
Up arrow icon