Exemple #1
0
 public static void SetDataGridViewHead(DataGridView gridView, Dictionary <String, GridColumnModel> configSettings)
 {
     foreach (String key in configSettings.Keys)
     {
         if (configSettings[key].DataType == typeof(String))
         {
             DataGridViewTextBoxColumn col   = new DataGridViewTextBoxColumn();
             GridColumnModel           model = configSettings[key];
             col.DataPropertyName = model.Name;
             col.Name             = model.Name;
             col.HeaderText       = model.HeadName;
             col.ReadOnly         = !model.CanEdit;
             col.Visible          = model.Visible;
             col.Width            = model.Width;
             gridView.Columns.Add(col);
         }
         else if (configSettings[key].DataType == typeof(Decimal))
         {
             DataGridViewTextBoxColumn col   = new DataGridViewTextBoxColumn();
             GridColumnModel           model = configSettings[key];
             col.HeaderText = model.HeadName;
             col.ReadOnly   = !model.CanEdit;
             col.Visible    = model.Visible;
             col.Width      = model.Width;
             gridView.Columns.Add(col);
         }
     }
 }
        private Syncfusion.EJ2.Grids.Grid ConvertGridObject(string gridProperty)
        {
            Syncfusion.EJ2.Grids.Grid GridModel = (Syncfusion.EJ2.Grids.Grid)Newtonsoft.Json.JsonConvert.DeserializeObject(gridProperty, typeof(Syncfusion.EJ2.Grids.Grid));
            GridColumnModel           cols      = (GridColumnModel)Newtonsoft.Json.JsonConvert.DeserializeObject(gridProperty, typeof(GridColumnModel));

            GridModel.Columns = cols.columns;
            return(GridModel);
        }
        /// <summary>
        /// Method to build body cell models using reflection.
        /// </summary>
        /// <typeparam name="T">Model entity.</typeparam>
        /// <param name="gridColumnModel">Column model containing header and body cell configuration details.</param>
        /// <param name="dataEntity">Model entity containing data to be displayed.</param>
        /// <param name="rowNum">Zero based index row number for which cell has to be build.</param>
        /// <returns>GridRowCellModel.</returns>
        private GridRowCellModel BuildGridRowCellModel <T>(GridColumnModel gridColumnModel, T dataEntity, int rowNum)
        {
            var gridRowCellModel = new GridRowCellModel();

            // Get dataEntity type. To be used later to extract data to be displayed.
            var dataEntityProperty = dataEntity.GetType().GetProperty(gridColumnModel.HeaderCell.BindingColumnName);

            // Build different cells based on header column type.
            switch (gridColumnModel.HeaderCell.ColumnType)
            {
            case GridColumnType.Image:
                // Proceed only if this column has any valid datasource column binded to it.
                if (dataEntityProperty != null)
                {
                    // If data type be bool then take image path from configuration.
                    if (dataEntityProperty.PropertyType.Name.Equals("Bool") || dataEntityProperty.PropertyType.Name.Equals("Boolean"))
                    {
                        gridRowCellModel.ImagePath = ((bool)dataEntityProperty.GetValue(dataEntity, null))
                                                             ? ConfigHelper.GetBooleanImage(true)
                                                             : ConfigHelper.GetBooleanImage(false);
                    }
                    // Else take image path from data source.
                    else
                    {
                        gridRowCellModel.ImagePath = dataEntityProperty.GetValue(dataEntity, null).ToString();
                    }
                }
                break;

            case GridColumnType.Link:
                // Simply choose the list of links for the cell as per current row number.
                if (gridColumnModel.Links != null && gridColumnModel.Links.Count > 0)
                {
                    gridRowCellModel.Links = new List <GridLinkModel>(gridColumnModel.Links.ElementAt(rowNum));
                }
                break;

            case GridColumnType.Text:
                // Proceed only if this column has any valid datasource column binded to it.
                if (dataEntityProperty != null)
                {
                    // If data type be bool then set text value as Yes or No.
                    if (dataEntityProperty.PropertyType.Name.Equals("Bool") || dataEntityProperty.PropertyType.Name.Equals("Boolean"))
                    {
                        gridRowCellModel.Text = ((bool)dataEntityProperty.GetValue(dataEntity, null))
                                                        ? BoolYesNo.Yes.ToString()
                                                        : BoolYesNo.No.ToString();
                    }
                    // Else set text from data source.
                    else
                    {
                        if (dataEntityProperty.GetValue(dataEntity, null) != null)
                        {
                            gridRowCellModel.Text = dataEntityProperty.GetValue(dataEntity, null).ToString();
                        }
                    }
                }
                break;

            default:
                break;
            }

            // Set the column type and disable flag.
            gridRowCellModel.ColumnType = gridColumnModel.HeaderCell.ColumnType;
            gridRowCellModel.IsDisabled = gridColumnModel.HeaderCell.IsDisabled;

            // Set general properties of cell.
            if (!string.IsNullOrEmpty(gridColumnModel.BodyCellProperty.CssClass))
            {
                gridRowCellModel.CssClass = gridColumnModel.BodyCellProperty.CssClass;
            }

            return(gridRowCellModel);
        }