Format Cell Values in an Infragistics UltraWinGrid - formatting

I have an Infragistics UltraWinGrid bound to a data source, and one of the columns in that data source contains boolean values. I have the Style property for the column set to Edit so that it will display text instead of a checkbox. However, instead of displaying the values True or False, I want it to display Yes or No.
How do I format the value of the cell to display Yes or No for a boolean value?

You could define a ValueList and set the ValueList property of the column
private void ultraGrid1_InitializeLayout(object sender, InitializeLayoutEventArgs e)
{
ValueList vl=new ValueList();
vl.ValueListItems.Add(true, "Yes");
vl.ValueListItems.Add(false, "No");
e.Layout.Bands[0].Columns["ColumnBoolean"].ValueList=vl;
}

Related

How do you format a datagridview column that is connected to a datasource?

Current screenshot:
I'd like to format the displayed value in the datagridview in the picture to two decimals, i wanted to make the change at display so i still have my orignal values untouched.
I've tried it placing the format in code
before, (At Form's New Sub)
after assigning the datagridview's datasource, (at Form's New Sub)
and also placing it in the designer, (datagridviewtextboxcolumn.defaultcellstyle)
still the values in the datagridview didnt change
You can put this in form load:
dataGridView.Columns["Total"].DefaultCellStyle.Format = "N2";
OR
private void dataGridView_CellFormatting(object sender,DataGridViewCellFormattingEventArgs e)
{
if (e.ColumnIndex == indexOfColumn)
{
e.CellStyle.Format = "N2";
}
}

Format UltragridRow Cells

I need to format the cell of an UltraGrid.
Like: Making the cell to format a DateTime.
I have done for a column,
UltraGridColumn.Format = "d"
likewise is there any option to format individual cells?
UltraGridRow.Cells("abc")= ?
Note: Using the Infragistics version 12.1
The trick to force the UltraGrid to use different editors for cells in the same column is based on a set of programming objects and patterns offered by the infrastructure of the control.
The first thing is to consider is the InitializeRow event. This event is called for each row when you set the initial DataSource of the grid or when you change something in the underlying DataSource. You can differentiate between the two situations thanks to the e.Reinitialize flag. If it is false, then the whole datasource is applied to the grid, if it is true then only a subset of rows are reinitialized usually because the user has edited the cell or the code has changed a value in the datasource.
The second thing to consider is the Editor property present in every UltraGridCell or UltraGridColumn. It is an object that is built automatically by the UltraGrid to allow the cell editing. The UltraGrid code set this object based on the datatype of the column and, obviously, sets the same editor for every cell of the column. You could set your own editor at the column level (usually in the InitializeLayout event) or on cell by cell basis looking at your formatting rule.
Let's try an example (Big parts of this is taken from the example code suggested in its comments by Alhalama)
Suppose you have a DataTable with only two columns:
First column is called CONDITION and contains a string. This string is my formatting requirement.
The second column is called DATEINFO and contains the dates that should be formatted differently accordingly to the value in the column CONDITION.
So if CONDITION = 'TEST1' then the DATEINFO cell is formatted with Day/Month/Year pattern, if the CONDITION='TEST2' then the DATEINFO cell should be formatted with Hour/Minute/Seconds.
private void ultraGrid1_InitializeRow(object sender, InitializeRowEventArgs e)
{
if (e.ReInitialize == false)
{
DefaultEditorOwnerSettings editorSettings;
DateTimeEditor datetime_editor;
string condition = e.Row.GetCellValue("Condition").ToString();
switch (condition)
{
case "TEST1":
editorSettings = new DefaultEditorOwnerSettings()
editorSettings.DataType = typeof(DateTime);
editorSettings.MaskInput = "mm/dd/yyyy";
datetime_editor = new DateTimeEditor(new DefaultEditorOwner(editorSettings));
e.Row.Cells["DateInfo"].Editor = datetime_editor;
break;
case "TEST2":
editorSettings = new DefaultEditorOwnerSettings()
editorSettings.DataType = typeof(DateTime);
editorSettings.MaskInput = "hh:mm:ss";
datetime_editor = new DateTimeEditor(new DefaultEditorOwner(editorSettings));
e.Row.Cells["DateInfo"].Editor = datetime_editor;
break;
}
}
}

DataGridView editing cells on WinForms

I have a Windows Form VS2010 .NET 4 project with a standard DataGridView bound to a datasource on a form.
The grid has a text column that I want to be a point and edit at the character clicked to.
Like normal textbox/editors when you click on the character you want to adjust. If possible I would also like to use the UP/DOWN keys to move between rows but would like the cursor to move to the same character position obviously in the same column without selecting the entire text.
I have tried a few things:
DataGridView1.ClearSelection()
DataGridView1.BeginEdit(False)
The BeginEdit just puts the cursor at the end of the text, which means another click to point to the character position for editing.
I know a Commercial grid like DevExpress defaults to editing in which you can click to the correct character position with one click but obviously costs money.
I have tried in the DataGridView1_EditingControlShowing event
If TypeOf e.Control Is System.Windows.Forms.DataGridViewTextBoxEditingControl Then
Dim tb As TextBox = e.Control
tb.SelectionStart = 5
tb.SelectionLength = 5
End If
But this does nothing.
I am just trying to remove the two or three clicks to get to the character position that needs adjustment.
I haven't looked at a Custom DataColumn as yet.
Any suggestions would be greatly appreciated.
There is no good out of the box way of doing this. The closest there is is to set the EditMode of the grid to EditOnEnter but that means you only need two clicks, not three.
You will need to write your own column type.
Someone has done just that here.
I haven't checked if that example handles up and down - if it doesn't then you were on the right track with the SelectionStart and SelectionLength properties, just grab the caret position of the cell you are leaving and apply it to the new cell.
It turns out that setting these properties is a little bit more involved that I remembered (possibly because I was already using a MaskedTextBox custom column type last time I did this).
The code below (in c# but the principle holds for vb.Net and I can give the vb code if you can't convert it yourself) works happily - could be tidied up by putting it into a custom control but I'll leave that as an exercise :)
First I add a handler for the EditingControlShowing event:
void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
DataGridViewTextBoxEditingControl t = e.Control as DataGridViewTextBoxEditingControl;
current_control = t;
t.Leave += new EventHandler(t_Leave);
}
In the method above current_control is a form level private variable. The event handler for t looks like this:
void t_Leave(object sender, EventArgs e)
{
cell_caret_pos = current_control.SelectionStart;
}
There again we have a class level private field - cell_caret_pos.
Then what I found was that to set SelectionStart and SelectionLength you need to work within the CellEnter event handler:
private void dataGridView1_CellEnter(object sender, DataGridViewCellEventArgs e)
{
dataGridView1.BeginEdit(false);
DataGridViewTextBoxEditingControl editControl =
(DataGridViewTextBoxEditingControl)dataGridView1.EditingControl;
if (cell_caret_pos != 0)
{
editControl.SelectionStart = cell_caret_pos;
editControl.SelectionLength = 0;
}
}

Vb.net combobox formatstring property doesn't work

I have a form created in VB.net. It is used to get some information form a user. The form is not bound to any data source.
A combobox on this form is used to enter a cost. I want the value entered by the user to be displayed using currency format. I have used the Format String Dialog that opens from the ellipses button on the FormatString property of the combobox and selected Currency. This put C2 into the FormatString property.
When I run my application, this format is not applied to the value entered into the combobox at the time the number is entered or when I leave the combobox.
What am I missing?
Set the FormattingEnabled Property to True.
The FormatString property works only for data-bound controls. However, the input in a control can still be formatted with the ToString() method on a Change or Leave event.
The code sample below will format the text in the combo box to the default currency once the focus leaves control. Error handling can be done in the else clause:
private void comboBox1_Leave(object sender, EventArgs e)
{
string s = comboBox1.Text;
decimal result;
if (Decimal.TryParse(s, out result))
{
comboBox1.Text = result.ToString("C2");
}
}

How to change font color of a dojo DataGrid row after a cell has been edited

I want to change the font color of a row after a cell in that row has been edited and set to a certain value.
myStore is the dojo.data.ItemFileWriteStore associated to the dojox.grid.DataGrid dataGrid.
I have written this:
myStore.onSet = function(item, attribute, oldValue, newValue) {
if (item.myField == myValue) {
var index = dataGrid.selection.selectedIndex;
dojo.style(dataGrid.getRowNode(index), "color" , "red");
}
}
but unfortunately this doesn't make any effect...
UPDATE: I added the following style property: "backgroundColor" : "red". Well, the background color of the row changes to red, but when the mouse moves away from the row, the color changes back to the default! It might be that some default event handlers restore the default styles...
The dojo.style line works if you call it by itself. Either your function isn't being called at all, the if's condition false or there is no row selected and you are getting an invalid number for the index. (You can put some console.logs there to check)