I have an ultrawingrid (v7.2) and I want to display custom default values to the user if a cell's value is null. The details of the calculation of the default value is not really important but I need to run some code every time the cell is being displayed to determine what to display, but crucially, I do not want to change the null value in the cell. It is simply for display purposes.
I have tried writing a datafilter which works up to a point. The main problem I have is that there are occasions where the datafilter doesnt appear to run correctly and I get spurious values being displayed or cells being displayed as empty. In particular if the user moves the mouse over other cells in the grid whilst a cell is in edit mode, cells are painted with weird display values.
My simplified code is as follows where my 'default value' is simply the column.key property. If you click to edit a cell then move the mouse around you get the weird behaviour.
Can you reproduce this. Is this a bug or am i doing something wrong?
Thanks
Andy
using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Text;using System.Windows.Forms;using System.Data.Common;using System.Data.Odbc;using Infragistics.Win.UltraWinGrid;namespace testgrid{ public partial class Form1 : Form { private string connectionString_ = "DSN=Autopricing;Pwd=pwd"; private OdbcConnection connection_; public Form1() { InitializeComponent(); connection_ = new OdbcConnection(connectionString_); DataTable table = loadSql("select * from value"); ultraGrid1.DataSource = table; DataFilterCallback callback = new DataFilterCallback(grid_DataFilter); foreach (UltraGridColumn col in ultraGrid1.DisplayLayout.Bands[0].Columns) col.Editor.DataFilter = callback; } DataTable loadSql(string sql) { OdbcDataAdapter adapter = new OdbcDataAdapter(sql, connection_); DataTable table = new DataTable(); adapter.Fill(table); return table; } class DataFilterCallback : Infragistics.Win.IEditorDataFilter { public delegate object Callback(Infragistics.Win.EditorDataFilterConvertArgs args); public DataFilterCallback(Callback callback) { callback_ = callback; } object Infragistics.Win.IEditorDataFilter.Convert(Infragistics.Win.EditorDataFilterConvertArgs args) { return callback_(args); } Callback callback_; } object grid_DataFilter(Infragistics.Win.EditorDataFilterConvertArgs args) { UltraGridCell cell = args.Context as UltraGridCell; if (cell != null) { if (args.Direction == Infragistics.Win.ConversionDirection.EditorToDisplay) { if (args.Value == DBNull.Value) { args.Handled = true; return cell.Column.Key; } } } return args.Value; } }}
Yes i got this solution to work thanks to your help by writing a creationfilter. My repaint issue has been fixed. Thanks
Do you have any pointers about using creationfilters?
Yes NullText would be a good way to go for simple default values. In my case though different rows in the same column should display different default values according to some particular custom logic. NullText doesnt really handle this i think.
I didn't try to reproduce this but I took a look at the implementation and if you are always returning the column's key when the underlying value is null, you can get the same effect by assigning the column's key to the NullText property instead. NullText was desgined for just the purpose you describe here, to affect only the display while leaving the value intact.
Hi,
It's very hard to tell much from a code snippet like this. But I don't think a DataFilter is a good way to go, anyway. Using a DataFilter would mean you would have to handle both EditorToDisplay and DisplayToEditor, and you don't want to handle DisplayToEditor, because then you would be affecting the value of the cell.
You would probably be better off using a CreationFilter and setting the Text on the TextUIElement inside the grid cells. The text of the TextUIElement will never affect the cell's value.