|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Announcements
Chapters
Services
Feature Zones
|
IntroductionIn .NET framework, there is no model driven grid (table) component. And I missed it a lot. Therefore, I made this simple component. It is a GridView descendant with an additional property: BackgroundThis article assumes that you are familiar with WinForms programming in the .NET Framework using C#, and that you have the beta of version 2.0 installed. Using the codeUsing the Here is the public abstract class MVCGridViewModel
{
public abstract void AddNewElement(Object Element);
public abstract System.Int32 ElementCount { get; }
public virtual Boolean IsValid(String formattedValue, String colName)
{
return true;
}
public abstract Object GetElement(System.Int32 index);
public abstract Object GetElementValue(Object element, String ColName);
public abstract Object NewElement();
public abstract void SetElement(System.Int32 index, Object Element);
public abstract void SetElementValue(Object element,
String ColName, Object Value);
public abstract void RemoveAt(System.Int32 index);
public abstract IEnumerable<DataGridViewColumn> VisibleColumns { get; }
}
The descendant is the only thing you have to implement. Very easy. You can almost copy and paste this code.
public class Person
{
private String _FirstName = "";
public String FirstName
{
get { return _FirstName; }
set { _FirstName = value; }
}
private String _LastName = "";
public String LastName
{
get { return _LastName; }
set { _LastName = value; }
}
private System.Double _Salary;
public System.Double Salary
{
get { return _Salary; }
set { _Salary = value; }
}
public String FullName
{
get { return _FirstName + " " +_LastName; }
}
}
public class SimpleModel : MVCGridViewModel
{
private List<Person> Persons = new List<Person>();
public override void AddNewElement(Object Element)
{
Persons.Add((Person)Element);
}
public override System.Int32 ElementCount
{
get
{
return Persons.Count;
}
}
public override Boolean IsValid(String formattedValue, String colName)
{
try
{
if (colName == "Salary")
Convert.ToDouble(formattedValue);
return true;
}
catch
{
Console.Beep();
return false;
}
}
public override Object GetElement(System.Int32 index)
{
return Persons[index];
}
public override Object GetElementValue(Object element, String ColName)
{
switch (ColName)
{
case "First Name": return ((Person)element).FirstName;
case "Last Name": return ((Person)element).LastName;
case "Full Name": return ((Person)element).FullName;
case "Salary": return ((Person)element).Salary;
default: return "";
}
}
private IEnumerable<DataGridViewColumn> GetVisibleColumns()
{
DataGridViewColumn col = new DataGridViewTextBoxColumn();
col.Name = "First Name";
col.HeaderText = "First Name";
col.Resizable = DataGridViewTriState.True;
yield return col;
col = new DataGridViewTextBoxColumn();
col.Name = "Last Name";
col.HeaderText = "Last Name";
col.Resizable = DataGridViewTriState.True;
yield return col;
col = new DataGridViewTextBoxColumn();
col.Name = "Full Name";
col.HeaderText = "Full Name";
col.Resizable = DataGridViewTriState.True;
col.ReadOnly = true;
yield return col;
col = new DataGridViewTextBoxColumn();
col.Name = "Salary";
col.HeaderText = "Salary";
col.Resizable = DataGridViewTriState.True;
col.ReadOnly = false;
yield return col;
}
public override void SetElementValue(Object element,
String ColName, Object Value)
{
switch (ColName)
{
case "First Name": ((Person)element).FirstName =
(String)Value; FireDataChanged(); break;
case "Last Name": ((Person)element).LastName =
(String)Value; FireDataChanged(); break;
case "Salary": ((Person)element).Salary =
Double.Parse(Value.ToString(),
(NumberStyles.AllowDecimalPoint)); break;
}
}
public override void SetElement(System.Int32 index, Object Element)
{
Persons[index] = (Person)Element;
}
public override Object NewElement()
{
return new Person();
}
public override void RemoveAt(System.Int32 index)
{
Persons.RemoveAt(index);
}
public override IEnumerable<DataGridViewColumn> VisibleColumns
{
get { return GetVisibleColumns(); }
}
}
A little explanation: What next?
History
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||