dev 根据datatable动态生成gridview

 /// <summary>
        /// 根据datatable动态生成gridview
        /// </summary>
        /// <param name="dt"> code列是数据源,name列是显示名称</param>
        /// <param name="gv"></param>
        private void GetDynamicView(DataTable dt, GridView gv)
        {
            //gv.IndicatorWidth = 40;//行号列宽度 
            //此处DataTable中有两列:code name
            if (dt != null)
            {
                foreach (DataRow dr in dt.Rows)
                {
                    DevExpress.XtraGrid.Columns.GridColumn cl = new DevExpress.XtraGrid.Columns.GridColumn();
                    cl.Name = dr["Code"].ToString();
                    cl.Caption = dr["Name"].ToString();
                    cl.FieldName = dr["Code"].ToString();
                    cl.OptionsColumn.AllowSize = true;
                    cl.OptionsColumn.ReadOnly = true;
                    cl.OptionsColumn.AllowEdit = false;
                    cl.OptionsColumn.AllowMove = false;
                    cl.OptionsColumn.AllowSort = DevExpress.Utils.DefaultBoolean.False;
                    cl.OptionsColumn.AllowGroup = DevExpress.Utils.DefaultBoolean.False;
                    cl.OptionsFilter.AllowFilter = false;
                    cl.OptionsFilter.AllowAutoFilter = false;
                    cl.Visible = true;                  
                    gv.Columns.Add(cl);
                }
                //gv.BestFitColumns();
                gv.OptionsView.ColumnAutoWidth = true;
                //gv.CustomDrawEmptyForeground += new DevExpress.XtraGrid.Views.Base.CustomDrawEventHandler(this.gridView_CustomDrawEmptyForeground);
                //gv.MouseDown += new System.Windows.Forms.MouseEventHandler(this.gridView_MouseDown);
                //gv.CustomDrawRowIndicator += new DevExpress.XtraGrid.Views.Grid.RowIndicatorCustomDrawEventHandler(this.gridView_CustomDrawRowIndicator);
            }
        }

你可能感兴趣的:(GridView,动态,Datatable,dev,gridcontrol)