繁体   English   中英

组合框显示system.data.datarowview

[英]The combobox displays system.data.datarowview

我在winform中有一个组合框,它通过在MySQL中调用存储过程来获取数据。

我的存储过程:

CREATE PROCEDURE `GetCourses`()
BEGIN
SELECT course_name FROM my_db.courses where group_id=1;
END

现在,将课程名称与Combobox(ComboBox2)绑定,如下所示-在选择另一个Combobox(ComboBox1)时:

private void Form_Load(object sender, EventArgs e)
{
  var connectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
  conn = new MySqlConnection(connectionString);
  conn.Open();
  MySqlCommand cmd1 = new MySqlCommand();
  cmd1.Connection = conn;
  cmd1.CommandType = CommandType.StoredProcedure;
  cmd1.CommandText = "GetCourses";
  DataTable dt1 = new DataTable();
  MySqlDataAdapter adp1 = new MySqlDataAdapter(cmd1);
  adp1.Fill(dt1);
}

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
  if (comboBox1.SelectedIndex == 3)
  {
    comboBox2.ValueMember = "course_name";
    comboBox2.DisplayMember = "course_name";
    comboBox2.DataSource = dt1;
  }
}

但是,当我运行表单时,组合框填充为“ system.data.datarowview”值

形成

谁能帮我这个忙。

注意:我不想通过使用'MySqlDataReader'实现

提前致谢。

只需通过下面两行代码即可解决。

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
  if (comboBox1.SelectedIndex == 3)
  {
    foreach (DataRow row in dt1.Rows)
      comboBox2.Items.Add(row["course_name"]);
  }
}    

您可以将数据投影到具有命名字段的集合中,以防止datarow对象的默认ToString()ing:

 if (comboBox1.SelectedIndex == 3)
  {
    comboBox2.ValueMember = "course_name_value";
    comboBox2.DisplayMember = "course_name";
    comboBox2.DataSource = dt1.AsEnumerable().Select 
       (n => new { course_name = n["course_name"], course_name_value = n["course_name"]}).ToList();
  }

编辑

我认为您应该将这些行放在Load事件中。 您不需要多次设置它们,这可能是组合框显示获取对象的ToString()结果而不是单个属性的原因。

comboBox2.ValueMember = "course_name";
comboBox2.DisplayMember = "course_name";

我运行了一个模拟测试,我认为它是在完成selectedIndex更改事件之前(在完成OnLoad事件时)处置您的数据集。 尝试让您的SelectedIndexChanged事件引发一个函数来填充第二个框。 PS,别介意我使用SQLite数据库进行测试。

Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
        If (ComboBox1.SelectedIndex = 3) Then            
            Select3()    
        End If
    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load    
        Dim range() As String = {"0", "1", "2", "3 - Fire Combo2", "4", "5", "6"}
        ComboBox1.Items.AddRange(range)            
    End Sub

    Private Sub Select3()
        Dim connectionString As String = MyStandAloneDB.DBConnStr
        Dim conn As New System.Data.SQLite.SQLiteConnection(connectionString)
        conn.Open()
        Dim cmd1 As New System.Data.SQLite.SQLiteCommand
        cmd1.Connection = conn
        cmd1.CommandType = CommandType.Text
        cmd1.CommandText = "SELECT * FROM Foo"
        Dim dt1 As New DataTable()
        Dim adp1 As New System.Data.SQLite.SQLiteDataAdapter(cmd1)
        adp1.Fill(dt1)
        ComboBox2.DataSource = dt1
        ComboBox2.ValueMember = dt1.Columns(1).ToString
        ComboBox2.DisplayMember = dt1.Columns(0).ToString
    End Sub

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM