WinForm中使用XtraGrid控件,实现在界面中动态修改列显示,列名列宽等(进阶)

来源:互联网 发布:linux增加用户命令 编辑:程序博客网 时间:2024/06/05 02:59

  GridControl  第一次载入的时候,用XML文件记录  GridView  的列的信息。关闭窗体的时候,再次保存列的信息。列的隐藏,固定在弹出窗体中设定。

用到的一些方法入下:
//
根据XML文件保存的信息设置GridView中的列
public static void SetGridView(DevExpress.XtraGrid.Views.Grid.GridView gridView, string xmlFile)
        {
            gridView.Columns.Clear();

            DataSet ds = new DataSet();
            ds.ReadXml(xmlFile);
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                DevExpress.XtraGrid.Columns.GridColumn gridCoulumn = new DevExpress.XtraGrid.Columns.GridColumn();
                gridCoulumn.Caption = ds.Tables[0].Rows[i]["Caption"].ToString();
                gridCoulumn.FieldName = ds.Tables[0].Rows[i]["FieldName"].ToString();
                gridCoulumn.Name = ds.Tables[0].Rows[i]["ColumnName"].ToString();
                gridCoulumn.VisibleIndex = int.Parse(ds.Tables[0].Rows[i]["VisibleIndex"].ToString());
                gridCoulumn.Visible = ds.Tables[0].Rows[i]["Visible"].ToString().ToLower() == "true" ? true : false;
                if (!gridCoulumn.Visible)
                {
                    gridCoulumn.VisibleIndex = -1;
                }
                gridCoulumn.Width = int.Parse(ds.Tables[0].Rows[i]["Width"].ToString());
                switch (ds.Tables[0].Rows[i]["Fixed"].ToString().ToLower())
                {
                    case "none":
                        gridCoulumn.Fixed = DevExpress.XtraGrid.Columns.FixedStyle.None;
                        break;
                    case "left":
                        gridCoulumn.Fixed = DevExpress.XtraGrid.Columns.FixedStyle.Left;
                        break;
                    case "right":
                        gridCoulumn.Fixed = DevExpress.XtraGrid.Columns.FixedStyle.Right;
                        break;
                }
                if (ds.Tables[0].Rows[i]["ColumnEdit"].ToString() == "RepositoryItemLookUpEdit")
                {
                }
                gridCoulumn.OptionsColumn.ReadOnly = true;
                //gridCoulumn.BestFit();

                gridView.Columns.Add(gridCoulumn);
            }
        }

      
  public static void GridStyleShow(DevExpress.XtraGrid.Views.Grid.GridView gridView, string xmlFile)
        {
            gridView.Columns.Clear();

            DataTable dt = new DataTable();
            dt.TableName = "XMLStyle";
            dt.Columns.AddRange(new DataColumn[] {
                    new DataColumn("FieldName", typeof(string)),
                    new DataColumn("Caption", typeof(string)),
                    new DataColumn("ColumnName", typeof(string)),
                    new DataColumn("Visible", typeof(bool)),
                    new DataColumn("VisibleIndex", typeof(int)),
                    new DataColumn("Width", typeof(int)),
                    new DataColumn("Fixed", typeof(string)),
                    new DataColumn("ReadOnly", typeof(bool))
                });

            dt.ReadXml(xmlFile);
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    DevExpress.XtraGrid.Columns.GridColumn gridCoulumn = new DevExpress.XtraGrid.Columns.GridColumn();
                    gridCoulumn.FieldName = dt.Rows[i]["FieldName"].ToString();
                    gridCoulumn.Caption = dt.Rows[i]["Caption"].ToString();
                    gridCoulumn.Name = dt.Rows[i]["ColumnName"].ToString();
                    gridCoulumn.Visible = (bool)dt.Rows[i]["Visible"];
                    gridCoulumn.VisibleIndex = (int)dt.Rows[i]["VisibleIndex"];
                    gridCoulumn.Width = (int)dt.Rows[i]["Width"];

                    switch (dt.Rows[i]["Fixed"].ToString().ToUpper())
                    {
                        case "LEFT":
                            gridCoulumn.Fixed = DevExpress.XtraGrid.Columns.FixedStyle.Left;
                            break;
                        case "RIGHT":
                            gridCoulumn.Fixed = DevExpress.XtraGrid.Columns.FixedStyle.Right;
                            break;
                        case "NONE":
                            gridCoulumn.Fixed = DevExpress.XtraGrid.Columns.FixedStyle.None;
                            break;
                        default:
                            break;
                    }

                    gridCoulumn.OptionsColumn.ReadOnly = (bool)dt.Rows[i]["ReadOnly"];
                    gridView.Columns.Add(gridCoulumn);
                }
        }

        public static void GridStyleSave(DevExpress.XtraGrid.Views.Grid.GridView gridView, string xmlFile)
        {
            DataTable dt = GridStyleGet(gridView, xmlFile);
            DataTableToXML(dt, xmlFile); 
        }

        public static void DataTableToXML(DataTable dt, string xmlFile)
        {
            DataTable dtTemp = TableSort(dt, "VisibleIndex asc");

            System.IO.FileStream fs = new System.IO.FileStream(xmlFile, System.IO.FileMode.Create);
            System.Xml.XmlTextWriter xw = new System.Xml.XmlTextWriter(fs, System.Text.Encoding.Default);
            xw.WriteStartDocument();
            dtTemp.WriteXml(xw);

            xw.Close();
            fs.Close();
        }

        public static DataTable GridStyleGet(DevExpress.XtraGrid.Views.Grid.GridView gridView, string xmlFile)
        {
            DataTable dt = new DataTable();
            dt.TableName = "XMLStyle";
            dt.Columns.AddRange(new DataColumn[] {
                    new DataColumn("FieldName", typeof(string)),
                    new DataColumn("Caption", typeof(string)),
                    new DataColumn("ColumnName", typeof(string)),
                    new DataColumn("Visible", typeof(bool)),
                    new DataColumn("VisibleIndex", typeof(int)),
                    new DataColumn("Width", typeof(int)),
                    new DataColumn("Fixed", typeof(string)),
                    new DataColumn("ReadOnly", typeof(bool))
                });

            for (int i = 0; i < gridView.Columns.Count; i++)
            {
                DataRow dr = dt.NewRow();
                dr["FieldName"] = gridView.Columns[i].FieldName;
                dr["Caption"] = gridView.Columns[i].Caption;
                dr["ColumnName"] = gridView.Columns[i].Name;
                dr["Visible"] = gridView.Columns[i].Visible;
                dr["VisibleIndex"] = gridView.Columns[i].VisibleIndex;
                dr["Width"] = gridView.Columns[i].Width;

                switch (gridView.Columns[i].Fixed)
                {
                    case DevExpress.XtraGrid.Columns.FixedStyle.Left:
                        dr["Fixed"] = "Left";
                        break;
                    case DevExpress.XtraGrid.Columns.FixedStyle.Right:
                        dr["Fixed"] = "Right";
                        break;
                    case DevExpress.XtraGrid.Columns.FixedStyle.None:
                        dr["Fixed"] = "None";
                        break;
                    default:
                        dr["Fixed"] = "None";
                        break;
                }

                dr["ReadOnly"] = gridView.Columns[i].OptionsColumn.ReadOnly;

                dt.Rows.Add(dr);
            }
            return dt;
        }

        public static DataTable GridFixedType()
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("FixShow", typeof(string));
            dt.Columns.Add("FixName", typeof(string));

            DataRow dr1 = dt.NewRow();
            dr1[0] = "
不固定";
            dr1[1] = "None";
            dt.Rows.Add(dr1);

            DataRow dr2 = dt.NewRow();
            dr2[0] = "
左邊";
            dr2[1] = "Left";
            dt.Rows.Add(dr2);

            DataRow dr3 = dt.NewRow();
            dr3[0] = "
右邊";
            dr3[1] = "Right";
            dt.Rows.Add(dr3);

            return dt;
        }

        public static DataTable TableSort(DataTable dt,string sort)
        {
            DataRow[] resultRows= dt.Select(null, sort);
            DataTable tableTemp = dt.Copy();
            tableTemp.Rows.Clear();
            foreach (DataRow dr in resultRows)
            {
                object[] objItem = dr.ItemArray;
                DataRow row = tableTemp.NewRow();
                row.ItemArray = objItem;
                tableTemp.Rows.Add(row);
            }
            return tableTemp;
        }

栏目设置窗体代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace ProcessPay
{
    public partial class FrmColumnSetup : ProcessPay.FrmBase
    {
        public FrmColumnSetup()
        {
            InitializeComponent();
        }

        public delegate void RefreshEventHandler(string strFilter);
        public event RefreshEventHandler RefreshData;

        private DataTable dt;
        private string xmlFile;

        public DataTable DT
        {
            set { dt = value; }
            get { return dt; }
        }

        public string XmlFile
        {
            set { xmlFile = value; }
            get { return xmlFile; }
        }

        private void FrmColumnSetup_Load(object sender, EventArgs e)
        {
            repositoryItemLookUpEdit1.DataSource = Common.GridFixedType();

            gridControl1.DataSource = dt;
        }

        private void gridView1_CellValueChanged(object sender, DevExpress.XtraGrid.Views.Base.CellValueChangedEventArgs e)
        {
            string fieldName = e.Column.FieldName;
            int rowHandle = e.RowHandle;
            if (fieldName == "Visible")
            {
                if ((bool)gridView1.GetRowCellValue(rowHandle, fieldName))
                {
                    int maxIndex = 0;
                    for (int i = 0; i < gridView1.RowCount; i++)
                    {
                        if (int.Parse(gridView1.GetRowCellValue(i, "VisibleIndex").ToString()) > maxIndex)
                        {
                            maxIndex = int.Parse(gridView1.GetRowCellValue(i, "VisibleIndex").ToString());
                        }
                    }
                    gridView1.SetRowCellValue(rowHandle, "VisibleIndex", maxIndex + 1);
                }
                else
                {
                    int visibleIndex = int.Parse(gridView1.GetRowCellValue(rowHandle, "VisibleIndex").ToString());
                    for (int i = 0; i < gridView1.RowCount; i++)
                    {
                        if (int.Parse(gridView1.GetRowCellValue(i, "VisibleIndex").ToString()) > visibleIndex)
                        {
                            int tempIndex = int.Parse(gridView1.GetRowCellValue(i, "VisibleIndex").ToString()) - 1;
                            gridView1.SetRowCellValue(i, "VisibleIndex", tempIndex);
                        }
                    }
                    gridView1.SetRowCellValue(rowHandle, "VisibleIndex", -1);
                }
            }
            gridView1.CloseEditor();
        }

        private void SBSave_Click(object sender, EventArgs e)
        {
            Common.DataTableToXML(dt, xmlFile);
            if (RefreshData != null)
            {
                RefreshData("");
            }
            this.Close();
        }
    }
}

主窗体代码:

private void FrmMain_Load(object sender, EventArgs e)
        {
            BindDataStyle("");
        }

        private void BindDataStyle(string strFilter)
        {
            bool isExist = System.IO.File.Exists(xmlFile);
            if (isExist)
            {
                Common.GridStyleShow(gridView1, xmlFile);
            }

            BindData(strFilter);

            if (!isExist)
            {
                Common.GridStyleSave(gridView1, xmlFile);
            }
        }

        private void BindData(string strFilter)
        {
            DataSet ds = (new ProcessPay.BLL.SetNo()).GetList();
            gridControl1.DataSource = ds.Tables[0];
        }

        private void FrmMain_FormClosing(object sender, FormClosingEventArgs e)
        {
            Common.GridStyleSave(gridView1, xmlFile);
        }
        //
逆ヘ砞竚
        private void barButtonItem11_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            FrmColumnSetup frm = new FrmColumnSetup();

            DataTable dt = Common.GridStyleGet(gridView1, xmlFile);
            frm.DT = dt;
            frm.XmlFile = xmlFile;
            frm.RefreshData += new FrmColumnSetup.RefreshEventHandler(BindDataStyle);
            frm.ShowDialog();
        }