Fill Combobox with Dapper

5

I'm using a combobox component with Dapper , but I'm not able to fill in the combobox correctly,

LookUpEdit ctlControle = (LookUpEdit) pr_Controle;
var parametro = new DynamicParameters();
parametro.Add("@TABELA_NOME", pr_TabelaNome);
parametro.Add("@TABELA_CAMPO_VALOR", pr_CampoValor);
parametro.Add("@TABELA_CAMPO_DISPLAY", pr_CampoDisplay);

ctlControle.Properties.DataSource = acessoDados.criarConexao().Query("PRP_PREENCHER_COMBO", parametro, commandType: CommandType.StoredProcedure);
ctlControle.Properties.ValueMember = pr_CampoValor;
ctlControle.Properties.DisplayMember = pr_CampoDisplay;
ctlControle.Properties.PopulateColumns();

So when adding to the datasource it fills in erroneously putting all the fields on the same line as devexpress lookupedit in>). How can I do this?

    
asked by anonymous 20.08.2015 / 01:54

1 answer

3

One thing Dapper does not clarify is that its return is not exactly structured the way a data source of a .NET component waits, so I wrote an extension method that converts the result to something more intelligible, such as a list, for example:

public static class DapperExtensions
{
    public static IEnumerable<T> ToTypedList<T>(this IEnumerable<dynamic> list, bool colunasMaiusculas = true)
        where T: class, new()
    {
        var properties = typeof(T).GetProperties();

        foreach (var element in list)
        {
            var obj = new T();
            foreach (var keyValue in ((IDictionary<string, object>) element).Where(e => e.Value != null)) 
            {
                PropertyInfo property;
                if (colunasMaiusculas) 
                { 
                    property = properties.FirstOrDefault(p => p.Name.ToUpper() == keyValue.Key);
                } else {
                    property = properties.FirstOrDefault(p => p.Name == keyValue.Key);
                }

                switch (property.PropertyType.ToString())
                {
                    case "System.Int32":
                        property.SetValue(obj, Convert.ToInt32(keyValue.Value));

                        break;
                    case "System.Int64":
                        property.SetValue(obj, Convert.ToInt64(keyValue.Value));

                        break;
                    case "System.DateTime":
                        property.SetValue(obj, Convert.ToDateTime(keyValue.Value));

                        break;
                    default:
                        if (keyValue.Value != null)
                        {
                            property.SetValue(obj, keyValue.Value);
                        }

                        break;
                }
            }

            yield return obj;
        }
    }
}

Use this:

ctlControle.Properties.DataSource = acessoDados.criarConexao().Query("PRP_PREENCHER_COMBO", parametro, commandType: CommandType.StoredProcedure).ToTypedList<ClasseDeDados>().ToList();

ClasseDeDados is a class that has the same fields that come from the database.

    
20.08.2015 / 02:06