GridView动态显示隐藏列,并保存客户的配置
Devpress GridView 没有RowCreated事件,无法用自带控件自定义选中数据列的显示
这里选择带有CheckedList属性的控件,可选CheckedListBoxControl,CheckedListBox,CheckedComboBoxEdit
这里我用的控件时CheckedComboBoxEdit
主要控件:RibbonForm,GridControl 的GridView(GridVIew1),CheckedComboBoxEdit(displayBox)
方法步骤:
1.窗体加载事件(Load)时遍历GridView
读取选中GridView所有列的列名(Caption)和绝对索引(AbsoluteIndex)
Colums存在四个索引属性,其中VisibleIndex会在这条列被隐藏后变为-1,所以不适合使用
放入集合中 Dictionary<string, int> cols = new Dictionary<string, int>();
Dictionary<string, int> cols = new Dictionary<string, int>();
for (int x = 0; x < gridView1.Columns.Count; x++)
{
string cap = gridView1.Columns[x].Caption.ToString();
int field = gridView1.Columns[x].AbsoluteIndex+1;
cols.Add(cap, field);
displayBox.Properties.Items.Add(cap);
}
同时将列标题存入下拉多选控件中
2.设置下拉多选框的EditValueChanged事件
在选中需要显示的数据列后触发displayBox_EditValueChanged事件
获取多选项的文本值(displayBox.EditValue),"公司名称, 个人税号, 部门, 车间"
这里存在 ', ' 这个符号,逗号后有空字符,需要用replace'方法转换下方便切割
再存入集合中
string str=displayBox.EditValue.ToString().Replace(", ","-");
List<string> colList = str.Split(new Char[] { '-' }, StringSplitOptions.RemoveEmptyEntries).ToList();
注:再初始化加载时因为多选框会一直加入数据列列名所以会一直触发这个事件,需要设置变量控制这个事件再初始化时触发
获取需要显示的数据列后重新设置显示列,这里的cols[item]就是之前设置的绝对索引,
设置显示后显示坐标VisibleIndex是被打乱的不方便查看,也要重新按绝对索引的值设置
for (int i = 0; i < gridView1.Columns.Count; i++)
{
gridView1.Columns[i].Visible = false;
}
colList.ForEach(item =>
{
if (cols.ContainsKey(item))
{
gridView1.Columns[cols[item]].Visible = true;
gridView1.Columns[cols[item]].VisibleIndex = cols[item];
}
});
3.保存和读取设置
private void saveLayout()
{
string path = Path.GetDirectoryName(Application.ExecutablePath) + "\\LayoutStyle\\";
string file = this.Name + ".xml";
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
gridControl1.MainView.SaveLayoutToXml(path + file);
}
private void getLayout()
{
string path = Path.GetDirectoryName(Application.ExecutablePath) + "\\LayoutStyle\\";
string file = this.Name + ".xml";
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
if (!File.Exists(path + file))
return;
gridControl1.MainView.RestoreLayoutFromXml(path + file);
}
private void Frm_ryxx_FormClosed(object sender, FormClosedEventArgs e)
{
saveLayout();
}
GridView获取变量名和表头名实现动态导入excel
string filepath = dialog.FileName.ToString();
//导入设置字典
Dictionary<string, string> exconfig = new Dictionary<string, string>();
Workbook workbook = new Workbook();
workbook.LoadDocument(filepath);
var sheets = workbook.Worksheets.ToArray(); ;
Worksheet sheet = sheets[0];
//根据Excel管理中确定的标题行titlenum 读取所有需要导入的标题名存入dt中
var col = sheet.Rows[0].Where(k => k.Value.ToString().Trim() != "").ToList();
//获取所有列表名和参数名字典
Dictionary<string, string> cols = new Dictionary<string, string>();
for (int x = 0; x < gridView1.Columns.Count; x++)
{
string cap=gridView1.Columns[x].Caption.ToString();
string field = gridView1.Columns[x].FieldName.ToString();
cols.Add(cap, field);
}
for (int i = 0; i < col.Count; i++)
{
if (cols.ContainsKey(col[i].Value.ToString()))
{
string str = cols[col[i].Value.ToString()];
exconfig.Add(i.ToString(), str);
}
}