2014年5月14日 星期三

[學習] DataGridView 設置 Column 顯示的格式為百分比

目的:
將Column的格式設置為百分比!

法一:    
DataGridView1.DataSource = dt
DataGridView1.Columns("新增次數").Visible = False
DataGridView1.Columns("供應商成交率").DefaultCellStyle.Format = "p"
DataGridView1.Columns("集團成交率").DefaultCellStyle.Format = "p"

法二:

Private Sub DataGridView1_CellFormatting(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellFormattingEventArgs) Handles DataGridView1.CellFormatting
      If Me.DataGridView1.Columns(e.ColumnIndex).Name = "供應商成交率" OrElse Me.DataGridView1.Columns(e.ColumnIndex).Name = "集團成交率" Then
          If e.Value IsNot Nothing Then
              e.Value = e.Value * 100 & "%"
          End If
      End If
End Sub

結果:
(1)原始資料


(2)顯示資料







參考:
DataGridView中显示百分比
DataGridView.CellFormatting 事件
格式化 Windows Form DataGridView 控制項中的資料
[C#] 字串輸出格式
string.Format 格式參數
23654.3654          Result
N0 取整數           23654
N1 取一位數       23654.3
N2 取二位數       23654.36(若有進位就 23654.37)
以此類推
DataGridView 控制項 (Windows Form)

[ASP.NET] Gridview 欄位輸出格式 DataFormatString
http://msdn.microsoft.com/zh-tw/library/vstudio/f9x2790s(v=vs.100).aspx?cs-save-lang=1&cs-lang=vb#code-snippet-1

2014年4月16日 星期三

[除錯] 控制項Dock Left(Up/Down/Right)在Panel時的dock順序

問題:
在設計模式,dock的順序會亂跳(我也不知程式是依什麼來做插入的順序??)

解決:
Dock Left
Form1.Designer.vb
需修改Panel1.Controls.Add順序

Code:
        Me.Panel1.Controls.Add(Me.Button5)
        Me.Panel1.Controls.Add(Me.Button4)
        Me.Panel1.Controls.Add(Me.Button3)
        Me.Panel1.Controls.Add(Me.Button2)
        Me.Panel1.Controls.Add(Me.Button1)

參考:

2014年3月23日 星期日

[除錯] System.InvalidCastException: System.Data.DataViewManagerListItemTypeDescriptor

原始碼:
Function_A 初始化ComboBox內容選擇值
                DataTable dt=new DataTable("ExchangeRateType");
                dt.Columns.Add("en", typeof(Int16));
                dt.Columns.Add("zh", typeof(string));
                dt.Rows.Add("0", "×(乘)");
                dt.Rows.Add("1", "÷(除)");
                cbo_ExchangeRateType.DisplayMember = "zh";
                cbo_ExchangeRateType.ValueMember = "en";
                cbo_ExchangeRateType.DataSource = dt;

                cbo_ExchangeRateUSDType.DisplayMember = "zh";
                cbo_ExchangeRateUSDType.ValueMember = "en";
                cbo_ExchangeRateUSDType.DataSource = dt.DefaultView ;

Function_B 從資料庫撈資料
                   dt = GetDataSet(OriginFindQry).Tables[0].Copy();
                    dt.TableName = _TblName;
                    if (ds.Tables.Contains(_TblName))
                    {
                        ds.Tables[_TblName].Dispose();
                        ds.Tables.Remove(_TblName);
                    }
                    ds.Tables.Add(dt);

Function_C 為每個控制項DataBinding
            foreach (Control item in this.tableLayoutPanel1.Controls)
            {
                item.DataBindings.Clear();
            }
            txt_CustomerOrderNo.DataBindings.Clear();
            txt_ExchangeRate.DataBindings.Clear();
            txt_ExchangeRateUSD.DataBindings.Clear();
            cbo_ExchangeRateType.DataBindings.Clear();
            cbo_ExchangeRateUSDType.DataBindings.Clear();

            bs_tblT訂單統計資料表.DataMember = null;
            bs_tblT訂單統計資料表.DataSource = null;
            bs_tblT訂單統計資料表.Clear();

            bs_tblT訂單統計資料表.DataMember = _TblName;
            bs_tblT訂單統計資料表.DataSource = ds;
           
            this.bindingNavigator1.BindingSource = bs_tblT訂單統計資料表;
            dataGridView1.DataSource = bs_tblT訂單統計資料表;

            cbo_ExchangeRateType.DataBindings.Add("SelectedValue", bs_tblT訂單統計資料表, "ExchangeRateType");
            cbo_ExchangeRateUSDType.DataBindings.Add("SelectedValue", bs_tblT訂單統計資料表, "ExchangeRateUSDType");



問題: 紫色底為錯誤發生點
(1)錯誤訊息:

            bs_tblT訂單統計資料表.DataMember = null;
            bs_tblT訂單統計資料表.DataSource = null;

            bs_tblT訂單統計資料表.Clear();

========
(2)錯誤訊息:
            //bs_tblT訂單統計資料表.DataMember = null;
            bs_tblT訂單統計資料表.DataSource = null;

            bs_tblT訂單統計資料表.Clear();

========
(3)錯誤訊息: 無法清除這個清單

            //bs_tblT訂單統計資料表.DataMember = null;
            //bs_tblT訂單統計資料表.DataSource = null;

            bs_tblT訂單統計資料表.Clear();

原因:


解決:
cbo_ExchangeRateType.DataBindings.Clear();

參考:
[除錯] 無法繫結至 DataSource 上的屬性或欄位 或 無法清除這個清單
疑難排解例外狀況:System.InvalidCastException
InvalidCastException 類別
关于模板控件如何实现多数据源绑定的问题

2014年2月10日 星期一

2013年12月24日 星期二

[除錯] 無法繫結至 DataSource 上的屬性或欄位 或 無法清除這個清單

原因:
Error:無法繫結至 DataSource 上的屬性或欄位
            bs_tblT內外銷統計附檔.DataMember = null;
            bs_tblT內外銷統計附檔.DataSource = null;

Error:無法清除這個清單
            bs_tblT內外銷統計附檔.Clear();

解決:
用bindingsource isbindingsuspended


            foreach (Control item in this.tableLayoutPanel1.Controls)
            {
                item.DataBindings.Clear();
            }
            lbl_檔案名稱.DataBindings.Clear();  
            //忘記把這個ctl給DataBindings.Clear()...
            //因為此ctl是屬於flowLayoutPanel2
            //而flowLayoutPanel2是屬於tableLayoutPanel1
            //所以此ctl我需要另外將其DataBindings.Clear()
            //而不是寫在foreach裡面...那邊只會找到flowLayoutPanel2

參考:
我的表單本來可以開啟的,因為我把access資料庫裡的欄位user改成user_1
[除錯] System.InvalidCastException: System.Data.DataViewManagerListItemTypeDescriptor
如何在dataTable更新完成後,才讓綁定的dataGridView 做更新?

2013年12月13日 星期五

[除錯] entilb DAAB [The type Database cannot be constructed. You must configure the container to supply this value]

 問題:
The type Database cannot be constructed. You must configure the container to supply this value.

Activation error occured while trying to get instance of type Database, key "ExpSys.Properties.Settings.ConnSqlStr"

解決:
組件[YC_ExpSys.exe]
設定檔[YC_ExpSys.exe.config]

當組件在組件[DayCensorWinApp.exe]上運行時
設定檔[DayCensorWinApp.exe.config]需新增設定檔[YC_ExpSys.exe.config]的設定資料
因為是由[DayCensorWinApp.exe]開啟[YC_ExpSys.exe]
而其設定檔是讀[DayCensorWinApp.exe.config]

環境:
entlib 5.0
vs2010
.net 3.5 sp1

參考:
The type Database cannot be constructed. You must configure the container to supply this value.
The type Database cannot be constructed. You must configure the container to supply this value (EntLib 5 + ODP.NET)
Activation error occured while trying to get instance of type Database, key "cnx"
Activation error occured while trying to get instance of type Database, key “” <— blank
Enterprise Library 5 with ODP.NET