Ejemplo n.º 1
0
        public static void BindData(this ListBox listBox, object collection, string valueField, string textField, string groupField = null)
        {
            listBox.Items.Clear();

            foreach (var item in (IList)collection)
            {
                if (string.IsNullOrEmpty(groupField))
                {
                    listBox.AddSelectItem(item?.GetType()?.GetProperty(textField)?.GetValue(item, null)?.ToString(), item?.GetType()?.GetProperty(valueField)?.GetValue(item, null)?.ToString());
                }
                else
                {
                    listBox.AddSelectItem(item?.GetType()?.GetProperty(textField)?.GetValue(item, null)?.ToString(), item?.GetType()?.GetProperty(valueField)?.GetValue(item, null)?.ToString(), item?.GetType()?.GetProperty(groupField)?.GetValue(item, null)?.ToString());
                }
            }
        }