天天看點

CombBox 的selectedvalue 的第一次綁定時的問題

            http://social.msdn.microsoft.com/Forums/zh-CN/csharpgeneral/thread/2eae1861-76fa-4f46-9142-0c37e59fa471

Hi,

I am trying to do it: when a combo is loaded or user selects an item, I want to read the SelectedValue of the combo in order to get the ID of a table to get data.

I have the next code in the  SelectedIndexChanged event of the combo:

            if (comboCursoActual.SelectedValue==null)

                return;

            if (comboCursoActual.SelectedValue.ToString() == "")

                return;

            string sql = "SELECT CENTRO_ID FROM AÑO_ACADEMICO WHERE ID=" + comboCursoActual.SelectedValue;

  ......

But when the combo is loaded (when form loaded) the SelectedValue contains the value of "System.Data.DataRowView" insted of the ID (and sql statment crashes) . It hapens when the combo is populated with data from the database (databinding).

How can I fix it? Is possible that SelectedValue has not been yet filled with data?

Thanks

碰到了上述問題,下面有個方法可以:

DataRowView firstdrv = (DataRowView)productionSearchclasscb.ComboBox.SelectedValue;
            string firstStr = firstdrv.Row.ItemArray[productionSearchclasscb.SelectedIndex].ToString();
            
           

搞定了。

以下是完整的程式片段:

private void productionSearchclasscb_SelectedIndexChanged(object sender, EventArgs e)
        {
            string sql = null;
            DataRowView firstdrv = (DataRowView)productionSearchclasscb.ComboBox.SelectedValue;
            string firstStr = firstdrv.Row.ItemArray[productionSearchclasscb.SelectedIndex].ToString();
                       
            switch(productionSearchcb.SelectedIndex)
            {
                case -1://空白行
                case 0://産品名稱
                case 1://産品拼音
                case 2://産品條碼
                case 5://産品工廠編号
                    break;
                case 3://産品分類
                   
                    if (productionSearchclasscb.SelectedIndex > -1)
                    {
                        sql = "Select TOP 100 ProductionID,ProductionName,ProductionPingYin,ProductionPrice,ProductionBuyPrice,ProductionBarCode,ProductionClass,ProductionQty,ProductionFactory,ProductionMark,ProductionOrderQty,EnterDate From Pro_ProductionTable Where" +
                                  " ProductionClass  = "+firstStr;                        
                        try
                        {                           
                            commUse.DataGridViewReset(ProductionDGV);
                            ProductionDGV.DataSource = db.GetDataTable(sql, "ProductionTable");
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show(ex.Message, "軟體提示");
                            throw ex;
                        }
                    }
                    break;
                case 4://産品工廠
                   
                    if (productionSearchclasscb.SelectedIndex > -1)
                    {
                        sql = "Select TOP 100 ProductionID,ProductionName,ProductionPingYin,ProductionPrice,ProductionBuyPrice,ProductionBarCode,ProductionClass,ProductionQty,ProductionFactory,ProductionMark,ProductionOrderQty,EnterDate From Pro_ProductionTable Where" +
                                  " ProductionFactory  = " + firstStr;
                        try
                        {
                            commUse.DataGridViewReset(ProductionDGV);
                            ProductionDGV.DataSource = db.GetDataTable(sql, "ProductionTable");
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show(ex.Message, "軟體提示");
                            throw ex;
                        }
                    }
                    break;

                default:
                    break;
            }
        }
           

問題又來了,這樣子是解決了一個初始化的時候會出System.Data.DataRowView的問題,但是再次選擇清單中的資料的時候無法繼續下去了。沒法做了~!

隻能再改,再改了下一個新的思路,初始化的時候會讀列出錯,那就屏蔽它去。

string sql = null;
           
         
            if (productionSearchclasscb.SelectedIndex > -1)
            {
           
                if (productionSearchclasscb.ComboBox.SelectedValue.ToString() == "System.Data.DataRowView")
                {
                    MessageBox.Show("初始化", "軟體提示");
                }
                else
                {
                    sql = "Select TOP 100 ProductionID,ProductionName,ProductionPingYin,ProductionPrice,ProductionBuyPrice,ProductionBarCode,ProductionClass,ProductionQty,ProductionFactory,ProductionMark,ProductionOrderQty,EnterDate From Pro_ProductionTable Where" +
                              " ProductionClass  = " + productionSearchclasscb.ComboBox.SelectedValue.ToString();
                    try
                    {
                        commUse.DataGridViewReset(ProductionDGV);
                        ProductionDGV.DataSource = db.GetDataTable(sql, "ProductionTable");
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message, "軟體提示");
                        throw ex;
                    }
                }
            }
           

腦殼痛。