Pre-select Last Record in a Dropdownlist c#

Asked

Viewed 258 times

1

I’m assigning a list to the DataSource, and in this assignment I am concatenating the code with the company name, however, it loads the list with the first selected item. How do I have the last record loaded preselected ?

My code is like this:

    for (int i = 0; i < empresa.Count; i++)
    {
            empresa[i].ParEmpresa = empresa[i].ParCodigo + " - " + empresa[i].ParEmpresa;
    }
    ddlEmpIni.DataSource = empresa;
    ddlEmpIni.DataTextField = "ParEmpresa";
    ddlEmpIni.DataValueField = "ParCodigo";
    ddlEmpIni.DataBind();
  • Have Tried? ddlEmpIni.SelectedValue = ddlEmpIni.items[ddlEmpIni.items.count].ParCodigo

  • Thanks @Marconi already got with the company[i],Parcodigo inside the loop.

1 answer

1


Do so

    for (int i = 0; i < empresa.Count; i++)
    {
            empresa[i].ParEmpresa = empresa[i].ParCodigo + " - " + empresa[i].ParEmpresa;
    }
    ddlEmpIni.DataSource = empresa;
    ddlEmpIni.DataTextField = "ParEmpresa";
    ddlEmpIni.DataValueField = "ParCodigo";
    ddlEmpIni.DataBind();
    ddlEmpIni.SelectedValue = empresa[empresa.Count()].ParCodigo;
  • does not have the property "length" brother.

  • Use Count(), anyway, take the last element, you can even use i

  • Didn’t work @Paulohdsousa

  • uses I then... the one that Voce used in i++, if it doesn’t work use i - 1

Browser other questions tagged

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