Saturday, August 29, 2015

1Y3M - 資源回收

拿著桶子,滿庭院跑,不知道是在 High 甚麼的

1Y3M - 資源回收

Friday, August 28, 2015

[C#] DataGridView 匯出 Excel

論壇問題 - 把 DataGridView 內資料匯出 Excel

[C#] DataGridView 匯出 Excel-4
Employ Class
namespace DataGridView2Excel
{
    public class Employ
    {
        public string EmpNO { get; set; }
        public string EmpName { get; set; }
        public DateTime Birthday { get; set; }
        public decimal Salary { get; set; }
    }
}

Thursday, August 27, 2015

[C#] Microsoft.Office.Interop.Excel

想學習一下,如何在 C# 中使用 Excel Automation,沒想到光是要把它加入參考,就花掉不少時間,最後是在下圖的位置,找到 Excel.dll,Orz

自己筆電

[C#] Microsoft.Office.Interop.Excel
using Excel = Microsoft.Office.Interop.Excel;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Excel.Application demo = new Excel.Application();
        }
    }
}
  • 20150828
在公司電腦是在參考管理員 => 組件 => 擴充功能下就找到啦

[C#] Microsoft.Office.Interop.Excel-1

Wednesday, August 26, 2015

[C#] ComboBox 多欄位 - DrawItem

利用 ComboBox DrawItem Event 來作到多欄位顯示功能

DateSelect Class
namespace ComboBoxMultiCols
{
    public class DateSelect
    {
        public int Year { get; set; }
        public int TWYear { get; set; }

        public static List<DateSelect> DataFill()
        {
            List<DateSelect> result = new List<DateSelect>();

            DateTime 
                Today = DateTime.Today ,
                LoopDate = DateTime.Today ;
            int 
                Year = 0 ,
                TWYear = 0;
   
            System.Globalization.TaiwanCalendar tc = new System.Globalization.TaiwanCalendar();

            for (int i = 0; i < 10; i++)
            {
    
                LoopDate = Today.AddYears(i * -1);
                Year = LoopDate.Year;
                TWYear = tc.GetYear(LoopDate);

                DateSelect ds = new DateSelect()
                {
                    Year = Year ,
                    TWYear = TWYear
                };

                result.Add(ds);
            }

            return result;
        }
    }
}
WinForm Demo
namespace ComboBoxMultiCols
{
    public partial class MultiCols_DrawItem : Form
    {
        public MultiCols_DrawItem()
        {
            InitializeComponent();
        }

        private void MultiCols_DrawItem_Load(object sender, EventArgs e)
        {
            comboBox1.DropDownStyle = ComboBoxStyle.DropDownList;
            comboBox1.DataSource = DateSelect.DataFill();
            comboBox1.DisplayMember = "Year";
            comboBox1.ValueMember = "TWYear";

            comboBox1.DrawMode = DrawMode.OwnerDrawFixed;
            comboBox1.DrawItem += comboBox1_DrawItem;
        }

        void comboBox1_DrawItem(object sender, DrawItemEventArgs e)
        {
            // 繪製預設背景
            e.DrawBackground();

            // DataSource 是 List<DateSelect>,所以這裡必須轉型為 DateSelect
            // 假如 DataSource 是 DataTable,就必須轉型為 DataRowView
            DateSelect ds = (DateSelect)this.comboBox1.Items[e.Index];

            // ComboBox 內文字
            string DropDownInfo = string.Format("{0} (民國 {1:000} 年)", ds.Year, ds.TWYear);

            // 取得 ComboBox 界限
            Rectangle rec = e.Bounds;
   
            // 把文字繪進去
            using (SolidBrush sb = new SolidBrush(e.ForeColor))
            {
                e.Graphics.DrawString(DropDownInfo, e.Font, sb, rec);
            }
        }
    }
}

[C#] ComboBox 多欄位 - DrawItem-1

Tuesday, August 25, 2015

[C#] PictureBox - 區段顏色

論壇問題,簡化成在動態產生 PicutrBox 並根據資料在 Paint Event 內畫出各區段顏色

專案內容
[C#] PictureBox - 區段顏色-2

WinForm Layout:只放一個 FlowLayoutPanel 容器

[C#] PictureBox - 區段顏色-1