How can 'Group By' on a ultrawingrid column? - c#

On the control appears a feature saying 'Drag a column header to gorup by that column'. Can i do that programmatically. Are there any properties or do I need to embed SQL statements?
Thanks,
Sun

You need to add the column to the SortedColumns collection in the band:
private void SwitchGroupByFor(string key) //key stands for the ultragridcolumn key
{
var band = grid.DisplayLayout.Bands[0];
var sortedColumns = band.SortedColumns;
sortedColumns.Add(key, false, true); //last flag indicates is a group by column
}
hth

Take a look here :
http://forums.infragistics.com/forums/p/2418/15231.aspx#15231
and here :
http://forums.infragistics.com/forums/t/5928.aspx
These lines are the one that do the magic :
grid1.DisplayLayout.ViewType = ViewType.OutlookGroupBy;
grid1.Rows.Band.Columns[0].IsGroupByColumn = true;
grid1.Rows.Band.Expandable = Expandable.Yes;

Just thought I'd also note that if you want to clear the group by here is how:
myGrid.Rows.Band.SortedColumns.Clear()

Related

C# DataGridView columnindex and visual display

There is a datagridview table, c# language.
Consisting, for example, of 6 columns. It is necessary to add the seventh, but not to the end of the table, but to the third place.
But because of this, you will have to rewrite a lot of code, because the column indexes will shift.
I'm interested in the question: is it possible to visually display a column in the third position, and refer to it as 7?*
*I understand that indexes start from zero and will be 2 and 6 respectively.
Screens:
Table columns
Visual display
Your question is: Is it possible to visually display a column in the third position, and refer to it as 7? and the answer is yes.
As a suggestion, what works best for me is to not deal with the int index at all, but to use the string indexer of Datagridview.Columns and use datagridview.Columns["columnName"] to refer to individual columns. This solves the problem of having to rewrite code when the index changes from an insert (as you mentioned) because you refer to columns by name.
Sometimes you know what all the possible column names might be. In this case, you can make this even more robust by abstracting them out to an enum:
enum StdColumnName
{
Apple0,
Orange1,
Banana2,
}
Example GitHub
Override this method in MainForm:
protected override void OnHandleCreated(EventArgs e)
{
base.OnHandleCreated(e);
foreach (string name in Enum.GetNames(typeof(StdColumnName)))
{
var col = new DataGridViewTextBoxColumn()
{
Name = name,
HeaderText = name,
AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill
};
dataGridView.Columns.Add(col);
}
}
to get this:
then click the button:
private void btnChangeOrder_Click(object sender, EventArgs e)
{
var col = dataGridView.Columns[nameof(StdColumnName.Banana2)];
col.DisplayIndex = 0;
Debug.WriteLine(
$"The Banana2.Index is { dataGridView.Columns[nameof(StdColumnName.Banana2)].Index}");
Debug.WriteLine(
$"but Banana2.DisplayIndex is { dataGridView.Columns[nameof(StdColumnName.Banana2)].DisplayIndex}");
}
to get this:
Now col.Index and col.DisplayIndex are two different numbers but using the name still works.
Hope this give you a few ideas.

Ultragrid not showing a ValueList for a certain column

I have a User Control with an Ultragrid. In a particular form where I add a ValueList. The value list will not show for the particular column I'm interested in. If I then code in another column instead by changing the index value for columns I get the value list in the column.
The code looks like:
private void AddCombo(object sender, UcUltraGen.RowClickArgs e)
{
ValueList vl;
if (!ucUltraGridMain.Grid.DisplayLayout.ValueLists.Exists("Texas"))
{
vl = ucUltraGridMain.Grid.DisplayLayout.ValueLists.Add("Texas");
}
else
{
vl = ucUltraGridMain.Grid.DisplayLayout.ValueLists["Texas"];
}
var row = e.VariantRow;
List<PcBase> list = PcBase.PcBaseList.Where(x => x.VariantId == row.Cells["Id"].Text).ToList();
AddValueList(list, vl);
ucUltraGridMain.Grid.DisplayLayout.Bands[0].Columns[1].ValueList =
ucUltraGridMain.Grid.DisplayLayout.ValueLists["Texas"];
And if I change to
...
ucUltraGridMain.Grid.DisplayLayout.Bands[0].Columns[2]
It works. How can I have changed the behavior of columns[1]?
The property in column[1] was read only that is it only had a "get" implemented. By adding a set it worked. Hope this helps someone.

Grid column contains int64 values but filter shows strings and doesn't work/

We've got problem with filtering for some of our columns in devexpress gridcontrol. We add the column dynamically (bound-type column) to the grid. The values from the source objects are long type. In the cells it seems that values are fine (since they're aligned to the right without any custom formating on our side) however in filter popup values behave like strings.
For example data set like 1,2,5,11,22,37 the filter list is sorted like 1,11,2,22,5,37 (just like strings) and when we choose one of the available values the filtering does not work (i mean, grid becames empty). Even filters like "Non empty cells" does not work, but when we choose "empty cells" only few from few thousand rows are shown even if most of the cells have no values.
It is important to point out that only dynamically added columns behave that way, the few columns we create every time our module runs work as intended.
The data source is a container (List like).
We're using DevExpress 13.2.
Example of creating 'custom column'
void CreateColumn(GridColumn gridColumn, string fieldName = null, string caption = null, bool visible = true,
bool readOnly = true, UnboundColumnType unboundType = UnboundColumnType.Bound,
int columnWidth = int.MinValue, int minColumnWidth = int.MinValue)
{
gridColumn.Caption = caption;
if (fieldName != null)
gridColumn.FieldName = fieldName;
gridColumn.Visible = visible;
gridColumn.OptionsColumn.ReadOnly = readOnly;
gridColumn.OptionsColumn.AllowEdit = !readOnly;
gridColumn.UnboundType = unboundType;
gridColumn.OptionsFilter.AllowAutoFilter = true;
gridColumn.FilterMode = ColumnFilterMode.Value;
gridColumn.OptionsFilter.AutoFilterCondition = DevExpress.XtraGrid.Columns.AutoFilterCondition.Contains;
if (columnWidth != int.MinValue)
{
gridColumn.Width = columnWidth;
gridColumn.OptionsColumn.FixedWidth = true;
}
if (minColumnWidth != int.MinValue)
gridColumn.MinWidth = minColumnWidth;
}
GridColumn gridColumn = new GridColumn();
CreateColumn(gridColumn, "someName", "someCaption", true, true);
View.Columns.Add(newGridColumn);
That's how it goes in our code (striped most of not related code just to give example process).
#edit
There's invalid cast exception when we add filter like this:
ColumnFilterInfo filter = GetFilter(); //cant really post code of this
ourGrid.MainView.ActiveFilter.Add(column, filter); // VS points here
Unfortunately it doesnt say what and where (except some 'lambda expression') exception is being thrown.
Of course column is the column mentioned above.
#edit
I've found new "tip". The FilterItem objects contain strings for sure, however they should contain long values. How can we influence the creation of these or atleast where to check why those are created like that (we dont do it manually)?
#Edit 19.11.2015
Alright, I had some breakthrough. The columns ('custom') thanks to our mechanism guess their type just fine. Then only problem is that infact our values which custom columns use are stored in Dictionary<string,object>-like collection and we think that thanks to PropertyDescriptor type for columns is fine, but for some reason FilterItem objects have Value of string. We belive that's because DX filter mechanism can't really guess type of "object" so it uses simple ToString on it so FilterItem.Value is string type instead column's data type.
How to overcome this?
We've found the solution and the mistake was on our side. Column creation etc is fine. Somewhere deep, someone has changed value types.

devexpress gridView.Rows?

I want to do it with Devexpress extension (gridview) :
string dataInCell = dataGridView1.Rows[i].Cells[j].Value.ToString();
Like :
gridView1.Rows[i].Cells[j]
If you are trying to get the value of a cell in a specefic row, here is how :
a. If you want the value of a cell of the focused row :
view.GetFocusedRowCellValue("fieldName");
b. If you want the cell value of a row knowing his handle :
view.GetRowCellValue(rowHandle, "fieldName");
Good luck
try this
for (int i = 0; i < gridView.RowCount; i++)
{
dataInCell = Convert.ToString(gridView.GetRowCellValue(i, "FieldName"));
}
To get a spescific row you can use these commands.
GridView.GetDataRow(rowHandle)
or
GridView.GetRow(rowHandle)
but if you want to modify a range of cells, it is usually better to go directly at the datasource
You presumably have set a datasource on the grid? If so, use the datasource and access it via its datasource index.
Using row handles could causes issues when the grid is sorted. I recommend...
int dataIndex = gridView.GetDataSourceRowIndex(rowHandle);
var myData = myDataSource[dataIndex];
Provided you're using a generic collection there is no casting involved and this handles grouping and sorting. Of course what is displayed and what is the data may not be the same thing. E.g. If the data is an enumeration. You would display the displayname for this but the value in the datasource is the enum. Normally I need the underlying value instead of the displayed text.
you can use below code:
dataGridView1.GetRowValues(dataGridView1.FocusedRowIndex,"column1-name","column2-name",...);
with this you can get value with row index that focused on it and select with field`s name,return value type of object and you cast to int,string and ...
such :
string id=(string)dataGridView1.GetRowValues(dataGridView1.FocusedRowIndex,"column1-name");
but it depends to type column1-name
string dataInCell = ((DataRowView)gridControl1.MainView.GetRow(i)).Row.ItemArray[j].ToString();
I think you are looking for this:
string str = gridView1.GetRowCellValue(Convert.ToInt32("ROW_NUMBER"), "COLUMN_NAME").ToString();
you should use GetRowCellValue
string cellValue;
cellValue = gridView1.GetRowCellValue(2, "ID").ToString();
All above step you can, but keep in mind that null values conversation will be thrown an error, so before access it do Convert.IsDBNull().
I think it's the best code for get row column field.
string name= gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "name").ToString(),
You can get the value of grid cell using
string cellValue = gvGrid.GetRowValues(visibleIndex, "FieldName").ToString();
where visibleIndex is the row's index. You can just loop like this
if (gvGrid.VisibleRowCount > 0)
{
for (int index = 0; index < gvGrid.VisibleRowCount; index++)
{
string cellValue = gvGrid.GetRowValues(index, "FieldName").ToString();
}
}

Datagridview Ordering of Column Headers- Winform C#

I have a datatable bound to a datagridview. However, the ordering of columns is messed up. I already made a column headers for each field put dataproperty name. I arranged it in the designer view. However, if i run the program column headers doesn't follow my arrangement. =_=. Does anybody know how to solve this....
EDIT::
I've Tried this approach. Is it Okay?
void SortDataGridViewColumns(DataGridView dgv)
{
var list = from DataGridViewColumn c in dgv.Columns
orderby c.Index
select c;
int i = 0;
foreach (DataGridViewColumn c in list)
{
c.DisplayIndex = i++;
}
}
***I've got this here but I use Index instead of Headertext. CASE CLOSED! LOL
I think you wil need to change the column order in runtime.
From MSDN:
When you use a DataGridView to display data from a data source, the columns in the data source's schema sometimes do not appear in the order you would like to display them. You can change the displayed order of the columns by using the DisplayIndex property of the DataGridViewColumn class.
You can change the order of the columns like this:
private void AdjustColumnOrder()
{
customersDataGridView.Columns["CustomerID"].Visible = false;
customersDataGridView.Columns["ContactName"].DisplayIndex = 0;
customersDataGridView.Columns["ContactTitle"].DisplayIndex = 1;
customersDataGridView.Columns["City"].DisplayIndex = 2;
customersDataGridView.Columns["Country"].DisplayIndex = 3;
customersDataGridView.Columns["CompanyName"].DisplayIndex = 4;
}
http://msdn.microsoft.com/en-us/library/wkfe535h.aspx#Y0
If you're going t use it frequently, I recomend you to make use of extension methods to add sintactic sugar and make it easy to read and maintain.

Categories

Resources