Click here to Skip to main content
6,596,602 members and growing! (19,825 online)
Email Password   helpLost your password?
Desktop Development » Grid & Data Controls » Grid controls     Intermediate

Model driven Gridview

By Burkovsky

A Model driven GridView.
C#, Windows, .NET 2.0VS.NET2003, VS2005, Dev
Posted:31 Oct 2004
Updated:16 Nov 2004
Views:63,802
Bookmarked:28 times
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
12 votes for this article.
Popularity: 3.35 Rating: 3.10 out of 5
2 votes, 16.7%
1
4 votes, 33.3%
2
3 votes, 25.0%
3
1 vote, 8.3%
4
2 votes, 16.7%
5

Introduction

In .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: Model, which is responsible for delivering of: Columns, Rows and Cell values. It is very nice to separate the View of Data from the Data itself. In this article, you will find the first step to how to implement it.

Background

This 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 code

Using the MVCGridView component is very simple. Put it on your Form. Implement a MVCGridViewModel. Assign the MVCGridView.Model property to your MVCGridViewModel descendant. That's all. You can hence add and edit your business entities.

Here is the MVCGridViewModel:

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: Person is your the business entity. It has 4 properties. First name, last name and salary is editable. Full name is computed not editable property. Of course, you can put the properties and logic in it. The Person represents a row in the GridView. You can and must provide the collection of visible columns in the GridView. Every other method is right trivial. The only interesting is the IsValid method. In this method, you can test the user input.

What next?

  1. You can add for every row, a different kind of object. You must only change your AddNewElement(Object Element) method.
  2. You can persist the changes immediately. You must update your data in the persistence storage in SetElement(System.Int32 index, Object Element), SetElementValue(Object element, String ColName, Object Value) and RemoveAt(System.Int32 index).

History

  • Initial release 10/29/2004.
  • Computed column added 11/15/2004.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Burkovsky


Member

Occupation: Web Developer
Location: Germany Germany

Other popular Grid & Data Controls articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 6 of 6 (Total in Forum: 6) (Refresh)FirstPrevNext
GeneralThe MVCGridViewModel should be an interface Pinmemberugog914:41 16 Nov '04  
GeneralRe: The MVCGridViewModel should be an interface PinmemberBurkovsky5:31 16 Nov '04  
GeneralRe: The MVCGridViewModel should be an interface Pinmemberugog916:23 16 Nov '04  
GeneralCollections integration PinmemberNick Hounsome20:26 12 Nov '04  
GeneralRe: Collections integration PinsussAnonymous2:49 13 Nov '04  
General.NET 1.1 Pinmemberaprogram2:18 9 Nov '04  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 16 Nov 2004
Editor: Nishant Sivakumar
Copyright 2004 by Burkovsky
Everything else Copyright © CodeProject, 1999-2009
Web22 | Advertise on the Code Project