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

问题描述

这是一个将 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;

结果是: str= System.Data.DataRowView

result is : str= System.Data.DataRowView

推荐答案

这里是 MSDN 参考.

As 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