Hi
I´m having some problems when I add a column with a UltraComboEditor to a ultragrid. The UltraComboEditor have a ValueList and when I load the grid all looks ok but when i try editing a row I get an error "Unable to update the data value. Unable to convert from system.string to system.Int32".
This is my setup:
var valueList = new ValueList();
var items = new ValueListItem[] { new ValueListItem(1, "one"), new ValueListItem(2, "two"), new ValueListItem(3, "three") }; valueList.ValueListItems.AddRange(items);
var ultraComboEditor = new UltraComboEditor() { ValueList = valueList };
ultraComboEditor.CheckedListSettings.CheckBoxStyle = CheckStyle.CheckBox; ultraComboEditor.CheckedListSettings.EditorValueSource = EditorWithComboValueSource.CheckedItems; ultraComboEditor.CheckedListSettings.ListSeparator = ", "; ultraComboEditor.CheckedListSettings.ItemCheckArea = ItemCheckArea.Item;
var gridTable = new DataTable(); gridTable.Columns.Add("column", typeof(int[])); gridTable.Rows.Add((new int[] { 1, 2 })); gridTable.Rows.Add((new int[] { 1 })); gridTable.Rows.Add((new int[] { }));
ultraGrid1.DataSource = gridTable;
ultraGrid1.DisplayLayout.Bands[0].Columns[0].EditorComponent = ultraComboEditor; ultraGrid1.DisplayLayout.AutoFitStyle = AutoFitStyle.ExtendLastColumn;
/Andreas
Hi Andreas,
The problem is in your code where you are adding the new rows.
gridTable.Rows.Add((new int[] { 1, 2 })); gridTable.Rows.Add((new int[] { 1 })); gridTable.Rows.Add((new int[] { }));
You are calling the Add method on the rows collection of the DataTable. This method takes in an object array. Each member of the array is the value of a column. Your daatTable only has one column, but you are trying to add 2 values. So you are specifying a value for a second column which does not exist.
What you probably intended to do here was to assign an array of integers as the value of the first (only) column. So you need to pass in an object array that has a single value, which in this case also happens to be an array.
gridTable.Rows.Add(new object[] { new int[] { 1, 2 } }); gridTable.Rows.Add(new object[] { new int[] { 1 } }); gridTable.Rows.Add(new object[] { new int[] { } });
Hi Mike
I don´t think that is the problem here. With the table below the first row add will add a row with int[] in column1 and column2. The second will add a row with int[] in column1. The third will also end up with int[] in column1
var gridTable = new DataTable(); gridTable.Columns.Add("column1", typeof(int[])); gridTable.Columns.Add("column2", typeof(int[])); gridTable.Rows.Add(new int[] { 1, 2 }, new int[] { 1, 2 }); gridTable.Rows.Add(new int[] { 1, 2 } ); gridTable.Rows.Add(new object[] { new int[] { 1, 2 } });
Anyways, both
gridTable.Rows.Add(new int[] { 1, 2 } );
and
gridTable.Rows.Add(new object[] { new int[] { 1, 2 } });
will load the grid with data and show the correct display values from the valuelist. Both gives the same data error when I leave the row after i edit it
You can try it out in the sample solution in the original post.
Since my code works correctly and your code raises an exception, I am pretty sure that my initial answer was correct.
I do see that this line of code:
does not raise an exception when the Datatable has two columns and that the datatable seems to treat the array as a single value for the first column in this case. I'm not sure why that is, but it has nothing to do with the WinGrid or any Infragistics controls, so if you want an explanation for why the DataTable behaves different with 2 columns than it does with one, you would need to pursue that with Microsoft.
The overload of the Add method takes in an object array and each member of the array is supposed to be the value of a different cell in the row. That's the way it works. Clearly there is some sort of attempt made to interpret an Int[] if you pass that in, but I wouldn't rely on that, since you are essentially relying on undocumented behavior and there's no guarantee that it will continue to work in the future.
I think I have failed explaining my problem. Even If I add the data to the tables as you suggest I get the same error when I edit the grid on the form. When the grid initially loads it shows the rows with the displaynames and if I open the dropdown for the column in the grid It correctly shows the differnt options. If I edit the column it will correctly display the differnt selected values but when leave the row I get the error "Unable to update the data value. Unable to convert from system.string to system.Int32". To be clear. I get this even if i change how the rows are added to the way you suggest. This is the reason I did not mark your answer as a solution to my problem
I think maybe I was a little confused, also. When I ran your sample the first time, it raised an Exception for me the first time you call Rows.Add. But it turns out that's because of a necessary change I made to the code.
The basic problem here is that the Multi-selected (CheckSettings) feature does not work with an array of Int or any other specific type. The DataType of the column MUST be an array of objects. I think it might also work with an array of Strings in some cases, but object works for sure.
So the first thing I did when running your sample was change:
gridTable.Columns.Add("column", typeof(int[]));
to:
gridTable.Columns.Add("column", typeof(object[]));
So the correct way to set this up is like so:
var gridTable = new DataTable(); gridTable.Columns.Add("column", typeof(object[])); gridTable.Rows.Add(new object[] { new object[] { 1, 2 } }); gridTable.Rows.Add(new object[] { new object[] { 1 } }); gridTable.Rows.Add(new object[] { new object[] { } });
I tried this out and it compiles and runs and I am able to drop down the list in a cell, choose some new values, and then click the button to lose focus from the grid and it all works.
Excellent!
This works for me. Thank you for your time.