本文介绍了当我的SelectedItem的组合框返回System.Data.DataRowView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是retrive两场在SQL中组合框的函数:
代码:

this is a function for retrive two field in sql to combobox :Code :

public void FillCmbKala()
    {
        cmbKala.Items.Clear();
        objCon.Connecting();
        string SQL = "SELECT [kID],[kName] FROM tblKala ORDER BY kName";
        DataSet ds = new DataSet();
        using (SqlConnection cn = new SqlConnection(objCon.StrCon))
        {
              using (SqlDataAdapter adapter = new SqlDataAdapter(SQL, cn))
              {

                  cn.Open();

              try
              {
                  adapter.Fill(ds);
              }
              catch (SqlException e)
              {

                  MessageBox.Show("There was an error accessing your data of 'Kala'. DETAIL: " + e.ToString());
              }
              finally
              {
                 cn.Close();
              }
          }
     }
     cmbKala.DataSource = ds.Tables[0];
     cmbKala.DisplayMember = "kName";
     cmbKala.ValueMember = "kID";
}



当我使用的组合框返回System.Data.DataRowView!

when i use the combobox return System.Data.DataRowView !

例如:

string str= cmbKala.SelectedItem;



的结果是:海峡= System.Data.DataRowView

result is : str= System.Data.DataRowView

推荐答案

下面是的。

由于的SelectedItem 返回对象。它返回选定特定行对象。这里是你将如何得到值:

As SelectedItem returns Object. It returns specific row object which is selected. Here is how you will get value:

DataRowView oDataRowView = cmbKala.SelectedItem as DataRowView;
string sValue = string.Empty;

if (oDataRowView != null) {
   sValue = oDataRowView.Row["kName"] as string;
}

这篇关于当我的SelectedItem的组合框返回System.Data.DataRowView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-20 02:55