Listview with checkbox: column order changes checkbox position - c#

In C# Windows Forms I have a listview with the CheckBoxes property set to true and the AllowColumnReorder property set to true.
The checkbox for each item is shown at the very left-hand side of the listview inside the first column. If the user now reorders the columns so that another column is the first one of the listview, the checkbox should be shown for this column. So the checkbox should always stay at the very left-hand side of the listview, but it doesn't buy default.
What do I need to do, to always show the checkbox for the leftmost column even if the column order was changed by the user at runtime?

I don't understant.
You put Checkboxes, aside another element into a column?
If yes, why don't you add a column , only for your checkbox, and then set Reorder property to false?
The others columns will always have Reorder property to true, so you can shamble whatever you want, exept the first column.

Related

Retain selected row(s) in a datagrid after sorting

I'm using a WPF datagrid in which multiple records can be selected & it can be sorted (by clicking the column headers).
I want to be able to retain the selected rows after the sort.
Please note I can retain the selected row, the problem arises when the rows to retain are more than 1.
Say for example: I select rows with indices 2 & 3, and then sort on a particular column. After the sort functionality I want to be able to retain the selected rows (old indices-2,3) & new indices - 4,5. Thus, after sorting, the 4th & 5th rows must be selected.
The items source for my datagrid is of type ListCollectionView to which I'm adding sort descriptions when reqd to sort.
Things I have tried:
Tried setting the SelectedItems property. Doesn't work since it is a Read Only property.
Tried setting the IsSelected property of the DataGridRow(s), doesn't work. Though I can see the IsSelected property for the selected rows is set to true, on the screen I do not see the records highlighted.
This one may be vague but I set the selected Index multiple times but this only highlights the record for the last selected index.
Each time after sorting, the last selected record is highlighted. Is there a way by which I can retain the selected rows?
EDIT:
I'm now able to set the selected items of the datagrid using Datagrid.SelectedItems.Add(dataGridRow) or setting the IsSelected property on the datagrid row.
The issue is though the selected items are present, they are not highlighted on the screen. How can I get the rows to be highlighted?
For ex: If 3 records were selected & highlighted before sort, after sort, all records are selected (put breakpoints & checked in Code) but only one record is highlighted.
One may set the selected items of a datagrid by setting the IsSelected property of the row to true.
Or use the SelectedItems.Add() method to add selected items.
I was doing the same but still couldn't see the selected items since, there was another place in the code where the selected items were being modified.
The above solution is tested & works.

Gridview Automatically Selects first Row after a sort

I am working on a master/detail gridview and detailsview in asp.net web forms using an objectdatasource. The details view is displaying extra information about the selected row from the gridview. When I sort the grid, I want the currently selected row, before sort occurs, to remain in the details view after the sort is completed. Instead, the gridview is auto selecting the new first row whenever I sort.
I found a partial solution to this problem. If I set WhateverGridview.SelectedIndex= -1 onsort and the value becomes null. This makes it deselect any rows after a sort. This leaves the details view blank. However, what I want to do is maintain the selected row not nullify it.
So, does anyone have a good way to retain the selected value or prevent the details view from displaying after sort event fires.
Here you need to use the GridView.EnablePersistedSelection property. Set this property to true.
Setting this property to true means that GridView will make sure that the selection of a row is based on data-key values.
By default GridView makes row selections based on index. This is the reason why when you sort, gridview is selecting row based on index and you lose your actual selected row.

How to make a specific Column Uneditable In datagridview?

Using a DataGridView, how can I make a specific column uneditable while the grid view itself has "Allow editing" enabled?
Also, how can I execute an event when the selected index in a ComboBox in the DataGridView has changed? Here, ComboBox is a column type.
Another question is, how can I make the header title aligned to the center? I can't find the appropriate property.
You've got a few questions here.
(1) How can I make a specific column uneditable in DataGridView?
Set the ReadOnly flag on the particular column you want to make uneditable.
dataGridView.Columns["YourColumnName"].ReadOnly = true;
(2) How can I execute an event when the selected index on a ComboBox in the DataGridView changes?
If it's in your DataGridView, it's not a ComboBox; it's a DataGridViewComboBoxColumn. According to MSDN:
Unlike the ComboBox control, the DataGridViewComboBoxCell does not have SelectedIndex and SelectedValue properties. Instead, selecting a value from a drop-down list sets the cell Value property.
This one I'm not familiar with, as I've never tried it myself. It appears you want to subscribe to the EditingControlShowing event and then see if something like this works for you (with a little tweaking).
(3) How can I make the header title align in the center?
Set the HeaderCell.Style.Alignment
dataGridView.Columns["YourColumnName"].HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter;

How can I remove unnecessary columns in a DataGridView?

How do I remove the specific column, from a DataGridView control, that is indicated in the picture?
That particular column is called a Row Header. Its purpose is to allow the user to select the entire row with a single click, and to indicate which row is currently selected.
You can hide it by setting the RowHeadersVisible property of the DataGridView control to False.

Typing text in Combobox placed inside Datagrid

How to allow Combobox placed inside Datagrid to type text.
It just binding the values already stored in the database. but i need to type the value in the Combobox which is not shown in the bound values.
How to make the Combobox as editable column?
You can set ComboBoxStyle to DropDown
Specifies that the list is displayed by clicking the down arrow and that the text portion is editable. This means that the user can enter a new value and is not limited to selecting an existing value in the list.

Categories