2014年11月23日 星期日

TextBox 如何對應 Nullable 資料欄??

最近專案使用到 TextBox 對應到 Nullable 的資料欄位,但如果發生編輯該欄位資料之後,又想改回空值時,卻發現游標卡住,無法跳到下一個欄位。

解決方案:

在 *.Designer.cs 中找到
DataBindings.Add(new System.Windows.Forms.Binding("Text", myClass, "MyTextProperty", true);

加入如下紅色字串部份,即可:
DataBindings.Add(new System.Windows.Forms.Binding("Text", myClass, "MyTextProperty", true, System.Windows.Forms.DataSourceUpdateMode.OnPropertyChanged, string.Empty));

2014年10月29日 星期三

VS2013 開啟專案提報錯誤訊息

VS2013 開啟舊專案時,提報錯誤訊息:

---------------------------
Microsoft Visual Studio
---------------------------
未正確載入套件 'Microsoft.VisualStudio.Editor.Implementation.EditorPackage'。


這個問題可能是因為組態變更或安裝其他擴充功能所造成。您可以從檢查檔案 'C:\Users\Slong\AppData\Roaming\Microsoft\VisualStudio\12.0\ActivityLog.xml' 以取得詳細資訊。

要繼續顯示這項錯誤訊息嗎?
---------------------------
是(Y)   否(N)
---------------------------

解決方法:

關閉 VS2013,到  %LOCALAPPDATA%\Microsoft\VisualStudio\12.0\ComponentModelCache 資料夾下,把所有的資料夾及檔案刪除掉,重新開啟 VS2013 即可。

2014年10月15日 星期三

Windows Server 2012 DHCP MAC address Filter management

手機使用者越來越多,佔用公司的 DHCP IP 太多,從微軟那兒挖到一篇可以攔阻不明使用者MAC的文章:

http://blogs.technet.com/b/teamdhcp/archive/2012/11/10/dhcp-mac-address-filter-management-made-easy-with-dhcp-powershell.aspx

裡面的指令挺管用的,但是照抄照打會出錯,我把我使用的指令寫下來,給大家參考:

PS C:\> Get-DhcpServerv4FilterList -ComputerName dhcp-server 
PS C:\> Set-DhcpServerv4FilterList -ComputerName dhcp-server -Allow 1 -Deny 1

2014年9月28日 星期日

C# - Bypass read only cells in DatagridView when pressing TAB (Shift+TAB) key

在 Visual Studio 裡,DataDridView 不會自動跳過唯讀欄位,在網路上找了好久才找到一、兩篇相關的文章,但是都有一些問題,最後我把它整理好,且用在自己的工作上。

下列程式碼,是用來跳過 DataGridView 中唯讀的欄位,希望對大家有幫助:

I learned it from the web: http://social.msdn.microsoft.com/Forums/windows/en-US/2025a294-9143-4ecf-82b1-f632da163e85/bypass-read-only-cells-in-datagridview-when-pressing-tab-key?forum=winforms

Note: Do not remove try {} catch {} blocks, it will bypass OnCellValiding() exception....

using System.Windows.Forms;

namespace TestDataGridView
{
    public partial class IfDataGridView : DataGridView
    {
        public IfDataGridView()
        {
            InitializeComponent();
        }

        protected override bool ProcessDialogKey(Keys keyData)
        {
            if (null == this.CurrentCell)
            {
                return true;
            }

            Keys key = keyData & Keys.KeyCode;
            if (!(key == Keys.ShiftKey || (int)keyData == 65545)) // 沒有按著 Shift
            {
                if (key == Keys.Tab)
                {
                    try
                    {
                        DataGridViewColumn nextColumn = this.Columns.GetNextColumn(this.Columns[this.CurrentCell.ColumnIndex], DataGridViewElementStates.Visible, DataGridViewElementStates.ReadOnly);
                        if (null != nextColumn)
                        {
                            this.CurrentCell = this.Rows[this.CurrentCell.RowIndex].Cells[nextColumn.Index];
                        }
                        else
                        {
                            nextColumn = this.Columns.GetFirstColumn(DataGridViewElementStates.Visible, DataGridViewElementStates.ReadOnly);
                            if ((this.CurrentCell.RowIndex + 1) == this.Rows.Count)
                            {
                                this.CurrentCell = this.Rows[0].Cells[nextColumn.Index];
                            }
                            else
                            {
                                this.CurrentCell = this.Rows[this.CurrentCell.RowIndex + 1].Cells[nextColumn.Index];
                            }
                        }
                    }
                    catch {}
                    return true;
                }
            }
            else
            {
                if (key == Keys.Tab)
                {
                    try {
                        DataGridViewColumn nextColumn = this.Columns.GetFirstColumn(DataGridViewElementStates.Visible, DataGridViewElementStates.ReadOnly);
                        DataGridViewColumn priorColumn = nextColumn;
                        //
                        if (nextColumn.DisplayIndex >= this.Columns[this.CurrentCell.ColumnIndex].DisplayIndex)
                        {
                            if (this.CurrentCell.RowIndex > 0)
                            {
                                this.CurrentCell = this.Rows[this.CurrentCell.RowIndex - 1].Cells[this.Columns.GetLastColumn(DataGridViewElementStates.Visible, DataGridViewElementStates.ReadOnly).Index];
                            }
                            else
                            {
                                this.CurrentCell = this.Rows[this.Rows.Count - 1].Cells[this.Columns.GetLastColumn(DataGridViewElementStates.Visible, DataGridViewElementStates.ReadOnly).Index];
                            }
                        }
                        else
                        {
                            //Same Row
                            while (nextColumn.DisplayIndex < this.Columns[this.CurrentCell.ColumnIndex].DisplayIndex)
                            {
                                priorColumn = nextColumn;
                                nextColumn = this.Columns.GetNextColumn(this.Columns[nextColumn.Index], DataGridViewElementStates.Visible, DataGridViewElementStates.ReadOnly);
                            }
                            this.CurrentCell = this.Rows[this.CurrentCell.RowIndex].Cells[priorColumn.Index];
                        }
                    }
                    catch {}
                    return true;
                }
            }

            return base.ProcessDialogKey(keyData);
        }

        protected override bool ProcessDataGridViewKey(KeyEventArgs e)
        {
            if (null == this.CurrentCell)
            {
                return true;
            }
   //
            if (!e.Shift)
            {
                if (e.KeyData == Keys.Tab)
                {
                    //
                    try
                    {
                        DataGridViewColumn nextColumn = this.Columns.GetNextColumn(this.Columns[this.CurrentCell.ColumnIndex], DataGridViewElementStates.Visible, DataGridViewElementStates.ReadOnly);
                        if (null != nextColumn)
                        {
                            this.CurrentCell = this.Rows[this.CurrentCell.RowIndex].Cells[nextColumn.Index];
                        }
                        else
                        {
                            nextColumn = this.Columns.GetFirstColumn(DataGridViewElementStates.Visible, DataGridViewElementStates.ReadOnly);
                            if ((this.CurrentCell.RowIndex + 1) == this.Rows.Count)
                            {
                                this.CurrentCell = this.Rows[0].Cells[nextColumn.Index];
                            }
                            else
                            {
                                this.CurrentCell = this.Rows[this.CurrentCell.RowIndex + 1].Cells[nextColumn.Index];
                            }
                        }
                    }
                    catch { }
                    return true;
                }
                //判斷編輯狀態下,游標在最左邊,還是在最右邊??
                bool isEditing = false;
                if (this.IsCurrentCellInEditMode && this.CurrentCell.EditType == typeof(DataGridViewTextBoxEditingControl))
                {
                    isEdititng = (((TextBox)this.EditingControl).SelectionStart > 0) &&
                                    (((TextBox)this.EditingControl).SelectionStart < ((TextBox)this.EditingControl).Text.Length);
                }
                if (!Edititng)
                {
                    // 按下向右鍵
                    if (e.KeyData == Keys.Right || e.KeyData == Keys.Enter)
                    {
                        try
                        {
                            DataGridViewColumn nextColumn = this.Columns.GetNextColumn(this.Columns[this.CurrentCell.ColumnIndex], DataGridViewElementStates.Visible, DataGridViewElementStates.ReadOnly);
                            if (null != nextColumn)
                            {
                                this.CurrentCell = this.Rows[this.CurrentCell.RowIndex].Cells[nextColumn.Index];
                            }
                            else
                            {
                                nextColumn = this.Columns.GetFirstColumn(DataGridViewElementStates.Visible, DataGridViewElementStates.ReadOnly);
                                this.CurrentCell = this.Rows[this.CurrentCell.RowIndex].Cells[nextColumn.Index];
                            }
                        }
                        catch { }
                        return true;
                    }
                    // 按下向左鍵
                    if (e.KeyData == Keys.Left)
                    {
                        try
                        {
                            DataGridViewColumn nextColumn = this.Columns.GetFirstColumn(DataGridViewElementStates.Visible, DataGridViewElementStates.ReadOnly);
                            DataGridViewColumn priorColumn = nextColumn;

                            if (nextColumn.DisplayIndex >= this.Columns[this.CurrentCell.ColumnIndex].DisplayIndex)
                            {
                                this.CurrentCell = this.Rows[this.CurrentCell.RowIndex].Cells[this.Columns.GetLastColumn(DataGridViewElementStates.Visible, DataGridViewElementStates.ReadOnly).Index];
                            }
                            else
                            {
                                //Same Row
                                while (nextColumn.DisplayIndex < this.Columns[this.CurrentCell.ColumnIndex].DisplayIndex)
                                {
                                    priorColumn = nextColumn;
                                    nextColumn = this.Columns.GetNextColumn(this.Columns[nextColumn.Index], DataGridViewElementStates.Visible, DataGridViewElementStates.ReadOnly);
                                }
                                this.CurrentCell = this.Rows[this.CurrentCell.RowIndex].Cells[priorColumn.Index];
                            }
                        }
                        catch { }
                        return true;
                    }
                }
                // 按向上鍵
                if (e.KeyData == Keys.Up)
                {
                    try
                    {
                        if (this.CurrentCell.RowIndex > 0)
                        {
                            this.CurrentCell = this.Rows[this.CurrentCell.RowIndex - 1].Cells[this.CurrentCell.ColumnIndex];
                        }
                        else
                        {
                            this.CurrentCell = this.Rows[this.Rows.Count - 1].Cells[this.CurrentCell.ColumnIndex];
                        }
                    }
                    catch { }
                    return true;
                }
                // 按向下鍵
                if (e.KeyData == Keys.Down)
                {
                    try
                    {
                        if ((this.CurrentCell.RowIndex + 1) == this.Rows.Count)
                        {
                            this.CurrentCell = this.Rows[0].Cells[this.CurrentCell.ColumnIndex];
                        }
                        else
                        {
                            this.CurrentCell = this.Rows[this.CurrentCell.RowIndex + 1].Cells[this.CurrentCell.ColumnIndex];
                        }
                    }
                    catch { }
                    return true;
                }
            }
            //
            if (e.Shift || (int)e.KeyData == 65545)
            {
                if ((e.KeyData & Keys.Tab) == Keys.Tab)
                {
                    try
                    {
                        DataGridViewColumn nextColumn = this.Columns.GetFirstColumn(DataGridViewElementStates.Visible, DataGridViewElementStates.ReadOnly);
                        DataGridViewColumn priorColumn = nextColumn;
                        //
                        if (nextColumn.DisplayIndex >= this.Columns[this.CurrentCell.ColumnIndex].DisplayIndex)
                        {
                            if (this.CurrentCell.RowIndex > 0)
                            {
                                this.CurrentCell = this.Rows[this.CurrentCell.RowIndex - 1].Cells[this.Columns.GetLastColumn(DataGridViewElementStates.Visible, DataGridViewElementStates.ReadOnly).Index];
                            }
                            else
                            {
                                this.CurrentCell = this.Rows[this.Rows.Count - 1].Cells[this.Columns.GetLastColumn(DataGridViewElementStates.Visible, DataGridViewElementStates.ReadOnly).Index];
                            }
                        }
                        else
                        {
                            //Same Row
                            while (nextColumn.DisplayIndex < this.Columns[this.CurrentCell.ColumnIndex].DisplayIndex)
                            {
                                priorColumn = nextColumn;
                                nextColumn = this.Columns.GetNextColumn(this.Columns[nextColumn.Index], DataGridViewElementStates.Visible, DataGridViewElementStates.ReadOnly);
                            }
                            this.CurrentCell = this.Rows[this.CurrentCell.RowIndex].Cells[priorColumn.Index];
                        }
                    }
                    catch { }
                    return true;
                }
            }
            return base.ProcessDataGridViewKey(e);
        }
    }
}

2014年8月18日 星期一

VS2013 - 如何共用已設好的 Entity Framework

在同一個方案總管中,有二個專案在同一個命名空間裡,其中一個是 WinForm 程式,另一個 DLL 程式,要如何讓 DLL 程式使用到 Windows Form 程式裡的 EF (Entity Framework) ??

步驟如下:
1.打開 WinForm 的 App.config 將 <connectionStrings>到</connectionStrings>以及<entityFramework></entityFramework>複製下來。

2.打開 DLL 的 App.config,將剛才複製的資料貼入到</startup>之後 (注意位置)。

3.記得在 DLL 專案裡,加入 WinForm 的參考。

4.注意!如果 WinForm 及 DLL 會互相參考的話,則可將 EF 放在另一個 DLL 裡,可避免此現象發生。

這樣就可以共用共一個 EF(Entity Framework) 了。

2014年8月15日 星期五

Visual Studio 2013 如何找回 SqlConnection 及 SqlDataAdapter

在 Visusl Studio 2013 工具箱裡,預設是沒有 SqlConnection 及 SqlDataAdapter。

如果有需要的話可以在 "工具箱" -> "資料",按滑鼠右鍵,點選 "選擇項目",經過載入項目之後,「勾」選 SqlConnection 及 SqlDataAdapter 即可,是不是很簡單??

記得要用「勾」選,不然下一次又要再選一次。

2014年8月13日 星期三

如何將 EF 6.X EntityObject Generator for C# 加到 CSPROJ

如何將 EF 6.X EntityObject Generator for C# 加入到 CSPROJ

最近在研究 VS C# 2013,系統預設使用的是 EF 6.X DBContext,但是.....於是決定使用 EntityObject。從微軟下載了 EF 6.X EntityObject Generator for C#,居然不知道如何使用,好在網路上有善心人士發文解救苦難眾生,將做法記錄如下:

1.產生一個你要維護維的 Database 的 C# 專案。
2.使用 ADO.NET Data Entity Model 範本產生一個 EDMX 檔。
  你應該可以在 Visual C# 的功能表 "專案" -> "加入新項目" -> "資料",找到 "ADO.NET 實體資料模型" (ADO.NET Data Entity Model)。
3.套用精靈裡的選項:
   a. 名稱: <資料庫名稱>.edmx,預設為 Model1.edmx
   b. 從資料庫產生
   c. 系統將連線設定以<資料庫名稱>儲存於 AppConfig
   d. 複數單數...
   e. 包括 foreign keys
   f.  導入已選的 stored procedures
   g. 模組命名空間: <依 EDMX 的資料庫架構為準>
4.刪除所有的 TT 檔:
   a. <資料庫名稱>.Context.tt,預設為 Model1.Context.tt
   b. <資料庫名稱>.tt,預設為 Model1.tt
5.使用 EF 6.x EntityObject Generator for C# 範本,產生一個 TT 檔
   你可上從 "加入新項目" 的 "線上範本" 找到 EF 6.x EntityObject Generator for C#
6. 套用對話框中的選項:
   a. 名稱: <資料庫名稱>.tt,預設為 Model1.tt
7. 開啟新的 TT 檔,並且將 $edmxInputFile$ 置換為 <DatabaseName>.edmx,預設為 Model1.edmx

EntityObject 範本在標準 TT 的好處有:
  1. All entities are decorated with WCF attributes such as [DataContractAttribute]
  2. All entities have ///<summary> text so your business layer can generate an XML documentation file and treat warnings as errors without suppressing warning 1591

  3. Foreign keys automatically cross-reference - adding a new child to its parent will assign the parent's ID to the correct property on the child

原文(英文)出處: http://simplylogical.net/TheSimplyLogicalWay/DevelopSoftware/HowtoAddEF5EntityObjectstoaProject.aspx