Load C # combobox from another in Load ()

0

It's been so long since I was working with WinForms that I'm letting something go. In Load from my Form I call this method below:

private void CarregarEstados()
{
    cboEstado.DataSource = Listas.ListaEstados();
    cboEstado.DisplayMember = "UF";
}

public static DataTable ListaEstados()
{
    DataTable _table = null;

    try
    {
        var _connector = GetSQLInstance.getInstance();
        _table = _connector.GetTable(CommandType.StoredProcedure, "SPL_CREDENCIADOS_UF_PESQUISA");
    }
    catch (Exception Error)
    {
        LastErrorCode = MazeFire.ErrorManager.GetErrorCode(Error);
        LastErrorObject = Error;
        LastError = Error.Message;
    }

    return _table;
}

The called method returns a DataTable with a column called UF. It loads the combo smoothly.

However, I was asked to automatically load the Cities and Neighborhoods that are available in the DB. So, I do this in the event below:

private void cboEstado_SelectedIndexChanged(object sender, EventArgs e)
{
    // Assim o Combo sempre retorna vazio
    CarregarCidades(cboEstado.SelectedText);
}

private void CarregarCidades(string estado)
{
    cboCidade.DataSource = Listas.ListaCidades(estado);
    cboCidade.DisplayMember = "Cidade";
}

My problem occurs when calling the LoadCities, because I can not get the value set in the cboEstado (in case SP). Here in the example it is as cboEstado.SelectedText passing "" to the method, but I already tried cboEstado.SelectedValue.ToString() and cboEstado.SelectedItem.ToString() and both pass System.Data.DataRowView as parameter.

Could you explain what I'm letting go?

Thank you.

    
asked by anonymous 19.10.2018 / 15:38

1 answer

4

Good morning, Daniel Afonso. I believe your problem lies in the fact that when working with the Combobox.SelectedIndexChanged(sender, e) event, the ComboBox.SelectedText() property always comes as an empty string. The right thing is to work with ComboBox.SelectedItem() .

The ComboBox is a component that works with a dictionary of entries that are displayed in the drop-down list, and each entry has a related object.

When we populate the ComboBox with native value types (string, int, bool, etc.), the list is populated with entries 1 to 1, where the content of the drop-down list is (objeto_referido).ToString() . Now, when we fill in the ComboBox with reference types (objects), then we have to determine which property of the object will be its entry in the drop-down list. When a given list item is selected, the ComboBox.SelectedItem() property will return the reference to that object, not the list entry.

  

But I already tried cboEstado.SelectedValue.ToString () and cboEstado.SelectedItem.ToString () and both pass System.Data.DataRowView

This happens because you are trying to get an explicit cast to a type (Data.DataRowView) that does not have an implementation of this function. When that happens, dotnet simply returns the "full path" of the object type.

Solution in your code

There would be a million possible approximations to get around this, but I think the easiest would be to get the call of the CarregarCidades(string) function, change the parameter cboEstado.SelectedText() to (cboEstado.SelectedItem).Row.Item["UF"].ToString() .

Fonts

link

link

link

    
19.10.2018 / 16:02