Permalink
Browse files

Checkbox Column for bool type properties

Modified the Bind Data method, to identify when a class object is of bool type, and then, add a Checkbox Column to the datagridview.
  • Loading branch information...
tassan committed Nov 22, 2018
1 parent 8ff53f0 commit 37e2a249799962a2271d1a60c8418027e9c347d2
Showing with 30 additions and 3 deletions.
  1. +30 −3 DataGridViewExtensions/DataGridExtensions.cs
@@ -118,9 +118,16 @@ public static void FormatCells(this DataGridView dataGridView, Func<DataGridView
throw new InvalidOperationException("Class cannot have nested collections.");
}

var columns = new Dictionary<PropertyInfo, string>();
properties.ForEach(p => columns.Add(p, (GetColumnName(p))));
columns.ToList().ForEach(column => dataGridView.Columns.Add(column.Key.Name, column.Value));
var columns = new Dictionary<PropertyInfo, string>();
properties.ForEach(p => columns.Add(p, (GetColumnName(p))));

foreach (var column in columns)
{
if (CheckPropertyType(column.Key, typeof(Boolean)))
dataGridView.Columns.Add(new DataGridViewCheckBoxColumn { Name = column.Key.Name });
else
dataGridView.Columns.Add(column.Key.Name, column.Value);
}
dataGridView.Rows.Add(dataToBindToGrid.Count());
var rowIndex = 0;
dataToBindToGrid.ToList().ForEach(data =>
@@ -158,5 +165,25 @@ private static string GetColumnName(PropertyInfo propertyInfo)
var description = descriptionAttribute as DescriptionAttribute;
return description == null ? propertyInfo.Name : description.Description;
}

/// <summary>
/// Check if the class property is of the specified type.
/// </summary>
/// <param name="propertyInfo">PropertyInfo</param>
/// <param name="type">typeof()</param>
/// <returns></returns>
private static bool CheckPropertyType(_PropertyInfo propertyInfo, Type type)
{
var result = false;
var typeToString = type.ToString();
var ptype = propertyInfo.PropertyType.ToString();

if (ptype == typeToString)
{
result = true;
}

return result;
}
}
}

0 comments on commit 37e2a24

Please sign in to comment.