I have the following logic for my View, which has a RadGridView control (add to form at design-time).
GUI is a lightweight view, Controller does the heavy lifting. That part is all good.
However setting the column name and column autosize stuff is a little more "over-complicated" than I'd wish to see. It all works though.
Would be nice to have a "GridViewTextBoxColumnAttribute" as per below, for properties on Custom datasource objects to achieve the same thing in a more concise way.
Or am I missing something and there is a much easier way to do what I want?
I have a number of datasource classes: MyObject, MyCar, MyDog, MyWhatever... that ALL have different property names and datatypes, but may or may-not have the same column name or MasterTemplate style.
Also BestFitColumns is a function of the MasterTemplate, would be nice if it were a property for consistency sake (call to internal function encapsulated in the Set etc), as from a Telerik user's perspective it's just an enum. Then it would be setable in the SmartTag window as well, as functions can't be assigned to.
public partial class RadForm1 : Telerik.WinControls.UI.RadForm, IView
{
public RadForm1)
{
InitializeComponent();
}
private void RadForm1_Load(object sender, EventArgs e)
{
var x = new Controller();
x.SetTemplate(this);
}
public Telerik.WinControls.UI.RadGridView RadGridView()
{
return this.radGridView1;
}
}
public class Controller
{
public void SetTemplate(IView view)
{
var View_RadGrid = view.RadGridView();
View_RadGrid.MasterTemplate.AutoGenerateColumns = false;
View_RadGrid.MasterTemplate.AutoGenerateHierarchy = false;
View_RadGrid.MasterTemplate.AllowColumnResize = false;
View_RadGrid.MasterTemplate.BestFitColumns(Telerik.WinControls.UI.BestFitColumnMode.AllCells);
var cols = new List<Telerik.WinControls.UI.GridViewDataColumn>()
{
new Telerik.WinControls.UI.GridViewTextBoxColumn("DisplayName1"),
new Telerik.WinControls.UI.GridViewTextBoxColumn("DisplayName2")
};
View_RadGrid.MasterTemplate.Columns.AddRange(cols.ToArray());
View_RadGrid.DataSource = new List<MyObject>()
{
new MyObject() { DisplayName1 = "1", DisplayName2 = "2" },
new MyObject() { DisplayName1 = "3", DisplayName2 = "4" }
};
}
private class MyObject
{
[GridViewTextBoxColumnAttribute("DisplayName1", BestFitColumn = Telerik.WinControls.UI.BestFitColumnMode.AllCells, AllowColumnResize = false)]
public string DisplayName1 { get; set; }
[GridViewTextBoxColumnAttribute("DisplayName2", BestFitColumn = Telerik.WinControls.UI.BestFitColumnMode.AllCells, AllowColumnResize = false)]
public string DisplayName2 { get; set; }
}
}
public interface IView
{
Telerik.WinControls.UI.RadGridView RadGridView();
}
[System.AttributeUsage(System.AttributeTargets.Property)]
public class GridViewTextBoxColumnAttribute : System.Attribute
{
private string FieldName;
public Telerik.WinControls.UI.BestFitColumnMode BestFitColumn;
public bool AllowColumnResize;
public GridViewTextBoxColumnAttribute(string fieldName)
{
this.FieldName = fieldName;
}
}