2014-11-04

Dependency Injection - Hello World

Here is a simple implementation of Dependency Injection. UnityContainer is initialize in code but you can use configuration file if you like.


using System;
using Microsoft.Practices.Unity;
 
namespace DependencyInjectionSample
{
    public interface IUnityClass
    {
        void HelloWorld();
    }
 
    public class UnityClass : IUnityClass
    {
        public void HelloWorld()
        {
            Console.WriteLine("Hello!");
        }
    }
 
    /// <summary>
    ///     This class is used to manage dependency injection using Unity.
    ///     It Manages containers and configurations.
    /// </summary>
    internal static class DependencyInjection
    {
        private static IUnityContainer InitUnityContainer()
        {
            IUnityContainer myContainer = new UnityContainer();
 
            //Register types
            myContainer.RegisterType(typeof (IUnityClass), typeof (UnityClass));
            //...
 
            return myContainer;
        }
 
 
        public static T Resolve<T>()
        {
            IUnityContainer myContainer = InitUnityContainer();
         
            return (T) myContainer.Resolve(typeof (T));
        }
    }
 
 
    public class HelloWorld
    {
        public HelloWorld()
        {
            var di = DependencyInjection.Resolve<IUnityClass>();
            di.HelloWorld();
        }
    }
}

2014-10-21

How to draw a grid in c#

Grids are often needed when we design or measure something. Here is example of a such grid.


Code for it:


using System;
using System.Drawing;
using System.Windows.Forms;
 
namespace WinFormDemo
{
    public partial class FormGrid : Form
    {
        public FormGrid()
        {
            InitializeComponent();
        }
 
 
        private void OnPanelPaint(object sender, PaintEventArgs e)
        {
            e.Graphics.PageUnit = GraphicsUnit.Millimeter;
            DrawHelpGrid(e.Graphics);
        }
 
 
        private void DrawHelpGrid(Graphics graphic, int width = 100int height = 100)
        {
            int max = Math.Max(width, height);
            var pen1 = new Pen(Brushes.LightGray, 0.1f);
            var pen2 = new Pen(Brushes.LightGreen, 0.1f);
            bool mainLine = true;
 
            for (int index = 0; index < max; index += 5)
            {
                Pen pen = mainLine ? pen2 : pen1;
 
                // Make main line little longer
                int ext = mainLine ? 2 : 0;
 
                if (index <= height)
                {
                    graphic.DrawLine(pen, 0, index, width + ext, index);
                    if (mainLine)
                    {
                        graphic.DrawString(index + "", 
                            SystemFonts.MenuFont, 
                            Brushes.Blue, 
                            width + 5, index - 2);
                    }
                }
 
                if (index <= width)
                {
                    graphic.DrawLine(pen, index, 0, index, height + ext);
                    if (mainLine)
                    {
                        graphic.DrawString(index + "", 
                            SystemFonts.MenuFont, 
                            Brushes.Blue, 
                            index - 3, height + 5);
                    }
                }
                mainLine = !mainLine;
            }
        }
    }
}
And Designer conde

namespace WinFormDemo
{
    partial class FormGrid
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;
 
        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }
 
        #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.panel1 = new System.Windows.Forms.Panel();
            this.SuspendLayout();
            // 
            // panel1
            // 
            this.panel1.BackColor = System.Drawing.Color.White;
            this.panel1.Dock = System.Windows.Forms.DockStyle.Fill;
            this.panel1.Location = new System.Drawing.Point(00);
            this.panel1.Name = "panel1";
            this.panel1.Size = new System.Drawing.Size(590428);
            this.panel1.TabIndex = 1;
            this.panel1.Paint += new System.Windows.Forms.PaintEventHandler(this.OnPanelPaint);
            // 
            // FormGrid
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(590428);
            this.Controls.Add(this.panel1);
            this.Name = "FormGrid";
            this.Text = "Grid";
            this.ResumeLayout(false);
 
        }
 
        #endregion
 
        private System.Windows.Forms.Panel panel1;
    }
}
 

2014-02-18

How to show row count always in first column in devexpress asp.net mvc GridView

Your partial view with gridview control may look something like this:

@using MyWebApplication.Controllers
@{    var gridHelper = new GridViewHelper();
    var grid = Html.DevExpress().GridView(settings =>
    {
        settings.Name = "MyGridView";
        settings.SettingsBehavior.ColumnResizeMode = ColumnResizeMode.Control;
        settings.Columns.Add("col1");
        settings.Columns.Add("col2");
        settings.Columns.Add("col3");
        settings.Settings.ShowFooter = true;
        settings.HtmlFooterCellPrepared = gridHelper.ShowRowCountInFirstColumn;
    });
}@grid.Bind(Model).GetHtml()



And in here is definition of class GridViewHelper



public class GridViewHelper{   public void ShowRowCountInFirstColumn( object sender,  ASPxGridViewTableFooterCellEventArgs args)   {      if (args.Column.VisibleIndex != 1)        return;       ASPxGridView grid = sender as ASPxGridView;       foreach (ASPxSummaryItem item in grid.TotalSummary)       {         if (item.SummaryType == DevExpress.Data.SummaryItemType.Count)         {            args.Cell.Text = string.Format("{0:n0} rows",grid.GetTotalSummaryValue(item));
           return;
         }
        }
 
       var countSummary = grid.TotalSummary.Add(DevExpress.Data.SummaryItemType.Count, "#");
       args.Cell.Text = string.Format("{0:n0} rows",grid.GetTotalSummaryValue(item));
       return;     }
}