2015年1月29日 星期四

[學習] MenuStrip 合併菜單

除錯:
mdiParent的MenuStrip若設定 Ctrl-X 等文字編輯快捷鍵時,當mdiChildren無MenuStrip設定...
則該視窗的 RichTextbox 會因 mdiParent 的快捷鍵設定而失效...

解決:
直接在mdiChildren設定MenuStrip並設定與mdiParent第一格相同的名稱,
並將該格enable設為false,MergeAction=Replace,MergeIndex設為要取代MenuStrip某欄的Index
無需再設定任何快捷鍵...
則RichTextbox的文字編輯快捷鍵立即復活...

參考:
【转】menustrip控件中mergeaction属性的作用
在 Windows 窗体 MenuStrip 控件中合并菜单项
合併 Windows Form MenuStrip 控制項中的功能表項目

2015年1月20日 星期二

[學習] 從一個datatable找某column的最大值

運用linq + lambda
或直接用datatable的彙總函式

e.Row.Cells("收退序號").Value = If(dgv_單據明細.Rows.Count > 1,
     tbl.Compute("max(收退序號)",
     "管理模式編號=" & cbo_管理模式編號.SelectedValue & _
     " AND 收退單號='" & txt_收退單號.Text & "'") + 1,
     1)

參考:
從查詢中建立 DataTable (LINQ to DataSet)
單一資料表查詢 (LINQ to DataSet)
DataTable: Get Max Value Using LINQ With Criteria Field (GroupBy)
How to select min and max values of a column in a datatable?
DataTable.Compute 方法

[學習] 當二個以上Combobox繫結相同來源時,若其中一個改變選項,則其它相同參考的Combobox不隨之改變....

程式:[用不同的BindingContext就好了]
        If MyUserProgInfo.frmLogin使用WebServices Then
            cbo_單據別1.DataSource = ws.ItemtblB備案手冊單據別基本資料表_GetMdl()
        Else
            cbo_單據別1.DataSource = New BLLMSSQL.BllBasicCtrlsSource().ItemtblB備案手冊單據別基本資料表
        End If
        cbo_單據別1.DisplayMember = "DisplayValue"
        cbo_單據別1.ValueMember = "單據別"

        cbo_單據別2.BindingContext = New BindingContext()
        cbo_單據別2.DataSource = cbo_單據別1.DataSource
        cbo_單據別2.DisplayMember = "DisplayValue"
        cbo_單據別2.ValueMember = "單據別"

參考:
WinForms ComboBox data binding gotcha
Multiple Combo Boxes With The Same Data Source (C#)

BindingContext 類別
关于控件使用DataBindings.Add方法进行简单绑定的问题

Bind multiple ComboBox to a single List - Issue: When I select an item, all combo boxes change

WinForm数据绑定--BindingContext
How can i bind data to a datagridview combobox column?

You could create a BindingList<> object to hold your objects and then bind that list to the three controls. Then setting the BindingContexts to different objects would let the 3 controls have different positions with a single list.
  
public partial class Form1 : Form
  {
  public Form1()
  {
    InitializeComponent();
  }
 
  private void Form1_Load(object sender, EventArgs e)
  {
    BindingList<MyObject> list = new BindingList<MyObject>();
    list.Add(new MyObject("Ramya", 43));
    list.Add(new MyObject("Manju", 43));
    list.Add(new MyObject("Gulnus", 43));
    list.Add(new MyObject("Sona", 43));
 
    dataGridView1.DataSource = list;
    comboBox1.DataSource = list;
    comboBox1.DisplayMember = "Name";
    comboBox1.ValueMember = "Name";
 
    comboBox2.DataSource = list;
    comboBox2.DisplayMember = "Name";
    comboBox2.ValueMember = "Name";
 
    comboBox1.BindingContext = new BindingContext();
    comboBox2.BindingContext = new BindingContext();
    dataGridView1.BindingContext = new BindingContext();
 
  }
  }
 
  public class MyObject
  {
  private string mName;
  private int mAge;
 
  public MyObject(string s, int i)
  {
    mName = s;
    mAge = i;
  }
  public string Name
  {
    get { return mName; }
    set { mName = value; }
  }
  public int Age
  {
    get { return mAge; }
    set { mAge = value; }
  }
  public override string ToString()
  {
    return Name;
  }
  }

2014年9月17日 星期三

2014年7月10日 星期四

[學習] 從自訂義類別中查詢 linq & lambda

說明:
主細表 Master-Detail
中幫USER自動瀘掉, Detail DGV某欄位(combobox)已存在的選項(ex: A 與 B)
當DGV AddNewRow時, 自動選出不包含A 與 B 的第一個選項(ex: C)

解決:
不知怎麼直接截取DGV當時對應於Master畫面的選項值
ex:
MasterA -> Detail A,B
MasterB -> Detail C

所以只好重新從D etail DataTable select 出來

籂選出DataRow
Dim b() As DataRow = CType(bs_tblB備案手冊號基本資料表進口項次.DataSource, DataSet).Tables(tblProductName).Select("手冊編號='" & txt_手冊編號.Text & "' AND 進口項次號=" & txt_進口項次號.Text)

將DataRow取其中一個欄位,並轉成陣列
Dim c = b.Select(Function(o) o("產品大類")).ToArray

飾選出來共有這麼多
MsgBox(CType(CType(dgv_項次產品.Columns("產品大類"), DataGridViewComboBoxColumn).DataSource, List(Of Mdl.tblBProductsType)).Where(Function(o) Not c.Contains(o.ProductsTypeID)).Count)

找出第一筆要的欄位資料
MsgBox(CType(CType(dgv_項次產品.Columns("產品大類"), DataGridViewComboBoxColumn).DataSource, List(Of Mdl.tblBProductsType)).Where(Function(o) Not c.Contains(o.ProductsTypeID))(0).ProductsTypeID)

參考:
逐步解說:使用兩個 Windows Form DataGridView 控制項建立主要/詳細表單
[學習] Master-Detail 父子表(主細表)

LINQ運算式基本構成子句
Lambda 運算式 (Visual Basic)
Visual Basic 中的 LINQ 簡介
[VB.NET]Lambda運算式
Lambda Expression for “not in”?
How to implement NOT IN clause in LINQ/LAMBDA Query expression?Explain with an example.
[C#][LINQ]動態組Where條件
How to select a field with linq on datarow
[LINQ] 查詢DataRow欄位(LINQ to DataSet)
VB.Net Linq - How to append a where clause?
How would you do a “not in” query with Linq?
Linq to Datarow, Select multiple columns as distinct?
Use LINQ to get items in one List<>, that are not in another List<>
LINQ - 在WHERE條件式中使用IN與NOT IN
LINQ to SQL 可以做到的功能
LINQ to Object - VB.NET
how to filter list in c# with lambda expression?
Filtering collections in C#

DataRow.GetChildRows 方法 (DataRelation)
比較 DataRow (LINQ to DataSet)

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