Combobox selection inside datagrid - apache-flex

I have a datagrid. In this datagrid I have a combobox item editor. This datagrid also has multiple columns where a user inputs numbers in each column. These numbers are then calculated by formula where the sum is posted in the "total" column. In this combobox there are two options for the user to choose from and each option has a different formula for calculating the inputted numbers. What I want is for when a user chooses "option 1" one formula is used to do the calculation, when "option 2" is chosen by the user then formula two is used to the calculation.
Here's an example:
Combobox Option 1 (formula 1) is chosen by user = (Column2 - Column1) x column3 = "total" column
Combobox Option 2 (formula 2) is chosen by user = (Column1 - Column2) x column3 ="total" column
I realize you would use a conditional such as "if else" statement, but im just not sure how to do it. I've been trying to implement this for a while with no success so any help or suggestions would be greatly appreciated.

Listen for the combobox's change event and implement the formula calculation in the change event handler according to the selectedItem.
public function changeEventHandler(event:Event){
if(ComboBox(evt.target).selectedItem.label == forumla1) {
//logic
} else if(ComboBox(evt.target).selectedItem.label == formula2) {
//logic
} else {
//do nothing
}
}

That's interesting. You cannot add listeners directly because item renderers are reused and don't keep their identity. Some thoughts on problem:
when combobox' selected item is changed, it dispatches bubbling event EVENT.CHANGE.
you should make custom renderer for computed columns. When renderer is added to datagrid (use EVENT.ADDED), use owner property (that should be datagrid) to add listener to EVENT.CHANGE. Check that you getting that event (change renderer's text to "got it", for example).
now all your computed cells are getting notification when any combobox changed. First, you need to discard events from rows other than item's row. To do that, renderer needs to know its own rowIndex - see Creating custom List renderers, item 2. Compare rowIndex and datagrid's selected index to bail out if they don't match.
now you have combobox in event.target, rowIndex and datagrid - that should be enough to get needed formula and data from datagrid's columns.

Related

How to set the values of a Combobox which programmatically added in a grid

I have a grid which for some reasons I added dynamically a combobox. I have done it and working like charm.
The next thing which I am stack is how to set the selection for each row per code.
for example in first row of the gird the combobox I want to has the value X and in the second row I would like to has the value Y.
FormGridControl grid = sender.formRun().design(0).controlName('FormGridControl1');
ColumnTable columnTable;
ValueTable valueTable;
while select * from columnTable
{
FormComboBoxControl combo1 = grid.addControl(FormControlType::ComboBox,columnTable.Name);
combo1.label(columnTable.Name);
combo1.enumType(enumNum(enumValue));;
combo1.registerOverrideMethod(methodStr(FormComboBoxControl, SelectionChange),'DynamicComboControl_SelectionChanged',this);
while select * from valueTable
where valueTable.ColumnName == columnTable.Name
{
// at this place I have to set the values of the combo1 for each line of the grid separately.
// and I have not any idea how I can do this.
}
}
Can someone help me please?
You never assigns values to grid fields using a loop.
If the data in your field in the grid comes from a field of the datasource then use a bound field (maybe created dynamically by addDataField).
fc = grid.addDataField(fbds.id(), fieldNum(MyTable,MyField));
If your data field can be computed by some method add a display method.
Added dynamically like this:
name = tableMethodStr(MyTable,myMethod);
fc = grid.addControl(FormControlType::ComboBox, ds.name()+'_'+name);
fc.dataSource(grid.datasource());
fc.dataMethod(name);
If your data can be edited, then the method must be an edit method. Added dynamically the same way as a display method.
IF your grid have not binded to a datasource
THEN table.cell(col,row).data()...
see tutorial_Form_Table form.
IF your grid have binded to a datasource
THEN create and fill records in the your table and research() the datasource on the form.
Use Selection(i) method if you looking method for FormComboBoxControl.
https://msdn.microsoft.com/ru-ru/library/aa499256(v=ax.50).aspx

NSTableView set column to disabled

I have a NSTableView tied to a NSArrayController. The first column are checkboxes. I want to have the users select the rows to perform an action on via the checkboxes and when the user clicks the "Run" button I want to set the state of that column to be un-editable.
How do I go about this?
Thanks!
rather than disabling the column I disable the individual checkbox cells in my app based on a global app state.
Preconditions:
1.) your "Run button" sets some sort of a global state in your controller class. For me its a "Play" button that sets a member in my controller like:
if ([self gotoNextSong])
_currentPlayMode = PLAYMODE_PLAY;
else
_currentPlayMode = PLAYMODE_STOP;
2.) Your controller is already a delegate to the NSTableView. If not:
In your MyController.h:
#interface MyController : NSObject <NSTableViewDelegate>
In your MyController.m make your controller to a delegate of the table - e.g. in your init: method
[self.myTableView setDelegate:self];
3.) You know the identifier of the column that holds the checkboxes. If not, either its the automatic name like column1, column2, ... or you can set it in Interface Builder while the column is active in Identity Inspector / Identity / Identifier. In my case the column name is "checkboxes"
Solution:
1.) Add a delegate method for the table to inform you when the cells are rendered. Inside this method enable or disable the cells of the checkbox-row as desired
- (void)tableView:(NSTableView *)aTableView willDisplayCell:(id)aCell forTableColumn: (NSTableColumn *)aTableColumn row:(int)rowIndex
{
if(([[aTableColumn identifier] isEqualToString:#"checkboxes"]))
{
if (_currentPlayMode == PLAYMODE_PLAY)
[aCell setEnabled:NO];
else
[aCell setEnabled:YES];
}
}
2.) Make sure that your RUN-button not only sets the global state (in my case _currentPlayMode), but also force the NSTableView to redraw afterwards:
[self.myTableView setNeedsDisplay:YES];

How to Get row by key value or visible index in ASPxGridView then change column value?

Hi
In ASPxGridView, is there a way to get a row by its VisibleIndex or KeyValue so that I can change any column value in it?, I mean something like this:
var row = myGrid.SelectRowByKeyValue(myKeyValue);
OR:
var row = myGrid.SelectRowByVisibleIndex(myKeyValue);
row["Column1"] = true;
Edit:
What I'm tring to do is that every time I hit the button I want to check one specific row (I'm using ajax to not reload all the page);
Thanks
This can be done using the ASPxGridView.GetRow() method. NOTE, that changing the value in the DataRow is not enough. If you want these changes to be preserved, save them to the DB.
Since you are using unbound columns, you should handle the CustomUnboundColumnData event and provide modified data for this row within this event handler. The common approach is described in the Providing Data for Unbound Columns topic. If this does not help, please describe in greater details.
UPDATE
Your approach is incorrect. The ASPxGridView does not provide a method to set a text of a certain cell (TD). Instead, you should force the grid to raise the CustomUnboundColumnData event. This can be done using the ASPxGridView's DataBind method. In this event handler, you should determine the KeyField value of the processed row, compare it with the keyField value of the row where the button was clicked and return the required value. This is how I would implement this feature...
I solved it by using this code:
for (int i = 0; i < myGridView.VisibleRowCount; i++)
{
if ( [My condition] )
{
(
(CheckBox)myGridView
.FindRowCellTemplateControl(i,
myGridView.Columns["MyColumnName"] as GridViewDataColumn,
"My_Unbound_Control_Name"
)
).Checked = true;
}
}
I's may not be the right way to do it but I couldn't solve it another way.

Retain the value entered in the cell of DevExpress Xtragrid

I am using DevExpress Xtragrid control in my C#.net windows application.
I enter some value into the first cell of the grid and if i go to second cell , the value entered in the first cell disappears.
How to retain the value entered in the cell ?
I am assuming that you are using this for an unbound column in a gridView (Xtragrid), first step is make sure to go to the column properties, and change the UnboundType property value to the datatype that you will be entering into that column, example below uses double.
Assign the CustomUnboundColumnData event to your gridView. Make sure that you declare a class level variable (named _userEnteredData in code sample below) to hold the value that you are entering into your gridView, then add the following piece of code, but make sure that you change the names to match your gridView and variable names:
Class level variable declaration:
private double _userEnteredData = 0;
Now the event:
private void gridView1_CustomUnboundColumnData(object sender, DevExpress.XtraGrid.Views.Base.CustomColumnDataEventArgs e)
{
if (e.Column == gridColumn_YourColumn && e.IsSetData)
{
_userEnteredData = Convert.ToDouble(e.Value);
}
else if (e.Column == gridColumn_YourColumn && e.IsGetData)
{
e.Value = _userEnteredData;
}
}
I hope this helps.
You can get further details from here:
http://documentation.devexpress.com/#WindowsForms/CustomDocument1477
Few possibilities:
check FieldName property of edited column. Maybe there is a typo, so grid does not pass your entered value to underlying datasource
property that is bound to column must have public setter. If there is only getter, grid also won't be capable to store entered value
check ColumnOptions.ReadOnly property in grid column - must be set to false
Hope this helps

DropDownList annoyance: same value won't trigger event

i've populated a dropdownlist control with different text properties but each text properties had THE SAME value (text property was A, value properties is blah,text property was B, value properties is blahblah, etc... )
ASP.net only checks value properties on postback and because ALL values were the same (for
testing reason) this little annoying behavior happened. Is there a work around? does this mean you can't never have the value to be the same?
Sounds like you are working on the wrong event. Try SelectedIndexChanged.
Ensure you also have the AutoPostBack property set to True.
Resolved
OK, so I got digging on this since I was curious :)
There is a "problem" when databinding with non-unique values.
So, firstly, I publicly apologise for saying otherwise.
To replicate:
ASPX
<asp:DropDownList ID="myDDL" runat="server" AutoPostBack="True">
</asp:DropDownList>
<asp:Label ID="lblSelItem" runat="server"Text="Currently Selected Item: 0"></asp:Label>
<asp:Label ID="lblSelVal" runat="server" Text="Currently Selected Value: X"></asp:Label>
Code-Behind
List<string> MyData()
{
List<string> rtn = new List<string>();
rtn.Add("I am the same value!");
rtn.Add("I am the same value!");
rtn.Add("I am the same value!");
rtn.Add("I am the same value!2");
return rtn;
}
protected void Page_Init()
{
if (!Page.IsPostBack)
{
// Load the Data for the DDL.
myDDL.DataSource = MyData();
myDDL.DataBind();
}
}
protected void Page_Load(object sender, EventArgs e)
{
// Display the Currently Selected Item/Value.
lblSelItem.Text = "Currently Selected Item: " + myDDL.SelectedIndex.ToString();
lblSelVal.Text = "Currently Selected Value: " + myDDL.SelectedValue;
}
Run, changing the values in the DropDownList. Note that a PostBack does not occur.
When looking at the Source, I realised that we need to explicitly set the "value" attribute for the <option> elements generated by the server control, which lead me to do something like:
New Code-Behind
Dictionary<string, string> MyTwoColData()
{
Dictionary<string, string> rtn = new Dictionary<string, string>();
rtn.Add("1", "I am the same value!");
rtn.Add("2", "I am the same value!");
rtn.Add("3", "I am the same value!");
return rtn;
}
protected void Page_Init()
{
if (!Page.IsPostBack)
{
// Load the Data for the DDL.
Dictionary<string, string> data = MyTwoColData();
foreach (KeyValuePair<string, string> pair in MyTwoColData())
{
myDDL.Items.Add(new ListItem(pair.Value, pair.Key));
}
myDDL.DataBind();
}
}
This explcitly sets the values to the "1", "2", "3" etc making them unique, while still displaying the correct data within the list.
Obviously, you can change this to work with single-column lists but just running through a for loop and using the value of i or something.
As to good workarounds with DataSets, not sure.
Realistically, would we present a list of options with the exact same values to the user?
I personally think not, which is probably why this "problem" hasn't been addressed :)
Enjoy!
PS:
Oh, I should also add, if you want to use the text value in the "fix" then change it to SelectedItem rather than SelectedValue.
ASP.NET can't distinguish between different items with the same values in the dropdown because when the browser sends the HTTP POST, it sends just the selected value.
ASP.NET will find the FIRST item in the dropdown with a value that matches.
You need to ensure that each item in the dropdown has a distinct value. You could do this by adding a key to each value. In other words, instead of having "blah" for each value, you'd use "blah-1", "blah-2", etc.
The problem is that if the selected index doesn't change the postback won't fire. In the case where the user makes the same selection, the selected index does not change.
Sorry that this doesn't answer the question, but it does explain the behavior as far as I know.
The SelectedIndexChanged won't even trigger because all the listitem value in the dropdownlist control are the same. I did some googling. It seem like this is the common problem. I haven't found any work around yet.
You could use values like this:
1:2
2:2
3:2
Where the second number is the "real" value. Then your event should fire and you can parse out the "real" value in your code behind.
Why do you have a drop down where all of the values are the same? Or is just that some of them are the same?
If you think back to pre ASP.Net days then the only thing that is send with a form submit from a <SELECT> is the VALUE of the <OPTION>. ASP.Net then effectively works out which item is selected by looking up this value in the list of data items.
You will also notice that if you have two items with the same value but different labels that if you do trigger a postback the next time the form loads the first one will be displayed, even if you have the second one selected before you performed the postback.
If you take a step back for a moment and consider your original data source - how would you identify which text value was selected if all you have is the Value? How would you select that value from a database, or from a list? How would you update that row in the database? If you try it you will find that .Net throw an Exception because it cannot uniquely identify the row.
Therefore you need to add a unique key to your data.

Resources