DataGrid Binding: Custom DataGrid Column Form : DataGrid Data Binding « GUI Windows Form « C# / C Sharp

Home
C# / C Sharp
1.2D Graphics
2.Class Interface
3.Collections Data Structure
4.Components
5.Data Types
6.Database ADO.net
7.Design Patterns
8.Development Class
9.Event
10.File Stream
11.Generics
12.GUI Windows Form
13.Language Basics
14.LINQ
15.Network
16.Office
17.Reflection
18.Regular Expressions
19.Security
20.Services Event
21.Thread
22.Web Services
23.Windows
24.Windows Presentation Foundation
25.XML
26.XML LINQ
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source
C# / C Sharp » GUI Windows Form » DataGrid Data BindingScreenshots 
DataGrid Binding: Custom DataGrid Column Form
DataGrid Binding: Custom DataGrid Column Form

/*
User Interfaces in C#: Windows Forms and Custom Controls
by Matthew MacDonald

Publisher: Apress
ISBN: 1590590457
*/
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace DataGridBinding
{
    /// <summary>
    /// Summary description for CustomDataGridColumnForm.
    /// </summary>
    public class CustomDataGridColumnForm : System.Windows.Forms.Form
    {
        internal System.Windows.Forms.DataGrid grid;
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.Container components = null;

        public CustomDataGridColumnForm()
        {
            //
            // Required for Windows Form Designer support
            //
            InitializeComponent();

            //
            // TODO: Add any constructor code after InitializeComponent call
            //
        }

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        protected override void Disposebool disposing )
        {
            ifdisposing )
            {
                if(components != null)
                {
                    components.Dispose();
                }
            }
            base.Disposedisposing );
        }

        #region Windows Form Designer generated code
        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.grid = new System.Windows.Forms.DataGrid();
            ((System.ComponentModel.ISupportInitialize)(this.grid)).BeginInit();
            this.SuspendLayout();
            // 
            // grid
            // 
            this.grid.Anchor = (((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom
                | System.Windows.Forms.AnchorStyles.Left
                | System.Windows.Forms.AnchorStyles.Right);
            this.grid.DataMember = "";
            this.grid.HeaderForeColor = System.Drawing.SystemColors.ControlText;
            this.grid.Location = new System.Drawing.Point(1017);
            this.grid.Name = "grid";
            this.grid.Size = new System.Drawing.Size(272232);
            this.grid.TabIndex = 1;
            // 
            // CustomDataGridColumnForm
            // 
            this.AutoScaleBaseSize = new System.Drawing.Size(513);
            this.ClientSize = new System.Drawing.Size(292266);
            this.Controls.AddRange(new System.Windows.Forms.Control[] {
                                                                          this.grid});
            this.Name = "CustomDataGridColumnForm";
            this.Text = "CustomDataGridColumnForm";
            this.Load += new System.EventHandler(this.CustomDataGridColumnForm_Load);
            ((System.ComponentModel.ISupportInitialize)(this.grid)).EndInit();
            this.ResumeLayout(false);

        }
        #endregion

        [STAThread]
        static void Main() 
        {
            Application.Run(new CustomDataGridColumnForm());
        }

        private void CustomDataGridColumnForm_Load(object sender, System.EventArgs e)
        {
            DataSet dsStore = new DataSet();
            
            dsStore.ReadXmlSchema(Application.StartupPath + "\\store.xsd");
            dsStore.ReadXml(Application.StartupPath + "\\store.xml");
            
            // Create the column collection.
            DataGridTableStyle columns = new DataGridTableStyle();
            columns.MappingName = "Products";
            
            // Create and configure the columns you want to display.
            DataGridPriceIconColumn priceCol = new DataGridPriceIconColumn(100);
            priceCol.HeaderText = "Price";
            priceCol.MappingName = "UnitCost";
            
            // Add the columns to the collection.
            columns.GridColumnStyles.Add(priceCol);
            
            // Configure the DataGrid to use these column settings.
            grid.TableStyles.Add(columns);  
            
            grid.ReadOnly = true;
            
            // Bind the grid.
            grid.DataSource = dsStore.Tables["Products"];
            
        }
    }

public class DataGridPriceIconColumn : DataGridColumnStyle
{
    public decimal NicePrice;

    public DataGridPriceIconColumn(decimal nicePrice)
    {
        this.NicePrice = nicePrice;
    }

    protected override void Abort(int rowNum)
    {
        // Do nothing.
    }

    protected override bool Commit(CurrencyManager dataSource, int rowNum)
    {
        return true;
    }

    protected override void Edit(CurrencyManager source,
        int rowNum, System.Drawing.Rectangle bounds,
        bool readOnly, string instantText, bool cellIsVisible)
    {
        // Do nothing.
    }

    protected override void Edit(CurrencyManager source,
        int rowNum, System.Drawing.Rectangle bounds, bool readOnly)
    {
        // Do nothing.
    }

    protected override void Edit(CurrencyManager source,
                            int rowNum, System.Drawing.Rectangle bounds,
                            bool readOnly, string instantText)
    {
        // Do nothing.
    }
    protected override int GetMinimumHeight()
    {
        return 20;
    }

    protected override int GetPreferredHeight(System.Drawing.Graphics g,
        object value)
    {
        return 20;
    }

    protected override System.Drawing.Size GetPreferredSize(
        System.Drawing.Graphics g, object value)
    {
        return new Size(10020);
    }

    protected override void Paint(System.Drawing.Graphics g,
        System.Drawing.Rectangle bounds, CurrencyManager source, int rowNum,
        System.Drawing.Brush backBrush, System.Drawing.Brush foreBrush,
        bool alignToRight)
    {
        // Clear the cell.
        g.FillRegion(backBrush, new Region(bounds));

        decimal price = (decimal)this.GetColumnValueAtRow(source, rowNum);
        Icon priceIcon;
        if (price < NicePrice)
        {
            priceIcon = new Icon(Application.StartupPath + "\\happy2.ico");

            // Draw the optional "nice price" icon.
            g.DrawIcon(priceIcon, new Rectangle(bounds.X, bounds.Y, 1616));
        }

        // Draw the text.
        g.DrawString(price.ToString("C")new Font("Tahoma"(float)8.25)
            Brushes.Black, bounds.X + 20, bounds.Y + 2);
    }

    protected override void Paint(System.Drawing.Graphics g,
        System.Drawing.Rectangle bounds, CurrencyManager source,
        int rowNum, bool alignToRight)
    {
        this.Paint(g, bounds, source, rowNum, Brushes.White, Brushes.Black,
            alignToRight);
    }

    protected override void Paint(System.Drawing.Graphics g,
        System.Drawing.Rectangle bounds, CurrencyManager source, int rowNum)
    {
        this.Paint(g, bounds, source, rowNum, Brushes.White, Brushes.Black, false);
    }

}

    
}



           
       
DataGridBinding.zip( 45 k)
Related examples in the same category
1.DataGrid BindingDataGrid Binding
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.