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;
Related
I have a databound gridview in my winform. I want to know how to get the index of Currently selected rows i.e multiple rows.
I am able to do this with a single row. but is there a way I can have a checkbox or something in which I can index of multiple rows.
The Image below will help u understand better of my requirement.
First set CellContentClick event to your DataGridView.
dataGridView.CellContentClick += new System.Windows.Forms.DataGridViewCellEventHandler(this.onCellContentClick);
For every cell click it will invoke the following method. Here you can create a list and populate it with clicked row index.
public void onCellContentClick(DataGridViewCellEventArgs cell)
{
// Check whether selected cell is check box column, here 0 indicates the check box column.
if (cell.ColumnIndex == 0)
{
bool isChecked = (Boolean) dataGridView[cell.ColumnIndex, cell.RowIndex].EditedFormattedValue;
if(isChecked)
{
// Below will give you the selected cell row index, for multiple rows you can populate those index in list or whatever you convenient with.
cell.RowIndex;
}
}
}
Use DataGridView.SelectedRows property to get all selected rows.
To select multiple rows:
DataGridView.MultiSelect = true;
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;
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.
I am using linq2sql to add a record to my datagridview and refreshing the grid. Right now after refresh it moves the row selection to the top. How would I tell it to highlight the most recent added record?
Not sure if this would help but I have included the code below I am using to find out the pk of the selected row. As what I would like to do is kind of the opposite. Thanks
//get selected row index
int index = this.dataGridView1.CurrentRow.Index;
//get pk of selected row using index
string cellValue = dataGridView1["pkColumn", index].Value.ToString();
//change pk string to int
int pKey = Int32.Parse(cellValue);
If you know in advance that anytime a new item is added, it should appear at the bottom of the list, you could use:
int index = this.dataGridView1.Rows.Count - 1;
this.dataGridView1.Rows[index].Selected = true;
this.dataGridView1.FirstDisplayedScrollingRowIndex = index;
But, if your order the list by other column then you're not going to get the desired results.
I recommend you, is to search for the row-index you need to give the selection to, this could be performed by a function, let's say int SearchRowIndexForPKValue(int pKey), that will perform a look-up within the cell values of the column that contains your PK/ID ("pkColumn") returning the row index. In such way the code above should looks like:
int index = SearchRowIndexForPKValue(pKey);
this.dataGridView1.Rows[index].Selected = true;
this.dataGridView1.FirstDisplayedScrollingRowIndex = index;
EDIT: int SearchRowIndexForPKValue(int pKey) is not an existing built-in method of the DataGridView. You must declare it and implement it in your code; for the body of the method, you can get the code from this post.
i am using master detail grid.when i click on the master grid row with focused row index, it must show the information in detail grid.fr that i have used check box to do that.i have checked checkbox if it is selected or not.but in my context checkbox not used so i have to get through focused row selection.
plz help me i tried for a days on tihis
for (int i = 0; i < ASPxGridView1.VisibleRowCount; i++)
{
object key = ASPxGridView1.GetRowValues(i, "GLOBALID");
if (ASPxGridView1.Selection.IsRowSelected(i))
{
}
}
In your case when you select the row you want to pass the Selected value to your Detailed GridView, you can then get the Selected Row value..
GridView1.SelectedValue // return selected row Data key value