Advertisement
paulluscastro

Preencher ComboBox (usando DataTable)

May 22nd, 2018
348
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.86 KB | None | 0 0
  1.         private void PreencherCombo(ComboBox combo)
  2.         {
  3.             // Usando DataTable, também é possível fazer usando BindingSource...
  4.             DataTable tabela = new DataTable();
  5.             tabela.Columns.Add("Id");
  6.             tabela.Columns.Add("Name");
  7.             DataRow registro = tabela.NewRow();
  8.             registro["Id"] = -1;
  9.             registro["Name"] = "";
  10.             tabela.Rows.Add(registro);
  11.             foreach(Sexo sexo in Sexo.List())
  12.             {
  13.                 registro = tabela.NewRow();
  14.                 registro["Id"] = sexo.Id;
  15.                 registro["Name"] = sexo.Name;
  16.                 tabela.Rows.Add(r);
  17.             }
  18.             combo.Items.Clear();
  19.             combo.ValueMember = "Id";
  20.             combo.DisplayMember = "name";
  21.             combo.DataSource = tabela;
  22.             combo.SelectedIndex = -1;
  23.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement