Author Topic: Forms.DataGridView.Column Reorder  (Read 2975 times)

0 Members and 1 Guest are viewing this topic.

Jeff_M

  • King Gator
  • Posts: 4087
  • C3D user & customizer
Forms.DataGridView.Column Reorder
« on: April 18, 2014, 07:12:59 PM »
I'm a bit stumped with this. I have a form with a DataGridView (DGV) which has 18 static columns. I currently allow the user to reorder them to any other location in the DGV and I save the new order for future use. I'm now expanding this DGV to include columns that can vary in number and names. I'd like to still allow the reordering/saving of the original 18 columns, while preventing any additional columns from doing so. Including not allowing any of the original 18 to be placed in columns 18+. What I have thus far been able to do, using the DGV MouseDown event, is determine if the left button has clicked on one of the new columns and, if so, turn off the DGV's AllowUserToOrderColumns property and then turn it back on with the MouseUp event. This works to keep the new ones from being moved around, but I'm not finding a way to prevent the others from being moved into the new columns area.

In summarry, DGV.Columns 0-17 display order should be able to change to any DisplayLocation 0-17. DGV.Columns 18+ display order cannot change at all.

Is what I'm trying to accomplish even possible? A Google search sure hasn't turned up anything like this.

LE3

  • Guest
Re: Forms.DataGridView.Column Reorder
« Reply #1 on: April 18, 2014, 07:40:15 PM »
Perhaps...

Quote
The DataGridView shipped with the framework allows the user to reorder the columns but there is nothing to persist the new column order.

http://www.codeproject.com/Articles/37087/DataGridView-that-Saves-Column-Order-Width-and-Vis

Jeff_M

  • King Gator
  • Posts: 4087
  • C3D user & customizer
Re: Forms.DataGridView.Column Reorder
« Reply #2 on: April 18, 2014, 07:54:02 PM »
Thanks, Luis. That is essentially what I have done up until now with my static 18 column DGV. But once I add new columns, say 5 this time, and change the display of one of the original to be at displayorder 20. Then the next time this form is used in another drawing that may only need the original 18 columns, when restoring the display order for the item at position 20 it will fail since that is now an invalid position. Plus there will also be 5 additional saved positions that cannot be applied since those columns do not exist.

owenwengerd

  • Bull Frog
  • Posts: 451
Re: Forms.DataGridView.Column Reorder
« Reply #3 on: April 18, 2014, 08:42:11 PM »
I think you should consider changing your strategy. Instead of "display order", think of it as "rank". Save a given column's rank ("rank" could be it's current position, or some arbitrary value that expresses an ordering relative to all other columns currently displayed), then upon the next initialization just take whatever columns you happen to have and order them by rank, with columns of equal rank falling back on alphabetic ordering (or whatever works), and columns with no rank getting tacked on the end. Then let the user rank their columns however they choose with no restrictions.

Jeff_M

  • King Gator
  • Posts: 4087
  • C3D user & customizer
Re: Forms.DataGridView.Column Reorder
« Reply #4 on: April 18, 2014, 09:57:05 PM »
Ah, thanks Owen! I shall have a go at this tonight.

LE3

  • Guest
Re: Forms.DataGridView.Column Reorder
« Reply #5 on: April 19, 2014, 07:59:39 PM »
I got the chance to play a little on this, since most of my time I have been out of the u.interfaces... I end up doing this simple test:

In case (Guess) I understood the conditions...

Note: If any of the new columns that it is added via right-click, it is moved over the area where the columns that can be reorder between them, it will move one of this, but once the grid control it is clicked, does the fix, to relocate them if needed.


HTH.-

Code - C#: [Select]
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Drawing;
  4. using System.Windows.Forms;
  5.  
  6. namespace WindowsFormsApplication1
  7. {
  8.     public partial class Form1 : Form
  9.     {
  10.         public Form1()
  11.         {
  12.             InitializeComponent();
  13.             object[] row0 = { "Swamp0", "Swamper", "Swampy", "Swamp", "1000" };
  14.             object[] row1 = { "TEST1", "XXXX", "YYYY", "ZZZZ", "2000" };
  15.             object[] row2 = { "TEST2", "RRRR", "UUUUUUUUUUUUUUUUUUUUUUUUUUUUU", "3000" };
  16.             object[] row3 = { "THIS IS ONLY A TEST", "YYYYYYYYYY", "OOOOOOOOOOOOOOOOOOOOO", "4000" };
  17.             object[] row4 = { "TESTER", "Dog", "Man", "Beatles", "5000" };
  18.             object[] row5 = { "Another Row For Test Here", "Master of Disaster", "GgGgggggggggggg", "88888", "6000" };
  19.             dataGridView1.Rows.Add(row0);
  20.             dataGridView1.Rows.Add(row1);
  21.             dataGridView1.Rows.Add(row2);
  22.             dataGridView1.Rows.Add(row3);
  23.             dataGridView1.Rows.Add(row4);
  24.             dataGridView1.Rows.Add(row5);
  25.             dataGridView1.AllowUserToOrderColumns = true;
  26.             FixedColumns = new Dictionary<DataGridViewColumn, int>();
  27.             NumberOfColumnsAllowedToOrder = dataGridView1.ColumnCount;
  28.             ReorderColumns = new Dictionary<DataGridViewColumn, int>();
  29.             foreach (DataGridViewColumn column in dataGridView1.Columns)
  30.             {
  31.                 ReorderColumns.Add(column, column.Index);
  32.             }
  33.         }
  34.  
  35.         public int NumberOfColumnsAllowedToOrder { get; set; }
  36.  
  37.         public Dictionary<DataGridViewColumn, int> ReorderColumns { get; set; }
  38.  
  39.         public Dictionary<DataGridViewColumn, int> FixedColumns { get; set; }
  40.  
  41.         private void dataGridView1_MouseDown(object sender, MouseEventArgs e)
  42.         {
  43.             if (e.Button != MouseButtons.Right) return;
  44.             var hitTest = dataGridView1.HitTest(e.X, e.Y);          
  45.             var rowIndex = hitTest.RowIndex;
  46.             if (rowIndex != -1) return;
  47.             dataGridView1.ContextMenuStrip = contextMenuStrip1;
  48.             dataGridView1.ContextMenuStrip.Show(dataGridView1, new Point(e.X, e.Y));
  49.         }
  50.  
  51.         private void addColumn_Click(object sender, EventArgs e)
  52.         {
  53.             var columnsCount = dataGridView1.ColumnCount + 1;
  54.             var column = new DataGridViewTextBoxColumn { Name = "Column" + columnsCount };
  55.             dataGridView1.Columns.Add(column);
  56.         }
  57.  
  58.         private void dataGridView1_ColumnAdded(object sender, DataGridViewColumnEventArgs e)
  59.         {
  60.             FixedColumns.Add(e.Column, e.Column.Index);
  61.         }
  62.  
  63.         private void dataGridView1_MouseUp(object sender, MouseEventArgs e)
  64.         {
  65.             foreach (DataGridViewColumn column in dataGridView1.Columns)
  66.             {
  67.                 if (!ReorderColumns.ContainsKey(column)) continue;
  68.                 if (column.DisplayIndex <= NumberOfColumnsAllowedToOrder) continue;
  69.                 int resetIndex;
  70.                 ReorderColumns.TryGetValue(column, out resetIndex);
  71.                 column.DisplayIndex = resetIndex;
  72.             }
  73.             foreach (var fixedLocationsColumn in FixedColumns)
  74.             {
  75.                 fixedLocationsColumn.Key.DisplayIndex = fixedLocationsColumn.Value;
  76.                 fixedLocationsColumn.Key.Frozen = true;
  77.             }
  78.         }
  79.     }
  80. }
  81.  

Jeff_M

  • King Gator
  • Posts: 4087
  • C3D user & customizer
Re: Forms.DataGridView.Column Reorder
« Reply #6 on: April 21, 2014, 09:25:37 AM »
Yes, Luis, this does exactly what I was asking. Thank you!

LE3

  • Guest
Re: Forms.DataGridView.Column Reorder
« Reply #7 on: April 21, 2014, 09:48:30 AM »
Yes, Luis, this does exactly what I was asking. Thank you!

Good.

I did an extension of this control, that handles much better - will post the whole solution - later here or at the stuff place.

Edit: Here it is, have a look at the url link below:
http://www.theswamp.org/index.php?topic=18383.msg518752#msg518752

HTH.-
« Last Edit: April 21, 2014, 11:04:37 AM by LE »