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

C#

 
GeneralRe: Move Up And Down rows in datagridview and update it in database Pin
Member 133258463-Mar-18 18:47
Member 133258463-Mar-18 18:47 
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 
Good idea ? Bad idea ? Would you ever use in your own code ? Code smells ?

A recent thread here [^] asked for help with (one-way) binding a group of TextBoxes to integer valus in an Array. I responded with an example of using 'ObservableCollection.

The first version I hard-coded (for POC) to match the thread cited's requirement; then, I had an idea that with such a simple one-way binding scenario a Class inheriting from List<T> ... where the generic parameter represented the Type of objects to be synced ...with a custom indexer ... could work for similar situations.

The next version, I made objects to be synced to the array generic, and put List value change code in a Func template to do the work of whatever a change in the Array/List values required. Here's the code:
public class TUpDater<T> : List<int>
{
    public TUpDater(Func<int, int, T, bool> mapfunc, params T [] syncobjects)
    {
        MapFunc = mapfunc;

        SyncObjects = new List<T>();

        for (int i = 0; i < syncobjects.Length; i++)
        {
            SyncObjects.Add(syncobjects[i]);
        }
    }

    public Func<int, int, T, bool> MapFunc { set; get; }

    public List<T> SyncObjects { get; } 

    // indexer override
    public new int this[int ndx]
    {
        set
        {
            base[ndx] = value;

            if (MapFunc?.Invoke(ndx, value, SyncObjects[ndx]) !=  null)
            {
                // for future use
            }
        }

        get
        {
            return base[ndx];
        }
    }
}
Here's a usage example:
TUpDater<TextBox> tba;
List<TextBox> TBoxes = new List<TextBox>();
List<int> Ints { set;  get; }

private void Form1_Load(object sender, System.EventArgs e)
{
    tba = new TUpDater<TextBox>(
        (ndx, val, tbx) =>
        {
            
            tbx.Text = val.ToString();
            return true;
        }
    );

    Ints =  new List<int> { 11, 22, 33, 44, 55 };
    tba.AddRange(Ints);

    for (int i = 0; i < 5; i++)
    {
        TextBox tb = new TextBox { Name = "Ints" + (i + 10), Text = i.ToString() };

        tba.SyncObjects.Add(tb);
    }

    tba[3] = 1000;     // as expected
  
    var xxx = tba[3];  // as expected
}
Comment: if I take this further, I think I'd provide an optional CTor overload where the user could pass both List Int values, and T objects.
«... thank the gods that they have made you superior to those events which they have not placed within your own control, rendered you accountable for that only which is within you own control For what, then, have they made you responsible? For that which is alone in your own power—a right use of things as they appear.» Discourses of Epictetus Book I:12

AnswerRe: a simpler binding technique for WinForms ? request for code review Pin
Richard Deeming1-Mar-18 3:49
mveRichard Deeming1-Mar-18 3:49 
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 

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.