顯示具有 ComboBox 標籤的文章。 顯示所有文章
顯示具有 ComboBox 標籤的文章。 顯示所有文章

2015年1月20日 星期二

[學習] 當二個以上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年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 類別
关于模板控件如何实现多数据源绑定的问题

2013年2月5日 星期二

[除錯] ComboBox 繫結 DataSource 顯示 System.Data.DataRowView

程式:
Dim sqlQuery1 As String = "SELECT (convert(nvarchar(4),系統ID) + '(' + 系統名稱 + ')') as 系統 FROM [系統別資料表] WHERE 系統ID <> 0;"
dt_Sys = basic.reDataset(sqlQuery1, Nothing, Nothing)

   For Each row As DataRow In dt_Sys.Rows
        If row.Item(0).ToString = "0" Then
             'way1 / way2
             ComboBox1.Items.Add(row.Item(0).ToString)
        End If
     Next

'dataTable 是可以顯示正常的...
'MsgBox(dt_Sys.Rows(0)("系統").ToString)

(way1) combobox1 每個row都顯示 System.Data.DataRowView
ComboBox1.DisplayMember = "系統"
ComboBox1.ValueMember = "系統"
ComboBox1.DataSource = dt_Sys
[C# CODE] 參考... 轉型 DataRowView
foreach(var dr in cbo_客戶編號.Items)
{
    MessageBox.Show(((DataRowView)dr)["中文名稱"].ToString());
}


(way2) 正常
For Each row As DataRow In dt_Sys.Rows
     ComboBox1.Items.Add(row.Item(0).ToString)
Next

備註:
我的程式裡並沒有 SelectedIndexChanged & SelectedValueChanged 的事件...
參考的資料無法解決問題…
還是(use way2)照原方式處理
但效率比較差...

2013/02/18 改way1 又正常了XD
應該跟以下有關係: (把obj的方式 變更為 datatable 就解決了)
form1
    'cbbox1 系統 資料繫結
    Dim dt_Sys As DataTable = New DataTable
    Public Property _dt_Sys() As DataTable
        Get
            _dt_Sys = dt_Sys
        End Get
        Set(ByVal value As DataTable)
            dt_Sys = value
        End Set
    End Property

    'Private obj As Object()
    'Public Property cbbox1() As DataTable
    '    Get
    '        cbbox1 = obj
    '    End Get
    '    Set(ByVal value As Object)
    '        obj = value
    '    End Set
    'End Property

     '將 combobox1 的item 複製到 系統別的combobox1
     'obj = New Object(ComboBox1.Items.Count - 1) {}
     'ComboBox1.Items.CopyTo(obj, 0)

form2
      '將form1的combobox1上的所有item 加入到 form2的combobox1
      'ComboBox1.Items.AddRange(Me.MyCallForm.cbbox1)

參考:
combobox.DisplayMember property
自訂ComboBox的DisplayMember及ValueMember
DataSet&DataTable如何绑定下拉列表控件获取不重复的值,如何添加一列自定义文字,并在下拉列表控件中显示自定义文字
Combobox出现System.Data.DataRowView的原因,以及指定ValueMember的时机问题
C#comboBox的DataSource获取数据显示System.Data.DataRowView
#VB ComboBox 問題集

2012年11月8日 星期四

[學習] Controls.Find with Option Strict On


DirectCast


原始為---
Option Strict Off
            For i As Integer = 1 To 10
                Dim C As TextBox = Me.Controls.Find("TextBox" & i, True)(0)
                If i = 10 Then
                    C.Text = arr(i + 4).ToString
                Else
                    C.Text = arr(i + 1).ToString
                End If
            Next
        End If

更改為---

Option Strict On

會發生 Error 隱含轉換
錯誤1 Option Strict On 不允許從 'System.Windows.Forms.Control' 到 'System.Windows.Forms.TextBox' 的隱含轉換。
錯誤2 Option Strict On 不允許從 'System.Windows.Forms.Control' 到 'System.Windows.Forms.ComboBox' 的隱含轉換。
錯誤3 Option Strict On 不允許從 'System.Windows.Forms.Control' 到 'System.Windows.Forms.DataGridView' 的隱含轉換。

故改為---

For i As Integer = 1 To 10
      Dim C As TextBox = DirectCast(Me.Controls.Find("TextBox" & i, True)(0), TextBox)                     
           If i = 10 Then
               C.Text = arr(i + 4).ToString
           Else
               C.Text = arr(i + 1).ToString
           End If
Next

錯誤4 Option Strict On 不允許從 'Object' 到 'String' 的隱含轉換

DataGridView1.Rows(i).Cells(2).Value = _
Trim(DataGridView1.Rows(i).Cells(2).Value) + Trim(DataGridView1.Rows(i).Cells(3).Value)

故改為---

DataGridView1.Rows(i).Cells(2).Value = _
Trim(DataGridView1.Rows(i).Cells(2).Value.ToString) + Trim(DataGridView1.Rows(i).Cells(3).Value.ToString)