Load combobox C# from another in Load()

Asked

Viewed 207 times

0

It’s been so long since I’ve worked with Winforms, I’m missing something. In my Form Load 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. He carries the combo smoothly.

However, I was asked to automatically upload the Cities and Neighborhoods that are available in the BD. 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 Loadings, because I can’t get the set value in the state (in the SP case). There in the example is as cboEstado.SelectedText which passes "" to the method, but I’ve tried cboEstado.SelectedValue.ToString() and cboEstado.SelectedItem.ToString() and both pass by System.Data.DataRowView as a parameter.

Could you explain to me what I’m missing?

Thank you.

  • And your method Listas.ListaCidades(estado);? How are you?

  • I will edit above.. I put the Liststatus.. the Listcity and the Listneighborhood are the same in structure

  • @Danielafonso, It is not necessary for you to put solved in the title of the question. And it is not correct for you to present your answer in it too. Whether you should accept one of the proposals as a response or post your solution as a response and mark it yourself.

  • @Leandroangelo sorry about that! I’m new here and I’m still understanding how it works. I’ll fix.

1 answer

4


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

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 fill the Combobox with native types of value (string, int, bool, etc), the list is filled with entries 1 to 1, where the contents of the drop-down list are (objeto_referido).ToString(). Now, when filling Combobox with reference types (objects), ai has to determine which property of the object will be its entry in the drop-down list. When a particular list item is selected, the property ComboBox.SelectedItem() will return the reference to that object, not the list entry.

mas já tentei cboEstado.SelectedValue.ToString() e cboEstado.SelectedItem.ToString() e ambos passam System.Data.DataRowView

This is because you are trying to get an explicit conversion to a string of a type (Data.Datarowview) that does not have an implementation of this function. When this 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 in function call CarregarCidades(string), exchange the parameter cboEstado.SelectedText() for (cboEstado.SelectedItem).Row.Item["UF"].ToString().

Sources

https://docs.microsoft.com/pt-br/dotnet/api/system.windows.forms.combobox.selectedtext?view=netframework-4.7.2

https://docs.microsoft.com/pt-br/dotnet/api/system.data.datarowview?redirectedfrom=MSDN&view=netframework-4.7.2

https://docs.microsoft.com/pt-br/dotnet/api/system.data.datarowview.row?view=netframework-4.7.2#System_Data_DataRowView_Row

  • Thank you very much for the detailed clarification on the functioning of the control. I will take a look at the references you sent me and the example. ;)

  • Using your example gave error in Selecteditem, as it can not be used as method. But following your explanation and tips, I managed to solve. I’m going to post the main question, as it might help someone tbm. Thank you very much.

  • Thanks for already correcting me too. I will change here also to be concise and not confused.

  • 1

    @Umbertosantos Put your implemented solution in the author’s code, maybe then it will accept as an answer

Browser other questions tagged

You are not signed in. Login or sign up in order to post.