c#如何从List <t>创建SortableBindingList

时间:2015-06-18 05:21:19

标签: c# list sorting datagridview

我有一个填充DataGridView的对象列表(所有字符串)但是我无法使用列标题进行排序。遗憾的是,我一直在阅读SortableBindingList以解决问题,因为我只学了几天代码,我无法理解如何实现它。任何让我再次感动的建议都会非常感激(请尽量保持简单)。

到目前为止,这是我的代码

{
public class Product
{
    public string itemCode;
    public string description;
    public string currentCount;
    public string onOrder;

    public string ItemCode
    {
        get
        {
            return itemCode;
        }
        set
        {
            itemCode = value;
        }
    }
    public string Description
    {
        get
        {
            return description;
        }
        set
        {
            description = value;
        }
    }
    public string CurrentCount
    {
        get
        {
            return currentCount;
        }
        set
        {
            currentCount = value;
        }
    }
    public string OnOrder
    {
        get
        {
            return onOrder;
        }
        set
        {
            onOrder = value;
        }
    }
}


{
class Program
{
    public static List<Product> itemList = new List<Product>();
    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main()
    {
        //Reads csv file
        var filestream = new FileStream(@"c:\StockFile\stocklist.csv", FileMode.Open,
        FileAccess.Read, FileShare.ReadWrite);
        var file = new StreamReader(filestream, Encoding.UTF8, true, 128);
        string lineOfText;

        bool firstLine = true;
        while ((lineOfText = file.ReadLine()) != null)
        {

            if (!firstLine)
            {
                // splits the values        
                string[] elements;
                Product newItem = new Product();
                elements = lineOfText.Split(',');
                newItem.itemCode = elements[0];
                newItem.description = elements[1];
                newItem.currentCount = elements[2];
                newItem.onOrder = elements[3];

                // add to list
                itemList.Add(newItem);
            }
            else
                firstLine = false;
        }
        //loads grid
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new Form1());


        //Output to file
        using (TextWriter tw = new StreamWriter(@"C:\StockFile\stocklist.csv"))
        {
            tw.WriteLine("Item Code,Item Description,Current Count,On Order");
            foreach (Product newItem in itemList)
            {
                tw.WriteLine(newItem.ItemCode + "," + newItem.Description + "," + newItem.CurrentCount + "," + newItem.OnOrder);

            }
        } 
    }
}


{
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }
    //Binds data to Grid
    private void Form1_Load(object sender, EventArgs e)
    {
        var source = new BindingSource();
        source.DataSource = Program.itemList;
        dataGridView1.DataSource = source;
        dataGridView1.AutoGenerateColumns = true;
        this.Controls.Add(dataGridView1);
        dataGridView1.Refresh();

        foreach (DataGridViewColumn column in dataGridView1.Columns)
        {

            dataGridView1.Columns[column.Name].SortMode = DataGridViewColumnSortMode.Automatic;
        }


        //Make Colomns read only
        foreach (DataGridViewColumn col in dataGridView1.Columns)
        {
            if (col.Name == "CurrentCount")
            {
                col.ReadOnly = false;
            }
            else
            {
                col.ReadOnly = true;
            }
        }
    }

}

1 个答案:

答案 0 :(得分:0)

这里有一个SortableBindingList的示例代码......

https://stackoverflow.com/a/40051526/2093531

将List转换为SortableBindingList ...

SortableBindingList<YourListType> sortableBindingList = new SortableBindingList<YourListType>(list);