Scroll goes bottom, but content keeps at top - c#

I am working with an own extended System.Windows.Forms.Datagrid... problem is that when rows are appended, the control does not scroll bottom correctly.
Here is the snippet I use:
if (filasAInsertar.Length > 0)
{
int row_count = niceDataGridDesvios.getVisibleRowsCount(niceDataGridDesvios.Parent) - 1;
ExtendedDataGrid extendedDataGrid = niceDataGridDesvios.dataGrid;
extendedDataGrid.getScrollBar().Value = extendedDataGrid.getScrollBar().Maximum;
niceDataGridDesvios.dataGrid.selectFullRow(row_count);
}
This code makes the scrollbar run bottom, but content keeps on top.... Any idea on how to make it well? Already tried to .performLayout() and .Refresh(), got same results.
Hope you guys could help me

DataGrid
To set the current row of a System.WindowsForms.DataGrid and scroll to the row you can use CurrentRowIndex property:
datGrid1.CurrentRowIndex = 50;
For example to scroll to the last row:
datGrid1.CurrentRowIndex = d.BindingContext[datGrid1.DataSource].Count - 1;
DataGridView
CurrentCell
If you set the CurrentCell of DataGridView it selects the specified cell and scrolls to make the cell visible.
For example to select the last row and scroll to it:
dataGridView1.CurrentCell = dataGridView1.Rows[this.dataGridView1.RowCount - 1].Cells[0];
FirstDisplayedScrollingRowIndex
You can also set FirstDisplayedScrollingRowIndex to scroll to a specific row, but it doesn't select the row:
For example to only scroll to the last row:
dataGridView1.FirstDisplayedScrollingRowIndex = dataGridView1.RowCount-1;

Related

Why will not Grid CurrentRow.index include Grid Line Selection?

If you can, please help me!
I made a line in a grid list. The selection is made apparently. The problem is that this selection does not change the CurrentRow.index. The CurrentRow.Index value was 0 before the selection. There is still 0 left after the selection :-(. How can I solve the selection and the CurrentRow index add the same value as I did?
int row_again = 3;
DataGridView_CONNECT.Rows[row_again].Selected = true;
int gridview_pointer = DataGridView_CONNECT.CurrentRow.Index;
//gridview_pointer = 0(?!)
dataGridView.Rows[index].Selected actually does not selects a row. It sets a value indicating whether the row is selected.
If you would like to select a row programmatically, first, select a cell in that row:
DataGridView_CONNECT.CurrentCell = DataGridView_CONNECT.Rows[row_again].Cells[0];
Then you can update the information that row is selected and access to the CurrentRow.Index.
DataGridView_CONNECT.Rows[row_again].Selected = true;
int gridview_pointer = DataGridView_CONNECT.CurrentRow.Index;

How to validate each cell value when pasting values in DataGridView in Winforms?

I have a DataGridView with 10 rows and 5 columns. When I select a row and paste it in the AddNewRow, the CellValidating event is not triggered for the cells. Below are my code to paste the copied values into the grid.
//Get the starting Cell
DataGridViewCell startCell = GetStartCell(dataGridView1);
//Get the clipboard value in a dictionary
Dictionary<int, Dictionary<int, string>> cbValue =
ClipBoardValues(Clipboard.GetText());
int iRowIndex = startCell.RowIndex;
foreach (int rowKey in cbValue.Keys)
{
int iColIndex = startCell.ColumnIndex;
foreach (int cellKey in cbValue[rowKey].Keys)
{
//Check if the index is within the limit
if (iColIndex <= dataGridView1.Columns.Count - 1
&& iRowIndex <= dataGridView1.Rows.Count - 1)
{
DataGridViewCell cell = dataGridView1[iColIndex, iRowIndex];
//Copy to selected cells if 'chkPasteToSelectedCells' is checked
if (cell.Selected)
cell.Value = cbValue[rowKey][cellKey];
}
iColIndex++;
}
iRowIndex++;
}
Is it the behavior of DataGridView that the cellValidating will not trigger for each cells when pasting the values in the grid?
Can we have trigger the cellValidation event for all the pasted cells while pasting is moved to next cell?
The CellValidating/RowValidating Events depend on the focus.
Remarks
When you change the focus by using the keyboard (TAB, SHIFT+TAB, and so on), by calling the Selector SelectNextControl methods, or by setting the ContainerControl.ActiveControl property to the current form, focus events occur in the following order:
Enter
GotFocus
Leave
Validating
Validated
LostFocus
See:
https://msdn.microsoft.com/en-us/library/system.windows.forms.control.validating(v=vs.110).aspx

DataGridView backcolor of the first row is white although the selection backcolor is transparent

I would like to hide the selection option of my DataGridView so that it seems always like nothing has been selected.
I have set the SelectionBackColor property of my DataGridView to Transparent. But when it loads, the first row's back color is always white although it turns transparent as I select other rows. But at the beginning it is always white.
This is how it looks after loading:
And this is how it looks as I click on another row:
How can I make it so that it always looks like the second picture?
Just deselect the first row after you've filled the grid. By default the first row is selected when you fill it, but it is possible to have no rows at all selected.
DataTable dtb = new DataTable("D");
dtb.Columns.Add("C1");
dtb.Rows.Add("A");
dtb.Rows.Add("B");
dtb.Rows.Add("C");
dtb.Rows.Add("D");
dtb.Rows.Add("E");
dataGridView1.DataSource = dtb;
dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
if (dataGridView1.SelectedRows.Count > 0)
{
dataGridView1.Rows[0].Selected = false;
}

How do I select a column in data grid view? [duplicate]

I've been trying to find out how to select all cells under a Column with a 'mouse right click+menu+Select this Column'...
MSDN isn't helping much...
I get this error when I try to change selection mode:
DataGridView control's SelectionMode cannot be set to FullColumnSelect while it has a column with SortMode set to DataGridViewColumnSortMode.Automatic.
Thanks,
Y_Y
Sorry it took so long - I wanted to test before I answered, so I plopped this into Visual Studio to test first.
I had to do this in mine to get it to work:
foreach (DataGridViewColumn c in dataGridView1.Columns)
{
c.SortMode = DataGridViewColumnSortMode.NotSortable;
c.Selected = false;
}
dataGridView1.SelectionMode = DataGridViewSelectionMode.FullColumnSelect;
dataGridView1.Columns[0].Selected = true;
Loop through the cells in the column and set their Selected property to true.
It sounds horrible, but I believe it's the only way to select an entire column and keep automatic sorting.
For example:
grid.ClearSelection();
for(int r = 0; r < grid.RowCount; r++)
grid[columnIndex, r].Selected = true;
You need 3 things.
Clear all selected rows and cells.
Remove the sort mode of every column to Not sortable. The default click event is sort, now it will be select.
Set the selection mode to column.
Finally you can select the first column to show user the selection mode.
This only have to be done once. The first time you load your form or your datagridview.
// Clear all selected cells or rows in the DGV.
dataGridView1.ClearSelection();
// Make every column not sortable.
for (int i=0; i < dataGridView1.Columns.Count; i++)
dataGridView1.Columns[i].SortMode = DataGridViewColumnSortMode.NotSortable;
// Set selection mode to Column.
dataGridView1.SelectionMode = DataGridViewSelectionMode.FullColumnSelect;
// In case you want the first column selected.
if (dataGridView1.Columns.Count > 0 ) // Check if you have at least one column.
dataGridView1.Columns[0].Selected = true;
I got this error while starting with WPF using the drag and drop interface and none of the manual coding. Viewing the properties of datagrid would give a way to select items like this:
But trying to change to type to Column Header Select or Column Select would result in the error you mentioned.
So how it was solved was by right-clicking on the grid and go to Edit Columns. Here all the columns and their SortingMode is available to change. Change them all to NotSortable.
I know this is a very old question. But I leave my solution below for people who will encounter this error in the future.
You will get this error through properties(UI) in general.
I mean when you do SelectionMode -> FullColumnSelect or ColumnHeaderSelect. You get it. For this reason, I suggest you to change the SelectionMode via code instead of UI.
My solution is as follows.
Give your data to DataGridView as SelectionMode.FullRowSelect or SelectionMode.RowHeaderSelect.
Make all columns not sortable in a loop.
Change the dataGridView's selection mode in code.
//1
dataGridView.DataSource = productList;
//2
for (int i = 0; i < dataGridView.Columns.Count; ++i)
dataGridView.Columns[i].SortMode = DataGridViewColumnSortMode.NotSortable;
//3
dataGridView.SelectionMode = DataGridViewSelectionMode.FullColumnSelect;

XtraGrid whole row is highlighted except the clicked cell

When I select a row in the following GridView, the cell that my mouse is resting on (in other words the cell that I left click on) to select a row, is not highlighted while the rest of the row's cells are all highlighted.
I would appreciate your help.
GridView myView = (GridView)oGrid.MainView;
myView.OptionsSelection.MultiSelect = true;
myView.OptionsSelection.MultiSelectMode = GridMultiSelectMode.RowSelect;
if (myView.RowCount > 0)
{
frmChangeMyStatus ff = new frmChangeMyStatus(ccfrms);
DialogResult dr = ff.ShowDialog();
if (dr == DialogResult.OK)
{
for (int i = 0; i < myView.SelectedRowsCount; i++)
{
row = myView.GetSelectedRows()[i];
//...........
}
}
}
If you want the focused cell to look like any other cell in the focused row, disable focused cell styling in view properties. You can do this in two different ways:
At runtime:
myView.OptionsSelection.EnableAppearanceFocusedCell = false;
At design-time: Invoke XtraGrid designer, select Views :: (your view) :: OptionsSelection :: Set EnableAppearanceFocusedCell to False.
If you have access to XtraGrid designer, you can check out the Appearance section if you need more complicated styling rules.
In addition to what Yuriy Guts said above about the focus cell appearance for the view, if the cell that is selected is editable, it will still not highlight that cell.
So, if the cell doesn't need to be editable, you can set OptionsColumn.AllowEdit = false for that column. Otherwise, if the user selects a row by clicking on a cell, you have to live with that appearance so that the user can tell which cell they are currently editing.

Categories