logo
down
shadow

UltraWinGrid bound to BindingList<string>, Add Row Error


UltraWinGrid bound to BindingList<string>, Add Row Error

By : dadzky33
Date : November 22 2020, 10:38 AM
To fix the issue you can do A BindingList with AllowNew to true causes the bindinglist to create new items by reflection. What the bindinglist does is instantiate the generictype ( in this case string ) through reflection. This is also was causes the exception. Because the string valuetype doesnt have any constructor. Binding a own created type with a constructor is surely possible. In this case you can create a wrapper class:
code :
    public class MyList
    {
        public string ListItem { get; set; }

        public MyList( string listItem )
        {
            ListItem = listItem;
        }
    }


Share : facebook icon twitter icon
How do I render a column bound to a collection class as a child row in an Infragistics UltraWinGrid?

How do I render a column bound to a collection class as a child row in an Infragistics UltraWinGrid?


By : rkyymmt
Date : March 29 2020, 07:55 AM
I wish did fix the issue. Ok, think I've solved this one (albeit needs a little tidying up). The trick is to pull the "parent" data out into a List (currently with an ugly foreach loop) and bind the grid to that, ensuring you're grid is set to MultiBand.
Since the two child collections are also List collections it handles them automagically.
BindingList not updating bound ListBox

BindingList not updating bound ListBox


By : Hadi 'Hadijoe' Priba
Date : March 29 2020, 07:55 AM
I wish this helpful for you (jump right to the last example if you want to see it fixed etc)
Threads and "observer" patterns (such as the data-binding on winforms) are rarely good friends. You could try replacing your BindingList usage with the ThreadedBindingList code I used on a previous answer - but this combination of threads and UI is not an intentional use-case of winforms data-binding.
code :
using System;
using System.ComponentModel;
using System.Windows.Forms;
class Foo
{
    public int Value { get; set; }
    public Foo(int value) { Value = value; }
    public override string ToString() { return Value.ToString(); }
}
static class Program
{
    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        using(var form = new Form())
        using (var lst = new ListBox())
        using (var timer = new Timer())
        {
            var data = new BindingList<Foo>();
            form.Controls.Add(lst);
            lst.DataSource = data;
            timer.Interval = 1000;
            int i = 0;
            timer.Tick += delegate
            {
                data.Add(new Foo(i++));
            };
            lst.Dock = DockStyle.Fill;
            form.Shown += delegate
            {
                timer.Start();
            };
            Application.Run(form);
        }
    }
}
using System;
using System.ComponentModel;
using System.Threading;
using System.Windows.Forms;
class Foo
{
    public int Value { get; set; }
    public Foo(int value) { Value = value; }
    public override string ToString() { return Value.ToString(); }
}
static class Program
{
    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        using(var form = new Form())
        using (var lst = new ListBox())
        {
            form.Controls.Add(lst);            
            lst.Dock = DockStyle.Fill;
            form.Shown += delegate
            {
                BindingList<Foo> data = new ThreadedBindingList<Foo>();
                lst.DataSource = data;
                ThreadPool.QueueUserWorkItem(delegate
                {
                    int i = 0;
                    while (true)
                    {
                        data.Add(new Foo(i++));
                        Thread.Sleep(1000);
                    }
                });
            };
            Application.Run(form);
        }
    }
}
public class ThreadedBindingList<T> : BindingList<T>
{
    private readonly SynchronizationContext ctx;
    public ThreadedBindingList()
    {
        ctx = SynchronizationContext.Current;
    }
    protected override void OnAddingNew(AddingNewEventArgs e)
    {
        SynchronizationContext ctx = SynchronizationContext.Current;
        if (ctx == null)
        {
            BaseAddingNew(e);
        }
        else
        {
            ctx.Send(delegate
            {
                BaseAddingNew(e);
            }, null);
        }
    }
    void BaseAddingNew(AddingNewEventArgs e)
    {
        base.OnAddingNew(e);
    }
    protected override void OnListChanged(ListChangedEventArgs e)
    {
        if (ctx == null)
        {
            BaseListChanged(e);
        }
        else
        {
            ctx.Send(delegate
            {
                BaseListChanged(e);
            }, null);
        }
    }
    void BaseListChanged(ListChangedEventArgs e)
    {
        base.OnListChanged(e);
    }
}
Loop through my BindingList collection or the DataGridView to which it is bound

Loop through my BindingList collection or the DataGridView to which it is bound


By : user3319077
Date : March 29 2020, 07:55 AM
it should still fix some issue David Hall's comment(s) gave me enough confidence to go in this direction:
leave the datagridview as is from the toolbox to the form (i.e. do NOT loop thru the DGV) loop thru the BindingList collection
BindingList<T> is not showing up in the datagridview it is bound too

BindingList<T> is not showing up in the datagridview it is bound too


By : Trimurthy P
Date : March 29 2020, 07:55 AM
This might help you Try this, i have a feeling you have been adding the ItemList instance instead of listItems.
code :
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        ItemList list = new ItemList();
        list.listItems.Add(new Item() { name = "Sample" });
        list.listItems.Add(new Item() { name = "Sample" });
        list.listItems.Add(new Item() { name = "Sample" });
        list.listItems.Add(new Item() { name = "Sample" });
        list.listItems.Add(new Item() { name = "Sample" });
        list.listItems.Add(new Item() { name = "Sample" });

        dataGridView1.DataSource = list.listItems;

    }
}

public class Item : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    private string name_;

    public string name
    {
        get { return name_; }
        set
        {
            name_ = value;
            this.NotifyPropertyChanged("name");
        }
    }
    private void NotifyPropertyChanged(string name)
    {
        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(name));
    }
}

public class ItemList
{
    public BindingList<Item> listItems { get; set; }

    // Default constructor 
    public ItemList()
    {
        listItems = new BindingList<Item>();
    }
    public BindingList<Item> returnList()
    {
        return listItems;
    }
    public void addItem(Item newItem)
    {
        listItems.Add(newItem);
    }
} 
Sort DataGridView bound to BindingList by row header

Sort DataGridView bound to BindingList by row header


By : Esty Thomas
Date : March 29 2020, 07:55 AM
I hope this helps you . You have to use the ColumnHeaderMouseClick event and DataGridViewColumn.HeaderCell.SortGlyphDirection to sort a DataGridView with a Generic List as the DataSource.
Sample code is
code :
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
        Person p = new Person { Birthday = new DateTime(2006, 12, 21), FirstName = "Jane", LastName = "Smith" };
        Person p2 = new Person { Birthday = new DateTime(2007, 9, 11), FirstName = "Alice", LastName = "Doe" };
        lstPeople.Add(p);
        lstPeople.Add(p2);
        dgvPeople.DataSource = lstPeople;
        foreach (DataGridViewColumn col in dgvPeople.Columns)
        {
            col.SortMode = DataGridViewColumnSortMode.Programmatic;
        }
    }
    public List<Person> lstPeople = new List<Person>();
    /// <summary>
    /// Sort the DataGridView when Header Column is clicked
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void dgvPeople_ColumnHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
    {
        DataGridView grid = (DataGridView)sender;
        SortOrder so = SortOrder.None;
        if (grid.Columns[e.ColumnIndex].HeaderCell.SortGlyphDirection == SortOrder.None ||
            grid.Columns[e.ColumnIndex].HeaderCell.SortGlyphDirection == SortOrder.Ascending)
        {
            so = SortOrder.Descending;
        }
        else
        {
            so = SortOrder.Ascending;
        }
        //set SortGlyphDirection after databinding otherwise will always be none 
        Sort(grid.Columns[e.ColumnIndex].Name, so);
        grid.Columns[e.ColumnIndex].HeaderCell.SortGlyphDirection = so; 
    }
    /// <summary>
    /// Sort the DataGridView
    /// </summary>
    /// <param name="column"></param>
    /// <param name="sortOrder"></param>
    private void Sort(string column, SortOrder sortOrder)
    {
        switch (column)
        {
            case "FirstName":
                {
                    if (sortOrder == SortOrder.Ascending)
                    {
                        dgvPeople.DataSource = lstPeople.OrderBy(x => x.FirstName).ToList();
                    }
                    else
                    {
                        dgvPeople.DataSource = lstPeople.OrderByDescending(x => x.FirstName).ToList();
                    }
                    break;
                }
            case "LastName":
                {
                    if (sortOrder == SortOrder.Ascending)
                    {
                        dgvPeople.DataSource = lstPeople.OrderBy(x => x.LastName).ToList();
                    }
                    else
                    {
                        dgvPeople.DataSource = lstPeople.OrderByDescending(x => x.LastName).ToList();
                    }
                    break;
                }
            case "Birthday":
                {
                    if (sortOrder == SortOrder.Ascending)
                    {
                        dgvPeople.DataSource = lstPeople.OrderBy(x => x.Birthday).ToList();
                    }
                    else
                    {
                        dgvPeople.DataSource = lstPeople.OrderByDescending(x => x.Birthday).ToList();
                    }
                    break;
                }
        }

    }
}
public class Person
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public DateTime Birthday { get; set; }
}
Related Posts Related Posts :
  • Why does C# also not allow empty conditions in while loops?
  • Unable to get a block of code into my regex match groups
  • What is difference between dbcontext.Add and dbcontext.AddObject
  • How do I update the file version number of C# DLL without recompiling?
  • Value for html control always null
  • HttpClient hangs when timeout is setting (Windows Phone)
  • Handle Multiple Form tag in asp.net page?
  • create word document with html content in c#
  • Confusion about Find And Replace
  • Format sms messages in Clickatell
  • Automated Function Overload
  • String to date in MS Access SQL statement gives type mismatch error
  • ShowDialog exiting on certain events
  • Is there a try Convert.ToInt32... avoiding exceptions
  • How to move wpf application into minimize tray at Window Start-up C#?
  • How to change display format of long variable?
  • How to use Addfields in MongoDB C# Aggregation Pipeline
  • MsTest TestCleanup method not called when an unhandled exception is thrown
  • missing last data when exporting gridview to excel
  • How to add array of objects to List in c#
  • Lambda Expression to order (sort) my list collection
  • Library for displaying music notation
  • How to compare two dictionaries in c# and get the output as True and False after validation
  • specify fields to be serialized with JSON
  • How do you obtain the content of a specific node using XmlDocument in C#?
  • How to ignore the first line in a csv file when you read the csv file in C#
  • c# - Problem calling public void from class
  • How to tell a class which objects it should create? Type vs. object confusion :(
  • Save CheckBox state to xml
  • WIX CAQuietExec NETSH Command Fails
  • Issues sending http put request every 60 seconds to RoR app
  • ConfigurationManager.ConnectionStrings.ConnectionString Issue
  • Real size WPF controls for printing
  • How to cancel properly?
  • C# String multiplication error
  • Using Solrnet and Assigning Attributes with Entity Framework Generated POCOs
  • Regex pattern for single backslash
  • TextBox: insert spaces for credit card number?
  • C# DLL loaded for exe-application is not found when launching similar DLL by rundll32.exe
  • Debug a Windows Service with WCF library
  • Open a file from an external assembly?
  • Servicestack RegistrationFeature Unable to bind request
  • Index was outside the bounds of the array confusion
  • Error in Xml to List code. The ':' character, hexadecimal value 0x3A, cannot be included in a name
  • I am trying to do a while loop with a string conditional statement in C#
  • C# 'Cannot access a disposed object. Object name: 'SslStream'.'
  • How to make Gecko use seperate CookieContainer per instance?
  • C# Advanced form "please wait"
  • Send and Receive data C# using network stream
  • How to discover that appsettings changed in C#?
  • Check what needs full trust
  • What is the execution order of an MVC Razor view/layout
  • Table designer (Entity Framework) is too resource intense
  • How to clean up an exception string so it can be displayed via Javascript Alert?
  • Configuring Amazon SES Feedback Notifications via Amazon SNS in ASP.NET MVC (C#)
  • C# api responce and request
  • Dynamodb putitem function not working properly
  • theme in windows phone(light or dark) using c#
  • Backup attached database file(.mdf) using c# and SQL Server
  • What is 'TextFile' and where it is uses in WPF project
  • shadow
    Privacy Policy - Terms - Contact Us © ourworld-yourmove.org