Combobox C#,用DataTable填充,但在我的数据库中选择了ID。

时间:2015-07-31 14:04:09

标签: c# visual-studio combobox

Combobox C#,用DataTable填充,但在我的数据库中选择了ID。

我的表是PRODUTO,ID为PRODUTO.PRO_LOCAL 表LOCAL: 1 - 萨帕托斯 2 - Roupas 3 - Acessórios

PRODUTO.PRO_LOCAL = 2,但我的组合框填充顶部LOCAL = 1,这是查询中的第一个。

填充组合框的DataTable:

angular.module('MyApp')
.config(function($mdThemingProvider, $mdIconProvider){
    $mdIconProvider
        .defaultIconSet('img/icons/sets/core-icons.svg', 24);

    $mdThemingProvider.theme('default')
        .primaryPalette('red')
        .accentPalette('red');
    })

.controller('ListCtrl', function($scope, $mdDialog) {
  $scope.accounts = [
    {id: 1, title: "Account A", selected: false},
    {id: 2, title: "Account B", selected: false},
    {id: 3, title: "Account C", selected: false},
  ];
...

Form FrmProduto中的Combobox:

public DataTable RetornaLocal()
    {
        SqlConnection sqlConnection = acessoDadosSqlServer.CriarConexao();
        sqlConnection.Open();
        SqlCommand sqlCommand = sqlConnection.CreateCommand();
        sqlCommand.CommandText = "SELECT loc_cod, loc_descricao FROM local ORDER BY loc_descricao";

        SqlDataReader sqlDataReader = null;
        sqlDataReader = sqlCommand.ExecuteReader();

        DataTable dataTable = new DataTable();

        dataTable.Load(sqlDataReader);
        return dataTable;

    }

我希望FrmProduto在更改模式下显示表PRODUTO的ID而不是查询返回LOCAL规范

1 个答案:

答案 0 :(得分:0)

设置了SelectedValue - > produto.pro_local字段。 怎么做:

        cbLocal.DisplayMember = "loc_descricao";
        cbLocal.ValueMember = "loc_cod";
        cbLocal.DataSource = localNegocios.RetornaLocal();
        cbLocal.SelectedValue = produto.pro_local ;