我在Windows窗体中有一个datagridview。我需要允许用户重新排序列,然后保存更改permanantly.I设置 myGrid.AllowUserToOrderColumns = true; 但这只会改变设计上的显示索引。重新排序DatagridViews列并以编程方式保存新位置
1
A
回答
1
实体:
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;
}
的序列化代理:
[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;
}
}
}
我Form1中进行测试:
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;
}
}
这将DataPropertyName和的DisplayIndex保存到一个XML文件。通过实施自定义的保存和加载方法,您可以轻松地在必须存储数据的位置进行扩展/修改。
1
这可能会帮助你
公共部分Form1类:表格{
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 { }
}
}
}
相关问题
- 1. 以编程方式重新排列RelativeLayout
- 2. 以编程方式重新排列图
- 3. 以编程方式更新DataGridViews源代码?
- 4. 重新排序SQL位置列
- 5. 以编程方式更改数据后重新排列ng2-google-
- 6. 重新排列(重新排序,排序)阵列,你所希望的方式PHP
- 7. 如何保存重新排列的表格视图位置
- 8. 如何以编程方式更新和保存app.config应用程序设置
- 9. 如何以编程方式重新排序WPF Toolkit数据网格列?
- 10. 以编程方式重新使用IBOutlet
- 11. Magento - 以编程方式重新订购
- 12. 用jQuery重新排列div位置?
- 13. 如何以编程方式重新定位ExpandableSplitter
- 14. 尝试以编程方式重新定位的UIImageView
- 15. 以自定义的方式重新排序阵列
- 16. SortedDictionary项目键位置重新排序
- 17. 内存重新排序:可以使用较早的商店将重新排序的内容重新排列到不同但包含的位置?
- 18. 重新排序列
- 19. 如何在读出DataGrid时保存重新排列的列位置?
- 20. 以编程方式重新启动应用程序?
- 21. 以编程方式重新启动应用程序崩溃
- 22. 以编程方式刷新mapview用户位置
- 23. 以编程方式更新iTunes曲目位置
- 24. 重新复制并重新排序表
- 25. 以编程方式将asp:ListView设置为编辑模式,然后更新并保存
- 26. 断开并重新连接以编程方式显示
- 27. 以编程方式创建元素并重新使用代码
- 28. 如何以编程方式重新排列IIS7中的Handler映射?
- 29. 如何以编程方式重新排列ATFolder子类的子项?
- 30. 如何以编程方式在iOS中的UIStackView中重新排序项目?
请提供更多信息例如:您尝试保存用户希望使用的设计的是什么? – Mitja
我还没有尝试过任何东西。我不知道如何实现this.setting属性的'AllowUserToOrderColumns'为真允许我在运行时更改列索引。但这并没有改变列索引的渗透性 – Anjitha