Hiding a particular cell based on row and column in flexgrid - c#

I have a flexgrid (flex component grid), how do i hide a cell.
For ex: 2nd row and 5th column - i need to hide/remove based on some condition.
for say
if(C1FlexGrid1.Rows[2][5].ToString().Length <0)
{
//I want this to be invisible.
C1FlexGrid1.Rows[2][5].isVisible=false;
}
There is no propery that supports isVisible the way i have used. Any way i can achieve this ? Thanks.

Finally i figured it out:
Create a ownerdrawcell event for your winforms component grid:
componentGrid.DrawMode = C1.Win.C1FlexGrid.DrawModeEnum.OwnerDraw;
componentGrid.OwnerDrawCell += componentGrid_OwnerDrawCell;
Method
void componentGrid_OwnerDrawCell(object sender, C1.Win.C1FlexGrid.OwnerDrawCellEventArgs e)
{
var value = componentGrid.GetCellCheck(e.Row,e.Col);
//Your custom condition
if (value is bool)
{
//Will hide the cell
e.Style.Display = DisplayEnum.None;
}
else
{
//Will show the cell
e.Style.Display = DisplayEnum.Stack;
}
}

Related

CellValueChanging event does not fire on first change

i have set a CellValueChanging event for devexpress grid view cell in which by changing the value of a cell (which is combobox) the value of other cells changes.
when the one of the items is selected, the cell value changing event fires but noting is shows in the other cells but when another combobox item is selected it works very good.
this also occur for each new row.
how can i handle this?
this is the event:
private void windowBasedMainGridView_CellValueChanging(object sender, CellValueChangedEventArgs e)
{
//if the selected column is not int or dbl, set the descritazation to be automatic
if (e.Column.FieldName == "CriterionParameter")
{
windowBasedMainGridView.SetFocusedRowCellValue("CriterionParameterDiscretization", "Automatic");
string strSelectedColumnName = commonlyUsedMethodsClass.ColumnNameFromColumnCaption(frmMainForm.projectDataSet.Tables[strinputDatatableName], e.Value.ToString());
Type typeOfSelectedParameter = frmMainForm.projectDataSet.Tables[strinputDatatableName].Columns[strSelectedColumnName].DataType;
if ((typeOfSelectedParameter == typeof(double) || typeOfSelectedParameter == typeof(int)))
{
windowBasedMainGridView.SetFocusedRowCellValue("CriterionParameterDesceretazationMethod", "Linear");
//set the min and max of property
if (typeOfSelectedParameter == typeof(double))
{
List<double> valuesList = commonlyUsedMethodsClass.DoubleListGenerator(frmMainForm.projectDataSet.Tables[strinputDatatableName], strSelectedColumnName);
windowBasedMainGridView.SetFocusedRowCellValue("CriterionParameterMinimum", valuesList.Min());
windowBasedMainGridView.SetFocusedRowCellValue("CriterionParameterMaximum", valuesList.Max());
}
else
{
List<int> valuesList = commonlyUsedMethodsClass.IntegerListGenerator(frmMainForm.projectDataSet.Tables[strinputDatatableName], strSelectedColumnName);
windowBasedMainGridView.SetFocusedRowCellValue("CriterionParameterMinimum", valuesList.Min());
windowBasedMainGridView.SetFocusedRowCellValue("CriterionParameterMaximum", valuesList.Max());
}
}
else
{
windowBasedMainGridView.SetFocusedRowCellValue("CriterionParameterDesceretazationMethod", "");
windowBasedMainGridView.SetFocusedRowCellValue("CriterionParameterMinimum", null);
windowBasedMainGridView.SetFocusedRowCellValue("CriterionParameterMaximum", null);
}
}
the picture below show that the item is selected first(the other cells does not fill automatically):
the below picture is when first select another item of combo box and then select the depth item(other cells are filled automatically based on cellvaluechanging event):
Try add this method at the end of your handler:
windowBasedMainGridView.BeginDataUpdate();
windowBasedMainGridView.EndDataUpdate();
OR:
windowBasedMainGridView.RefreshData();
Also, you may need call PostEditor method:
windowBasedMainGridView.PostEditor();

How display some cell without button and some with a button in datagridview

I have a DataGridView and I want in my grid a column having some cells that display a button and some cells that do not contain the button.
In order to solve this problem I've added a DataGridViewButtonColumn and I've written this method that I call to add the rows to my column:
private void AttachNewRow(bool validRow)
{
DataGridViewRow newRow = GetNewRow();
if (validRow)
{
newRow.Cells["Info"].Value = "Click me";
}
else
{
// Here I would like to hide the button in the cell
newRow.Cells["Info"].Value = null;
}
}
The problem is that when I set the cell value to null I receive an exception.
How can I display some of these cells without the button inside?
Thank you
Looks like the GetNewRow() returns the Row you are talking about already inserted in the DGV.
If that function has the knowledge if the 'Info' Column shall contain a Button, it can pass it, maybe in the Tag:
if (somecondiiotn) newRow.Columns["Info"].Tag = "Button";
Then you can write:
private void AttachNewRow()
{
DataGridViewRow newRow = GetNewRow();
if ( newRow.Cells["Info"].Tag == null ||
newRow.Cells["Info"].Tag != "Button") return;
//..
If otoh the code that calls AttachNewRow() has the required knowledge, it could instead pass it on in a parameter:
private void AttachNewRow(bool infoButton)
If the knowlegde is available only later you can still change individual cells.
Update:
since you are now passing the condition into the method you can act accordingly.
I don't know why you get an exception in your code - I don't. But to really hide the Button you should change the cell to be a 'normal' DataGridViewTextBoxCell:
else
{
DataGridViewCell cell = new DataGridViewTextBoxCell();
newRow.Cells["Info"] = cell;
You could try something like this in RowAdded event handler. Here it is assumed that third column is button column:
if (condition)
{
dataGridView1.Rows[e.RowIndex].Cells[2] = new DataGridViewTextBoxCell();
dataGridView1.Rows[e.RowIndex].Cells[2].ReadOnly = true;
}

How to set the focus on the next/previous cell inside a DataGrid? Especially after calling grid.CommitEdit()?

I've a custom combobox template because of some binding stuff that won't work with the 'default' ComboBoxColumn.
To make it look 'nice' I've one template for the edit mode (a Combobox) and one for the 'normal' mode (a Label).
Now, because of that I've to commit the edit made to the combobox manually inside the CellEditEnding event
private bool changeCommitInProgress = false;
private void table_CellEditEnding(object sender, DataGridCellEditEndingEventArgs e)
{
if (e.EditingElement is ContentPresenter && e.EditAction == DataGridEditAction.Commit)
{
if (!changeCommitInProgress)
{
changeCommitInProgress = true;
DataGrid grid = (DataGrid)sender;
grid.CommitEdit(DataGridEditingUnit.Row, false);
changeCommitInProgress = false;
}
}
}
The problem with this is, that it'll remove the focus from the entire datagrid. Just to be on the safe side, these are the only properties I changed on the datagrid (aside from the Name property and the ItemsSource):
grid.AutoGenerateColumns = false;
grid.IsSynchronizedWithCurrentItem = true;
grid.SelectionUnit = DataGridSelectionUnit.Cell;
This is a fun question. I have done similar with a nested DataList where I had to add a new row after last entry and focus on the first textbox of the newly generated row, maybe you can extrapolate my strategy to fit your situation?
protected void calcAvg(object sender, CommandEventArgs e)
{
int row = Convert.ToInt32(e.CommandArgument.ToString()) - 1;
DataListItem ActiveRow = dlMeasurements.Items[row];
// Snipped code doing stuff with current row
// Compare how many rows completed to number of rows requested
if (!(row + 1 == Convert.ToInt32(txtSample.Text)))
{
// Create new row
DataRow drNew = nextMeas.Tables[0].NewRow();
nextMeas.Tables[0].Rows.Add(drNew);
// Change item index and rebind
dlMeasurements.EditItemIndex = row + 1;
dlMeasurements.DataSource = nextMeas.Tables[0];
dlMeasurements.DataBind();
//Set focus with the Script Manager
smInspection.SetFocus((TextBox)(dlMeasurements.Items[row + 1].FindControl("txtRead1")));
}
else
{
// Otherwise close the measurements and show exit button
dlMeasurements.EditItemIndex = -1;
dlMeasurements.DataSource = nextMeas.Tables[0];
dlMeasurements.DataBind();
btnSaveAndPrint.Visible = true;
}
}
}

ObjectListView - Delete a row by clicking on a designated column with fixed content/text

I have a simple question which i am not able to solve myself.
I have an ObjectListView filled with some of my objects. But in addition to that I want to have another column, with a default text "Delete". On clicking that column, the selected Row should be deleted. How do I do that?
You can achieve this by making the desired row editable and use the CellEditActivation event. Initialize your OLV and "delete-column" as follows:
// fire cell edit event on single click
objectListView1.CellEditActivation = ObjectListView.CellEditActivateMode.SingleClick;
objectListView1.CellEditStarting += ObjectListView1OnCellEditStarting;
// enable cell edit and always set cell text to "Delete"
deleteColumn.IsEditable = true;
deleteColumn.AspectGetter = delegate {
return "Delete";
};
Then you can remove the row in the CellEditStarting handler as soon as the column is clicked:
private void ObjectListView1OnCellEditStarting(object sender, CellEditEventArgs e) {
// special cell edit handling for our delete-row
if (e.Column == deleteColumn) {
e.Cancel = true; // we don't want to edit anything
objectListView1.RemoveObject(e.RowObject); // remove object
}
}
To improve on this, you can display an image in addition to the text.
// assign an ImageList containing at least one image to SmallImageList
objectListView1.SmallImageList = imageList1;
// always display image from index 0 as default image for deleteColumn
deleteColumn.ImageGetter = delegate {
return 0;
};
Result:
If you don't want to display any text next to the image you can use
deleteColumn.AspectToStringConverter = delegate {
return String.Empty;
};
You could also set the Aspect to an empty string, but consider this as "best practice". By still returning an aspect, sorting and grouping will still work.
If the "Delete" column is not the first column in the ObjectListView, you will have to set
ShowImagesOnSubItems = true;
See also ObjectListView show icons.

How to disable a row by selecting value from comboBox in datagridview?

Here I have a column name "Status" in which I am using ComboBox whose values are "Pending" and "Delivered" now I want that when a user selects the delivered from the ComboBox then the entire row or this control should be disabled so user could not change it again and by default its value should be "Pending" how can i do it? Is it possible to disable a single row in gridview?
You cannot disable an individual row. However you can make it readonly:
DataGridView1.Rows[rowIndex].ReadOnly = true;
(This makes row with index of rowIndex set to readonly).
For your specific example, you would want to handle the CellValueChanged event of the datagridview and have code along the lines of:
void DataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
if (e.ColumnIndex == 4 && DataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString() == "Delivered")
{
DataGridView1.Rows[e.RowIndex].ReadOnly = true;
}
}
You can handle the RowChanged event and do something like
if (Status == 'Delivered')
{
e.Row.Enabled = False;
}

Categories

Resources