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

How do I display a calculated value on a bound column

Hi, I have a read-only column which is bound to a field in a DataTable. I want to claculate the displayed value based on the underlying data in the field. Can I do this without having to create another column and hiding the underlying data?

3 Replies

AD Administrator Syncfusion Team July 10, 2006 05:06 PM UTC

One way you can do this is to handle the grid.DrawCellDisplayText and there set e.DisplayText to be what you want. void gridDataBoundGrid1_DrawCellDisplayText(object sender, Syncfusion.Windows.Forms.Grid.GridDrawCellDisplayTextEventArgs e) { if (e.ColIndex == gridDataBoundGrid1.Binder.NameToColIndex("Col2")) { e.DisplayText = "someComputedTextFrom " + e.Style.Text; } }


BL Bill Langlais July 12, 2006 02:12 AM UTC

Hi, I am using a GridGroupingControl and have been unable to find: gridDataBoundGrid1.Binder.NameToColIndex("Col2") I have tried the following code: System::Void Form1::srgDrawCellDisplayText(System::Object * sender, Syncfusion::Windows::Forms::Grid::Grouping::GridTableControlDrawCellDisplayTextEventArgs * e) { if(e->Inner->ColIndex == e->TableControl->Model->NameToColIndex(S"Run Distance")) { String *Label = String::Format(S"Indexes {0} {1}", __box(e->Inner->RowIndex), __box(e->Inner->ColIndex)); Debug(Label, S"{0} {1}", e->Inner->Style->CellValue, e->Inner->Style->CellType); // bool IsRecord = Element::IsRecord(e->Inner); if(e->Inner->Style->CellType == S"TextBox") { Double x = Convert::ToDouble(e->Inner->Style->CellValue) / 5280; String *Value = x.ToString(DistFmt); e->Inner->DisplayText = Value; Label = String::Format(S"Value {0} {1}", __box(e->Inner->RowIndex), __box(e->Inner->ColIndex)); Debug(Label, S"{0} {1} {2} {3}", e->Inner->Style->CellValue, e->Inner->Style->CellType, Value, e->Inner->Style->ValueMember); } } I find that the Column Index returned by e->TableControl->Model->NameToColIndex(S"Run Distance") does not always come back with the correct index depending on what level I am in in the nested grid. The column name S"Run Distance" exists in several of the nested tables. Am I using the wrong code to figure out what the right column is for the data column I am trying to modify the display value? Thanks!


AD Administrator Syncfusion Team July 12, 2006 09:59 AM UTC

In the grouping grid, you can cast e.Inner.Style to a GridTableCellStyleInfo object and get the information you need from it. Here is C# code that makes Col1 display 2 times what is in Col2. (Col2 is a column of doubles.) private void gridGroupingControl1_TableControlDrawCellDisplayText(object sender, GridTableControlDrawCellDisplayTextEventArgs e) { //make Col1 be 2 times the value in Col2 GridTableCellStyleInfo style = (GridTableCellStyleInfo)e.Inner.Style; if(style.TableCellIdentity.Column != null && style.TableCellIdentity.Column.MappingName == "Col1") { GridRecord rec = style.TableCellIdentity.DisplayElement.GetRecord() as GridRecord; if(rec != null) { double d = (double) rec.GetValue("Col2"); e.Inner.DisplayText = string.Format("{0}", 2 * d); } } }

Loader.
Live Chat Icon For mobile
Up arrow icon