Fill Grid dynamically with Dapper framework return

Asked

Viewed 285 times

3

I have a screen written in VB.NET Winforms, where the goal of it will be to execute SQL commands without the need to open a sql studio.

For simpler commands like delete, update, alter table among others it already works perfectly. Now, the idea is to create a gridview dynamically when the command is a select. Basically what Management Studio itself does when we run a SELECT command, create the columns and fill in with the returned data.

I am using Dapper Framework to execute the commands, and the return of it is a "Dapperrow" that I can’t handle or traverse.

I would like to know a way to create this kind of functionality, which is, run a SELECT, pick up the return, and from this return I fill my gridview dynamically with the data returned from SELECT.

Thank you in advance.

  • Put the line you are trying to run!

1 answer

2


DapperRow can be treated as a dynamic object. I took this idea of an old code of mine. Help, but I admit that can do better. I think this is kind of slow.

public statis class DapperExtensions
{
    /// <summary>
    /// Transforma uma lista de DapperRows em uma lista tipada. 
    /// </summary>
    /// <typeparam name="T">Uma classe que seja um Model.</typeparam>
    /// <param name="list">A lista de DapperRows.</param>
    /// <returns></returns>
    public static IEnumerable<T> ToTypedList<T>(this IEnumerable<dynamic> list)
        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 = properties.FirstOrDefault(p => p.Name == keyValue.Key);
                }

                if (property == null) continue;

                switch (property.PropertyType.ToString())
                {
                    case "System.Int16":
                        if (!String.IsNullOrEmpty(keyValue.Value.ToString()))
                        {
                            property.SetValue(obj, Convert.ToInt16(keyValue.Value));
                        }

                        break;
                    case "System.Int32":
                        if (!String.IsNullOrEmpty(keyValue.Value.ToString()))
                        {
                            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;
                    case "System.Decimal":
                        property.SetValue(obj, Convert.ToDecimal(keyValue.Value));

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

                        break;
                }
            }

            yield return obj;
        }
    }
}

Use:

var listaTipada = listaDeDapperRows.ToTypedList<MeuModel>();

Browser other questions tagged

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