I have OnMouseMove event, during which I want to find a value of certain cell (not neccesarily the one under the mouse). Basically the question is:
How to access cell data using its x and y coordinates without selecting it, changing focus etc?
Tofig, you can use the MouseCoord procedure to get the current row and col, but to show the value of the pos [Col,Row] you must set the DataLink.ActiveRecord property to the Row value and create a new class descendent to access the protected property.
check this code
type
THackGrid = class(TCustomDBGrid); //Create a new class to access the protected properties
procedure TForm1.DBGrid1MouseMove(Sender: TObject; Shift: TShiftState; X,
Y: Integer);
var
Cell : TGridCoord;
Row,Col : integer;
OrigActiveRecord : integer;
begin
inherited;
Cell:=DBGrid1.MouseCoord(X,Y);
Col:= Cell.X;
Row:= Cell.Y;
if dgTitles in DBGrid1.Options then Dec(Row); //if the titles are shown then adjust Row index (-1);
if dgIndicator in DBGrid1.Options then Dec(Col); //if the indicator is shown then adjust the Column index (-1);
if THackGrid(DBGrid1).DataLink.Active and (Row>=0) and (Col>=0) then
begin
OrigActiveRecord:=THackGrid(DBGrid1).DataLink.ActiveRecord; //save the original index
try
THackGrid(DBGrid1).DataLink.ActiveRecord:= Row;
Label1.Caption:=DBGrid1.Columns[Col].Field.AsString; //show the current value in a tlabel
finally
THackGrid(DBGrid1).DataLink.ActiveRecord:= OrigActiveRecord; //restore the index
end;
end;
end;
Related
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 ");
}
}
I have requirement to sort group summary field.
Ex. I have 3 columns in the grid.
Step 1 : I have group by Id by dragging the Id column in Group by area.
Step 2: Add Sum,Count,Average on column.
Now i want to sort sum or count or average by clicking on that ,so that the whole grouped is sorted by sum like 100,200,300.
please help
The sort order is controlled by the GroupByComparer of the FieldSettings class and this can be accomplished by creating a custom IComparer for the field that is grouped. Note that grouping is actually also a sort so I am going to assume that you still want the default sort to happen when the column is first grouped.
In the following example group by records can be sorted by a single summary result when it is clicked on. This was accomplished by using a custom IComparer for the groups that sorts by the value of the tag if it is set and if not set falls back to the value of the group by record:
public class SummarySortComparer : IComparer
{
public int Compare(object x, object y)
{
GroupByRecord xRecord = x as GroupByRecord;
GroupByRecord yRecord = y as GroupByRecord;
IComparable xValue = xRecord.Value as IComparable;
object yValue = yRecord.Value;
if (xRecord.Tag != null)
{
xValue = xRecord.Tag as IComparable;
yValue = yRecord.Tag;
}
return xValue.CompareTo(yValue);
}
}
This is set on the grid using the following:
this.XamDataGrid1.FieldSettings.GroupByComparer = new SummarySortComparer();
Use the PreviewMouseLeftButtonDown of the grid to get the summary that was clicked on if there was one and set the tag of the group by records to be the value of that summary and refresh the sort of the grid:
void XamDataGrid1_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
SummaryResultPresenter summaryResultPresenter =
Utilities.GetAncestorFromType(e.OriginalSource as DependencyObject, typeof (SummaryResultPresenter), false) as
SummaryResultPresenter;
if (summaryResultPresenter != null)
{
GroupBySummariesPresenter groupBySummariesPresenter =
Utilities.GetAncestorFromType(summaryResultPresenter,
typeof(GroupBySummariesPresenter), false) as GroupBySummariesPresenter;
if (groupBySummariesPresenter != null)
{
SummaryResult summaryResult = summaryResultPresenter.SummaryResult;
int summaryResultIndex = summaryResult.ParentCollection.IndexOf(summaryResult);
foreach (GroupByRecord groupRecord in groupBySummariesPresenter.GroupByRecord.ParentCollection)
{
groupRecord.Tag = groupRecord.ChildRecords.SummaryResults[summaryResultIndex].Value;
}
this.XamDataGrid1.Records.RefreshSort();
}
}
}
Note that there are a few limitations in this example in that I haven't implemented any way to clear what summary is sorted so that is something that if desired would still need to be implemented by you. I also didn't include logic to change the sort direction and used the direction that the field is currently sorted by so if you also want to update the direction this will need to be added as well.
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.
I am trying edit the header cell value when using a grid view in virtual mode, but I can't get it working.
In my CellValueNeeded event I have the following code:
grvResults.Rows[e.RowIndex].HeaderCell.Value = record.RecordNumber;
Unfortunately, my header cells are still displaying blank.
How can I get the row header values to display? The end result is I need my rows to show what row number they are, to better keep track of data.
Update: The full event code is below:
private void grvResults_CellValueNeeded(object sender, DataGridViewCellValueEventArgs e)
{
const int BATCH_REQUEST_SIZE = 50;
int row = e.RowIndex + 1;
LogRecord record = null;
// Check if this record for this row is cached
if (!_recordCache.ContainsKey(row))
{
// Retrieve the requested record + a batch more to speed up operations and lessen queries
int page = (row / BATCH_REQUEST_SIZE) + 1;
var records = _currentStore.GetFilteredRecords(_filters, _currentSessionId, BATCH_REQUEST_SIZE, page);
if (records.Count > 0)
{
// Add all the records to the cache
for (int x = 0; x < records.Count; x++)
if (!_recordCache.ContainsKey(row + x))
_recordCache.Add(row + x, records[x]);
}
}
// Get the record from the cache
record = _recordCache[row];
if (record != null)
{
// Set the header cell to the record number
grvResults.Rows[e.RowIndex].HeaderCell.Value = record.RecordNumber;
// Match the cell to the field
string cellFieldName = grvResults.Columns[e.ColumnIndex].Name;
if (record.Fields.Any(x => x.FieldName.Equals(cellFieldName, StringComparison.CurrentCultureIgnoreCase)))
e.Value = record.Fields.Where(x => x.FieldName.Equals(cellFieldName, StringComparison.CurrentCultureIgnoreCase)).Single().StringValue;
}
}
In the brief experiments I've done the HeaderCell.Value always shows in the row header, regardless of where it was set or whether the grid was in virtual mode.
One possibility is that the row header is too narrow to see the value.
Another is that RecordNumber is not a string - the Value displayed in the row header must be a string.
Same thing was happening to me, I noticed I was applying the header to the new row and then overwriting it with a row with values but with no header, so just try this:
grvResults.Rows[e.RowIndex-1].HeaderCell.Value = record.RecordNumber;
(rowindex -1)
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();