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;
  }
  }