dev 根據datatable動態生成gridview

衣舞晨風發表於2015-03-12
 /// <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);
            }
        }


作者:jiankunking 出處:http://blog.csdn.net/jiankunking

相關文章