我正在尝试从SQL Server中的列表ID的标识中获取组合框数据,但遇到了检索错误

问题描述

我正在尝试从sql Server中列表ID的标识中获取组合框数据:

private void AddProductsScreen_Load(object sender,EventArgs e)
{
        if (!this.IsUpdate)
        {
        }

        LoadAllSizesInDataGridView();
        LoadDataIntoComboBoxes();
}

private void LoadDataIntoComboBoxes()
{
    ProductCategoryComboBox.DataSource = GetComboBoxData(3);
    ProductsupplierComboBox.DataSource = GetComboBoxData(2);
}

private DataTable GetComboBoxData(int listTypeID)
{
    DataTable dtrecords = new DataTable();

    using (sqlConnection con = new sqlConnection(ApplicationSetting.ConnectionString()))
    {
        using (sqlCommand cmd = new sqlCommand("usp_ListTypesDataIntoComboBox",con))
        {
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@ListTypeID",listTypeID);

            con.open();

            sqlDataReader sdr = cmd.ExecuteReader();
            dtrecords.Load(sdr);
        }
    }

    return dtrecords;
}

问题是我在大小的datagridview中获得了所有名称,并且在组合框中显示System.Data.DaTarowView

解决方法

您需要使用值配置组合框控件,并使用数据表列名称配置显示成员:

ProductCategoryComboBox.DisplayMember = "Key";
ProductCategoryComboBox.ValueMember = "Value";