How to limit column reorder to a few columns? - c#

In my WPF application, I am using a DataGrid control. I allow the user to reorder columns. However, I have to ensure that the first two columns and the last column cannot be reordered.
All the columns are generated programmatically using new DataGridTextColumn().
I am wondering if someone can guide me on what I need to do to accomplish this? Thank you in advance for your help.

You can use the DataGrid.FrozenColumnCount Property to not allow the users to move columns.
However, if you look at the documentation, you will see that you can only do this for the first x columns from the left side of the DataGrid. For example, if you set the FrozenColumnCount to 2, the two left columns in the display are frozen.

Use DataGridColumn.CanUserReorder property (set tis property to false for columns, which can't be reordered).

Related

ListView; Sort & Highlight columns

I was wondering 2 things.
How can I make it so when I click on a row it selects both columns?
How can I make it so it auto sorts them when they click a column?
If C# doesn't allow this then is there any libs I could use or anything easier? Thanks.
If you mean your listview has 2 columns and you want to select the both of them on row selection, you can just set FullRowSelect=true.
For your second question check this Sorting ListView Items by Column Using Windows Forms

Set the visibility of a column that has been grouped (single column)

I am grouping data in a grid by two different columns.
One of the columns needs to be retained as visible and the other needs to be removed from the grid.
I have set "ShowGroupedColumns" to false as it automatically handles the column chooser etc.
But I cant seem to figure out how to make the one I want to retain visible.
Is there any way, with ShowGroupedColumns set to false that I can have it grouped by column and have the column in the grid too?
Thanks
The same problem have been already discussed in DevExpress Support Center:
Display individual columns, even if they are grouped

Prevent readonly columns from reorder

I have a DataGridView that has three read only columns in it and one or several other writable columns. The users have decided they need to be able to reorder the columns. I need a way to allow them them to reorder the writable columns but not the read only ones. The only thing I have thought of is to check if the column that was just moved in the ColumnDisplayIndexChanged event is read only, check it's name and then move it back to where I want it. I don't like that solution. Does anyone have a more elegant solution to this problem?
If column reordering is enabled, the frozen columns are treated as a group distinct from the unfrozen columns. Users can reposition columns in either group, but they cannot move a column from one group to the other.
Does this sound like what you want?
http://msdn.microsoft.com/en-us/library/28e9w2e1.aspx
You could disable column reordering by default, and only enable it when the mouse cursor is over a column that you want to be moved.
Not that I actually tried this, but it's worth a try.

Customise a bound datagridview

Howdy, using vs2008 winforms.
I want to be able to use a slightly customised datagridview but cant think of a way to do it.
i have
1. a sqldataadaptor fill a dataset
2. a binding source bound to the dataset
3 a datagridview with the bindingsource set as the datasource.
I want the binding to allow sync between the dataset and datagridview, so i can edit data and then update to database with sqldataadaptor. update.
I want to show some custom columns that are calculated results.
And i want to show a final bottom row that is totals of all the columns in the DVG.
My problem is once the DGV is bound i cant add a custom column or row, it wont let me.
I know i could add it directly to the dataset that is the underlying datasource but then by changing the structure of the dataset i cant update to a database once edits have taken place.
or can i ???
Can someone tell me how i can add my custom columns and a final total row to a bound DGV.
Also while im here, if i click on the top of a column to sort on it, in a bound DGV, will it also re-sort the underlying dataset, so i i edit things will still stay synced ?
thanks in advance for any help
Yes, you can.
The adapter does not care about structure. It only cares about column names used in the Select/Insert/Update/Delete commands. You can add custom columns, expression columns, columns for subtotals, columns for totals, or whatever you need. Even change the order of the columns. I do advise adding a sort column, so you can keep the custom rows in the proper place when you or the user sorts.
You can do the same thing for custom rows. When you update through the adapter, you handle the RowUpdating event and set the SqlRowUpdatingEventArgs.Status to SkipCurrentRow for these custom rows. I strongly advise creating a row type column so you know which rows to skip when updating. (You can also use the sort column's value as a key to skip rows.)
There are a couple of articles on MSDN or KB that illustrate how to add total rows.
"How to sum the fields in a Windows Forms DataGrid control and then display the calculated totals in a footer by using Visual Basic .NET" at http://support.microsoft.com/kb/836672.
In my experience, manipulate the data table first before binding. Do what you need to do in order to support the grid and the grid's interface. It is okay to manipulate the data, add/change/remove columns and rows, then rebind when needed.
To be able to add additional unbound columns to your DataGridView you could also set its AutoGenerateColumns property to false and then add custom columns with the following method:
dataGridView->Columns->Add(...)
To calculate the contents of the unbound columns you can handle the CellValueNeeded and CellValuePushed events but you must set the DataGridView into 'virtual mode' by setting the VirtualMode property to true in order for the eventhandlers to be invoked.
Hope this helps a little....

Get DataGridView columns in order of user's ordering

I have an app that lets the user reorder columns in a DataGridView, and I want to save the "layout" upon closing the app...but it seems that when I iterate through the column collection, I get them in the order I added them, not the order they appear on screen. Is there a way to get the displayed ordering?
edit: found it, just check the DisplayIndex property of each column :)
You'll have to use the DataGridViewColumn.DisplayIndex property. http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridviewcolumn.displayindex.aspx

Categories

Resources