Click here to Skip to main content
16,015,756 members
Home / Discussions / C#
   

C#

 
GeneralRe: Move Up And Down rows in datagridview and update it in database Pin
Eddy Vluggen4-Mar-18 1:41
professionalEddy Vluggen4-Mar-18 1:41 
QuestionUse from dataReader to bind data to a data gridview Pin
Member 133258462-Mar-18 10:38
Member 133258462-Mar-18 10:38 
AnswerRe: Use from dataReader to bind data to a data gridview Pin
Eddy Vluggen2-Mar-18 12:35
professionalEddy Vluggen2-Mar-18 12:35 
GeneralRe: Use from dataReader to bind data to a data gridview Pin
Member 133258463-Mar-18 0:06
Member 133258463-Mar-18 0:06 
SuggestionRe: Use from dataReader to bind data to a data gridview Pin
Richard Deeming5-Mar-18 8:02
mveRichard Deeming5-Mar-18 8:02 
GeneralRe: Use from dataReader to bind data to a data gridview Pin
Member 1332584616-Mar-18 1:45
Member 1332584616-Mar-18 1:45 
Questiona simpler binding technique for WinForms ? request for code review Pin
BillWoodruff1-Mar-18 2:22
professionalBillWoodruff1-Mar-18 2:22 
AnswerRe: a simpler binding technique for WinForms ? request for code review Pin
Richard Deeming1-Mar-18 3:49
mveRichard Deeming1-Mar-18 3:49 
I don't think it makes sense to inherit from List<T>. It would be almost impossible to ensure that the SyncObjects list had the same number of items as the outer list.

I was also slightly confused by the value stored in the list, until I realised it was meant to be the value of the control at the specified index. It would probably be better to make that a generic type parameter as well.

Perhaps something like this?
C#
public class ControlUpdater<TControl, TValue> : IReadOnlyList<TValue>
{
    private readonly List<(TControl control, TValue value)> _items = new List<(TControl, TValue)>();
    private readonly Func<TControl, TValue, bool> _setValue;
    
    public ControlUpdater(Func<TControl, TValue, bool> setValue, IEnumerable<(TControl, TValue)> items)
    {
        _setValue = setValue ?? throw new ArgumentNullException(nameof(setValue));
        if (items != null) AddRange(items);
    }
    
    public ControlUpdater(Func<TControl, TValue, bool> setValue, params (TControl, TValue)[] items) : this(setValue, items.AsEnumerable())
    {
    }
    
    public ControlUpdater(Func<TControl, TValue, bool> setValue, IEnumerable<TControl> items)
    {
        _setValue = setValue ?? throw new ArgumentNullException(nameof(setValue));
        if (items != null) AddRange(items);
    }
    
    public ControlUpdater(Func<TControl, TValue, bool> setValue, params TControl[] items) : this(setValue, items.AsEnumerable())
    {
    }
    
    public ControlUpdater(Func<TControl, TValue, bool> setValue) : this(setValue, default(IEnumerable<(TControl, TValue)>))
    {
    }
    
    public void AddRange(IEnumerable<(TControl, TValue)> items) => _items.AddRange(items);
    
    public void AddRange(IEnumerable<TControl> items, TValue value = default(TValue)) => _items.AddRange(items.Select(c => (c, value)));
    
    public void Add((TControl, TValue) item) => _items.Add(item);
    
    public void Add(TControl control, TValue value = default(TValue)) => _items.Add((control, value));
    
    public void Insert(int index, (TControl, TValue) item) => _items.Insert(index, item);
    
    public void Insert(int index, TControl control, TValue value = default(TValue)) => _items.Insert(index, (control, value));
    
    public void RemoveAt(int index) => _items.RemoveAt(index);
    
    public int Count => _items.Count;
    
    public TValue this[int index]
    {
        get 
        {
            var (_, value) = _items[index];
            return value;
        }
        set 
        {
            var (control, _) = _items[index];
            if (_setValue(control, value))
            {
                _items[index] = (control, value);
            }
        }
    }
    
    public IEnumerator<TValue> GetEnumerator()
    {
        foreach (var (_, value) in _items)
        {
            yield return value;
        }
    }
    
    IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
}
Usage:
C#
var tba = new ControlUpdater<TextBox, int>(
    (tbx, value) =>
    {
        tbx.Text = value.ToString();
        return true;
    });

var controls = Enumerable.Range(0, 5).Select(i => new TextBox { Name = $"Ints{i + 10}" });
tba.AddRange(controls);

tba[3] = 1000;     // as expected
var xxx = tba[3];  // as expected




"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer


GeneralRe: a simpler binding technique for WinForms ? request for code review Pin
BillWoodruff1-Mar-18 7:27
professionalBillWoodruff1-Mar-18 7:27 
AnswerRe: a simpler binding technique for WinForms ? request for code review Pin
Eddy Vluggen1-Mar-18 9:23
professionalEddy Vluggen1-Mar-18 9:23 
GeneralRe: a simpler binding technique for WinForms ? request for code review Pin
BillWoodruff1-Mar-18 23:53
professionalBillWoodruff1-Mar-18 23:53 
GeneralRe: a simpler binding technique for WinForms ? request for code review Pin
Eddy Vluggen2-Mar-18 4:20
professionalEddy Vluggen2-Mar-18 4:20 
GeneralRe: a simpler binding technique for WinForms ? request for code review Pin
BillWoodruff2-Mar-18 4:58
professionalBillWoodruff2-Mar-18 4:58 
GeneralRe: a simpler binding technique for WinForms ? request for code review Pin
Eddy Vluggen2-Mar-18 5:18
professionalEddy Vluggen2-Mar-18 5:18 
GeneralRe: a simpler binding technique for WinForms ? request for code review Pin
BillWoodruff2-Mar-18 6:33
professionalBillWoodruff2-Mar-18 6:33 
QuestionRe: a simpler binding technique for WinForms ? request for code review Pin
Eddy Vluggen2-Mar-18 8:27
professionalEddy Vluggen2-Mar-18 8:27 
AnswerRe: a simpler binding technique for WinForms ? request for code review Pin
BillWoodruff4-Mar-18 14:50
professionalBillWoodruff4-Mar-18 14:50 
GeneralRe: a simpler binding technique for WinForms ? request for code review Pin
Eddy Vluggen5-Mar-18 1:30
professionalEddy Vluggen5-Mar-18 1:30 
AnswerRe: a simpler binding technique for WinForms ? request for code review Pin
Gerry Schmitz2-Mar-18 6:20
mveGerry Schmitz2-Mar-18 6:20 
GeneralRe: a simpler binding technique for WinForms ? request for code review Pin
BillWoodruff2-Mar-18 6:27
professionalBillWoodruff2-Mar-18 6:27 
GeneralRe: a simpler binding technique for WinForms ? request for code review Pin
Gerry Schmitz2-Mar-18 6:43
mveGerry Schmitz2-Mar-18 6:43 
QuestionNeed to export data from sql server to excel sheet in windows application Pin
Shankar M28-Feb-18 23:36
Shankar M28-Feb-18 23:36 
AnswerRe: Need to export data from sql server to excel sheet in windows application Pin
Richard MacCutchan28-Feb-18 23:56
mveRichard MacCutchan28-Feb-18 23:56 
GeneralRe: Need to export data from sql server to excel sheet in windows application Pin
Shankar M1-Mar-18 0:07
Shankar M1-Mar-18 0:07 
AnswerRe: Need to export data from sql server to excel sheet in windows application Pin
Gerry Schmitz1-Mar-18 8:01
mveGerry Schmitz1-Mar-18 8:01 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.