Click to See Complete Forum and Search --> : Updating a DataTable that is bounded to a gird control


Holly_vi
October 23rd, 2005, 01:15 PM
I have a grid control that is bounded to a DataTable.
MyGridCtrl.DataSource = MyDataSet.Table[0];
As I understand, this means that whenever the user changes the value of a cell on the GridView, it automatically updates that cell in the DataTable.
Most of the times, this mechanism works for me perfectly, but is it possible for me (the developer) to get in between?
What I mean is that on certain occasions I want to take the new cell’s value, do something on it, and then put the result value in the DataTable.
So, is that possible?

jhammer
October 23rd, 2005, 04:51 PM
What you are seeing is actually the DefaultView of the DataTable. This object has a ListChanged event which is raised whenever the user change something in the grid. Only after the user change the row (or you forcely do it by code) the actual DataTable will be changed, and then a RowChanged event is raised.

Holly_vi
October 25th, 2005, 02:32 PM
I don’t really understand your answer. I’ll appreciate if you could elaborate.
I’ll give an example of what I want.
Let’s say I have a column in my DataTable that represents an enumerator.
e.g.
public enum Subject
{
Mathematics = 1,
History = 4,
English = 2
};
For some reasons this column is of type System.Int32. (I can’t change that).
For convenience reasons, on the grid-view this column is displayed by combo-box with the following options: Mathematics, History & English.
Now, here’s the question. If the user selects ‘History’, I need somewhere in the code to translate this value to 4, and only then put this value in the DataTable.
How can I do that?

jhammer
October 25th, 2005, 02:43 PM
How do you show a ComboBox in the DataGrid?
If you are using a custom DataGridColumnStyle then you can make .NET do it automatically by using the DataSource of the combobox. Create a DataTable with columns String,Value and add the enumeration to that table. The ValueMember will be the Value, and the DisplayMember will be String. This way the Table in the grid will auomatically be set with the Value.
If you check tomorrow I might put code for this.

Holly_vi
October 25th, 2005, 04:09 PM
I am using DevExpress.XtraGrid.GridControl instead of the normal DataGrid.
This control lets you use different editors, such as combo-box, and not only text fields.
My problem is complicated because the specific column I’m talking about (I’ll refer to it as colValue), is of type System.Int32 at the DataTable. But, it represents different values in different rows. In some rows it represents enum1, in others it represents enum2, in other it represents just a number, …
(I get this information from another DataTable).
On a row that this column represents just a number, I display it on the grid-view as a simple text-field. In this case, the value that the user put should go straight to the DataTable, (and so it does).
On a row that this column represents an enumerator, I display it on the grid-view as a combo-box with the enumerator options. In this case, the user chooses an option, I have to translate it to a number, and only then it should go to the DataTable.
I hope you understand what I’m trying to say…