Ich habe eine Datagridview in meinem Windows-Formular. Ich muss den Benutzern erlauben, die Spalten neu anzuordnen und dann die Änderungen dauerhaft speichern. Ich legte myGrid.AllowUserToOrderColumns = true; Dies ändert jedoch nur den Anzeigeindex für das Design.DataGridViews Spalten neu anordnen und die neue Position programmatisch speichern
1
A
Antwort
1
Entity:
public class Customer : INotifyPropertyChanged
{
string _firstname = "";
public string Firstname
{
get { return _firstname; }
set { _firstname = value; OnPropertyChanged("Firstname"); }
}
string _lastname = "";
public string Lastname
{
get { return _lastname; }
set { _lastname = value; OnPropertyChanged("Lastname"); }
}
int _age = 0;
public int Age
{
get { return _age; }
set { _age = value; OnPropertyChanged("Age"); }
}
public Customer()
{
}
protected void OnPropertyChanged(string name)
{
var handler = PropertyChanged;
if (handler != null)
handler(this, new PropertyChangedEventArgs(name));
}
public event PropertyChangedEventHandler PropertyChanged;
}
Der serializable Proxy:
[Serializable]
public class DataGridViewColumnProxy
{
string _name;
public string Name
{
get { return _name; }
set { _name = value; }
}
int _index;
public int Index
{
get { return _index; }
set { _index = value; }
}
public DataGridViewColumnProxy(DataGridViewColumn column)
{
this._name = column.DataPropertyName;
this._index = column.DisplayIndex;
}
public DataGridViewColumnProxy()
{
}
}
[Serializable]
public class DataGridViewColumnCollectionProxy
{
List<DataGridViewColumnProxy> _columns = new List<DataGridViewColumnProxy>();
public List<DataGridViewColumnProxy> Columns
{
get { return _columns; }
set { _columns = value; }
}
public DataGridViewColumnCollectionProxy(DataGridViewColumnCollection columnCollection)
{
foreach (var col in columnCollection)
{
if (col is DataGridViewColumn)
_columns.Add(new DataGridViewColumnProxy((DataGridViewColumn)col));
}
}
public DataGridViewColumnCollectionProxy()
{
}
public void SetColumnOrder(DataGridViewColumnCollection columnCollection)
{
foreach (var col in columnCollection)
if (col is DataGridViewColumn)
{
DataGridViewColumn column = (DataGridViewColumn)col;
DataGridViewColumnProxy proxy = this._columns.FirstOrDefault(p => p.Name == column.DataPropertyName);
if (proxy != null)
column.DisplayIndex = proxy.Index;
}
}
}
Mein Form1 zum Testen:
public partial class Form1 : Form
{
BindingSource _customers = GetCustomerList();
public BindingSource Customers
{
get { return _customers; }
set { _customers = value; }
}
public Form1()
{
InitializeComponent();
dataGridView1.DataSource = Customers;
LoadDataGridOrderFromFile("myDataGrid.xml", dataGridView1.Columns);
}
private static BindingSource GetCustomerList()
{
BindingSource customers = new BindingSource();
customers.Add(new Customer() { Firstname = "John", Lastname = "Doe", Age = 28 });
customers.Add(new Customer() { Firstname = "Joanne", Lastname = "Doe", Age = 25 });
return customers;
}
static object fileAccessLock = new object();
private static void SaveDataGridOrderToFile(string path, DataGridViewColumnCollection colCollection)
{
lock (fileAccessLock)
using (FileStream fs = new FileStream(path, FileMode.Create))
{
XmlSerializer xmlSerializer = new XmlSerializer(typeof(DataGridViewColumnCollectionProxy));
xmlSerializer.Serialize(fs, new DataGridViewColumnCollectionProxy(colCollection));
}
}
private static void LoadDataGridOrderFromFile(string path, DataGridViewColumnCollection colCollection)
{
if (File.Exists(path))
{
lock (fileAccessLock)
using (FileStream fs = new FileStream(path, FileMode.Open))
{
XmlSerializer xmlSerializer = new XmlSerializer(typeof(DataGridViewColumnCollectionProxy));
DataGridViewColumnCollectionProxy proxy = (DataGridViewColumnCollectionProxy)xmlSerializer.Deserialize(fs);
proxy.SetColumnOrder(colCollection);
}
}
}
private void dataGridView1_ColumnDisplayIndexChanged(object sender, DataGridViewColumnEventArgs e)
{
SaveDataGridOrderToFile("myDataGrid.xml", dataGridView1.Columns);
}
private void Form1_Load(object sender, EventArgs e)
{
dataGridView1.ColumnDisplayIndexChanged +=dataGridView1_ColumnDisplayIndexChanged;
}
}
Es wird die DataPropertyName und den Displayindex in eine XML-Datei speichern. Sie können es leicht erweitern/ändern, wo Ihre Daten gespeichert werden müssen, indem Sie Ihre benutzerdefinierten Speicher- und Lade-Methoden implementieren.
1
Sie im Mai dieses Jahres
Hilfe public partial class Form1: Form {
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
m_Grid.AllowUserToOrderColumns = true;
SetDisplayOrder();
}
private void OnFormClosing(object sender, FormClosingEventArgs e)
{
CacheDisplayOrder();
}
private void CacheDisplayOrder()
{
IsolatedStorageFile isoFile =
IsolatedStorageFile.GetUserStoreForAssembly();
using (IsolatedStorageFileStream isoStream = new
IsolatedStorageFileStream("DisplayCache", FileMode.Create,
isoFile))
{
int[] displayIndices =new int[m_Grid.ColumnCount];
for (int i = 0; i < m_Grid.ColumnCount; i++)
{
displayIndices[i] = m_Grid.Columns[i].DisplayIndex;
}
XmlSerializer ser = new XmlSerializer(typeof(int[]));
ser.Serialize(isoStream,displayIndices);
}
}
private void SetDisplayOrder()
{
IsolatedStorageFile isoFile =
IsolatedStorageFile.GetUserStoreForAssembly();
string[] fileNames = isoFile.GetFileNames("*");
bool found = false;
foreach (string fileName in fileNames)
{
if (fileName == "DisplayCache")
found = true;
}
if (!found)
return;
using (IsolatedStorageFileStream isoStream = new
IsolatedStorageFileStream("DisplayCache", FileMode.Open,
isoFile))
{
try
{
XmlSerializer ser = new XmlSerializer(typeof(int[]));
int[] displayIndicies =
(int[])ser.Deserialize(isoStream);
for (int i = 0; i < displayIndicies.Length; i++)
{
m_Grid.Columns[i].DisplayIndex = displayIndicies[i];
}
}
catch { }
}
}
}
Verwandte Themen
- 1. Zwei DataGridViews; Anordnen von Spalten
- 2. Datenrahmen anordnen/neu anordnen und Entfernen von doppelten Spalten
- 3. Flex-Elemente ohne absolute Position neu anordnen
- 4. Spalten in einer Laravel-Migrationsdatei neu anordnen
- 5. Wie Sie Spalten mit Bootstrap neu anordnen
- 6. Refresh UIPageViewController - Seiten neu anordnen und neue Seiten hinzufügen
- 7. reibungslos gleichzeitig neu anordnen und neue Wortsegmente auf schweben
- 8. Wie kann ich meine UINavigationController-Elemente programmatisch neu anordnen?
- 9. Sparse Arrays durch Austauschen von Zeilen und Spalten neu anordnen
- 10. Mit Golang CSV lesen, Spalten neu anordnen und Ergebnis in eine neue CSV mit Concurrency schreiben
- 11. Wie kann ich Spalten in einem DataGridView neu anordnen?
- 12. Reshape und neu anordnen Array
- 13. Hostnamen analysieren und neu anordnen
- 14. DataFrame-Zeilen neu anordnen
- 15. Warum kann ich meine SQL Server-Spalten nicht neu anordnen?
- 16. Jedes sortierbare Plugin, mit dem ich die Widgets neu anordnen und ihren Status speichern kann?
- 17. Ein Array neu anordnen und Element verschieben
- 18. die Position einer programmatisch
- 19. Einen Datenrahmen neu anordnen
- 20. lineare Gleichung neu anordnen
- 21. Benutzerdefinierte Schaltflächen neu anordnen
- 22. XAML-Code neu anordnen?
- 23. Geojson Punkte neu anordnen
- 24. Bootstrap 3: Gestapelte Spalten auf mobilen Bildschirmen neu anordnen
- 25. Können Sie Spalten im Berichtsdesigner (Gebote 2005) neu anordnen
- 26. Spalten in Excel neu anordnen SQL Transorm/Pivot
- 27. Bereiche der Spalten in einer Textdatei neu anordnen
- 28. Android - Ziehen und Ablegen - Liste neu anordnen
- 29. Zellen neu anordnen - Stoppt die Arbeit
- 30. merge und neu anordnen 3 Arrays
Bitte geben Sie weitere Informationen ein Beispiel: Was haben Sie versucht, das Design zu speichern, das der Benutzer verwenden möchte? – Mitja
Ich habe noch nichts probiert. Ich habe keine Ahnung, wie ich das erreichen kann. Wenn ich die Eigenschaft 'AllowUserToOrderColumns' auf 'true' einstellte, änderte ich den Spaltenindex zur Laufzeit. Aber das änderte den Spaltenindex nicht durchgängig – Anjitha