Set background color table cell in codenameone - codenameone

i want to set the backgroundcolor of a couple of cells in codenameone
I was wondering if there is a function to do that
I kwow how to set the background color of a row in the table definition. But to set for each row a line.
But Is there something like setvalue? Like cell(row, column).getallstyles.bgcolor=

Table cells are created dynamically based on model data so you need to override the cell creation method e.g.:
Table table = new Table(model) {
#Override
protected Component createCell(Object value, int row, int column, boolean editable) {
Component cell = super.createCell(value, row, column, editable);
if(!isValidValue(value)) {
cell.getAllStyles().setFgColor(0xff0000);
}
return cell;
}
};
This sets all the cells with invalid values to red.

Related

How to get value of cell at current row GridView winforms devexpress

I need to know which row I selected and get value of cell(Ex:idproduct) before to click Edit button.
As #brendon is referring to, if gridView is the current View on your GridControl:
// Get your currently selected grid row
var rowHandle = gridView.FocusedRowHandle;
// Get the value for the given column - convert to the type you're expecting
var obj = gridView.GetRowCellValue(rowHandle, "FieldName");
You can use the GridView's GetRowCellValue method to retrieve the focused row value.
http://documentation.devexpress.com/#windowsforms/DevExpressXtraGridViewsGridGridView_GetRowCellValuetopic
See also: http://documentation.devexpress.com/windowsforms/CustomDocument753.aspx
public int idproductx;
public void tProductGridView_RowClick(object sender, RowClickEventArgs e)
{
if (e.Clicks > 0)
{
idproductx = (int)((GridView)sender).GetRowCellValue(e.RowHandle, "idproduct ");
}
}

How to get the selected column count in datagrid

I want selected column's index of the DataGrid. For example, if I select the first column, I want the first column's index (index = 0).
I tried it in the DataGrid SelectionChanged event, but I can't seem to get the particular column index. If any one knows how to do it, help me with some sample code.
The DataGrid.Items property returns a DataGridItemCollection representing the DataGridItems in the DataGrid.
Each DataGridItem is representative of a single row in the rendered table. Also, the DataGridItem exposes a Cells property which represents the no. of tablecells (in other words, the columns) in the rendered table.
// Get the Row Count
int rowCount = myGrid.Items.Count;
// Get the no. of columns in the first row.
int colCount = myGrid.Items[0].Cells.Count;
I am assuming that you want the indexes of any columns that are selected. Here is the code I've come up with:
List<int> selectedColumnIndexes = new List<int>(dataGrid.SelectedCells.Count);
for (int i = 0; i < dataGrid.SelectedCells.Count; i++)
{
foreach (DataGridColumn column in dataGrid.Columns)
{
if (column.DisplayIndex == dataGrid.SelectedCells[i].Column.DisplayIndex)
{
if (!selectedColumnIndexes.Contains(column.DisplayIndex))
{
selectedColumnIndexes.Add(column.DisplayIndex);
}
}
}
}
Thus you will have a list of all the indexes of the columns currently selected. This question gives some nice clues in what direction to head here.
Obviously, if you want just the number of columns actually selected, then that value is simply selectedColumnIndexes.Count after the for loops have run through.

Assigning values to array of 'Combo Boxes' in a 'Group Box' using for each loop in c#

I have 10 comboBox in a groupBox
for I just want to display a calculated value in respective comboBox like this say if I set a varible double i=08.00; then on button click cmboBox should display values like this
CB1-08.00
CB2-09.50
CB3-10.00
CB4-10.50
CB5-11.00
CB6-11.50
.... and so on upto CB10 But I am getting output like this
And Code
private void button1_Click(object sender, EventArgs e)
{
double i=08.00;
foreach (var comboBox in groupBox1.Controls.OfType<ComboBox>())
{
comboBox.Text = i.ToString("00.00");
i = i + 0.5;
}
}
Your combobox order is different in the collection so it inserts the numbers randomly. May be you can name your combobox for instance like cmb1,cmb2,cmb3 etc. and if you update your code it will run.
Your controls in the Controls collection are not sorted by their appearance on the form. You will need to find a way to sort them if you need different values in each based on their position.
Foreach loop doesn't give the collection in the order you wanted. The way to go forward is to give a tag id to each combo box, then you can use that to assign a value to them them.
So your first combo box will start with tag id 0, and the last one will have 8,
double val = 08.00;
for (int i = 0; i < groupBox1.Controls.Count; ++i)
{
var combobox = groupBox1.Controls[i] as ComboBox;
int tag = int.Parse(combobox.Tag.ToString());
double value = val + (0.5 * tag);
combobox.Text = value.ToString("00.00");
}
Make sure you tag the cobbo box in the order you wanted them.

Delete selected cells content in WPF DataGrid

I have a DataGrid bound to a matrix.
The main logic of the application is that any cell having a value of 0 will just display nothing (0 = no value in my referential).
Now what I want is that, when the user presses the Delete button, all the cells selected will be set to 0.
I can easily do that with one cell (because I keep track of the current row/column selected), but I can't get, let's say, a 2x2 square correctly.
The best way I figured out, using Binding magic, would be to get the row/column indexes of each cell selected, and then set the Binding's source to 0. But still, I can't have all of the row numbers (basically because grid.SelectedCells is an IEnumerable<DataGridCellInfo>, and I can only get the column with this object, I have no way to get the current row.)
It works when I only select one cell(or one row), by using the following:
DataGridRow dataGridRow = (DataGridRow)this.ItemContainerGenerator.ContainerFromItem(grid.CurrentItem);
How about multiple selection?
Any ideas here? Thanks!
Try this to get to the selected cells value:
DataGridCellInfo cell = dataGrid1.SelectedCells[0];
((TextBlock) cell.Column.GetCellContent(cell.Item)).Text = "";
Counting that the cells are textblocks.
foreach (DataGridCellInfo cellInfo in grid.SelectedCells)
{
DataGridColumn column = cellInfo.Column;
string propertyName = ((Binding)column.ClipboardContentBinding).Path.Path;
PropertyInfo pi = cellInfo.Item.GetType().GetProperty(propertyName);
if (pi != null)
{
pi.SetValue(cellInfo.Item, null, null);
}
}
ICollectionView cv = CollectionViewSource.GetDefaultView(grid.Items);
cv.Refresh();

How to change the focus from one cell to the another cell on the next row and the same column

I want to change the row selection from one row to next row and the focus from one cell to the another cell on the the same column.
that is, 2nd row is selected in my datagrid and the focus is currently on the 2nd row 3rd column.
when i click a button on my screen, 3rd must get selected and the focus must be on the 3rd row 3rd column.
now if i type something then it must automatically reflect on the 3rd row 3rd column.
Thanks in advance!
Try this little helper method for selecting a cell based on a column and a row item
private static void SelectCell(DataGrid dataGrid, DataGridColumn column, Object rowItem) {
if (rowItem != null) {
//scroll the item into view
dataGrid.ScrollIntoView(rowItem);
dataGrid.ScrollIntoView(rowItem, column);
//get the cell info
DataGridCellInfo cellInfo = new DataGridCellInfo(rowItem, column);
if (dataGrid.CurrentCell.Item == cellInfo.Item && dataGrid.CurrentCell.Column == cellInfo.Column) { }
else {
dataGrid.Focus();
dataGrid.CurrentCell = cellInfo;
//set the cell to be selected
dataGrid.SelectedCells.Clear();
dataGrid.SelectedCells.Add(cellInfo);
}
}
}
The following code will solve the issue.
_dgTemp.CommitEdit();
object SelectedItem = _dgTemp.SelectedItem;
DataGridRow _dgRow = DataGridHelper.GetRow(_dgTemp, _dgTemp.Items.IndexOf(SelectedItem));
DataGridCell _dgCell = DataGridHelper.GetCell(_dgTemp, _dgRow, _dgTemp.Columns.IndexOf(_dgTemp.CurrentColumn));
_dgCell.MoveFocus(new TraversalRequest(FocusNavigationDirection.Down));
_dgTemp.ScrollIntoView(_dgTemp.Items.IndexOf(_dgTemp.CurrentItem));
_dgTemp.UpdateLayout();
_dgTemp.SelectedIndex = _dgTemp.Items.IndexOf(_dgTemp.CurrentItem);

Resources