private void ColumnsSettingToolStripMenuItem_Click(object sender, EventArgs e) { IList <ColumnInfoVo> clist = new List <ColumnInfoVo>(); foreach (DataGridViewColumn col in dataGridView1.Columns) { ColumnInfoVo vo = new ColumnInfoVo(); vo.Columnheadertext = col.HeaderText; vo.Columndisplayindex = col.DisplayIndex; vo.Columnname = col.Name; vo.Columnwidth = col.Width; vo.Columnvisible = col.Visible; clist.Add(vo); } FrmColumnsSetting f = new FrmColumnsSetting(clist); DialogResult r = f.ShowDialog(this); if (r.CompareTo(DialogResult.OK) == 0) { string filepath = "temp\\" + this.Name + ".xml"; if (System.IO.File.Exists(filepath)) { File.Delete(filepath); CreateXml(clist, filepath); } else { //创建列显示定义XML文件 CreateXml(clist, filepath); } } }
private void SetColumnAttribute(ColumnInfoVo vo) { dataGridView1.Columns[vo.Columnname].Visible = vo.Columnvisible; dataGridView1.Columns[vo.Columnname].Width = vo.Columnwidth; dataGridView1.Columns[vo.Columnname].DisplayIndex = vo.Columndisplayindex; dataGridView1.Columns[vo.Columnname].HeaderText = vo.Columnheadertext; }
private void ParseXml(string filepath) { XmlReader reader = new XmlTextReader(filepath); XmlDocument doc = new XmlDocument(); doc.Load(reader); XmlNode root = doc.DocumentElement; XmlNodeList columnlist = root.SelectNodes("Column"); foreach (XmlNode columnnode in columnlist) { ColumnInfoVo vo = new ColumnInfoVo(); string headertext = columnnode.SelectSingleNode("ColumnHeaderText").InnerText; string displayindex = columnnode.SelectSingleNode("ColumnDisplayIndex").InnerText; string columnname = columnnode.SelectSingleNode("ColumnName").InnerText; string columnwidth = columnnode.SelectSingleNode("ColumnWidth").InnerText; string columnvisible = columnnode.SelectSingleNode("ColumnVisible").InnerText; vo.Columnheadertext = headertext; vo.Columndisplayindex = Convert.ToInt32(displayindex); vo.Columnname = columnname; vo.Columnwidth = Convert.ToInt32(columnwidth); vo.Columnvisible = Convert.ToBoolean(columnvisible); SetColumnAttribute(vo); } reader.Close(); }