如何BindingSource的排序后加上给定位置的行? C# [英] How to add a row in bindingsource on given position after sorting? C#

查看:468
本文介绍了如何BindingSource的排序后加上给定位置的行? C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我建立了我的绑定源数据网格:

I build up my datagrid with binding source:

    SqlDataAdapter adapter = new SqlDataAdapter(Datenbank.cmd);
    dataSet1.Tables.Clear();
    adapter.Fill(dataSet1, "Table");
    bs = new BindingSource();
    bs.DataSource = dataSet1.Tables["Table"];
    dataGridView1.DataSource = bs;

现在我对它进行排序:

    bs.Sort = "customer DESC";

现在我想在0

    dataSet1.Tables[0].Rows.InsertAt(newRow, 0);



然而,它不会被在位置0

However, it will not be insert at position 0

与在位置x删除类似的问题 - 这是在这里解决
>>堆栈溢出问题

Similar problem with deleting at position x - which was solved in here >> stack overflow question

使用 bs.insert的想法(0,NEWROW) ,但它说,外部对象不能被添加到这个列表

thought of using bs.insert(0,newRow) but it says external objects can not be added to this list

如何到的BindingSource上给定的位置排序后添加行?

推荐答案

下面是用于添加到列表顶部的有效的解决方案 - 我不知道,如果它是最好的解决方案,但它的工作原理。有很多其他的方式来处理这一问题,包括像制作,允许插入排序列表自己绑定集合,这可能给的更好的代码形式从某种角度来看,但是这是在简单最少的。

Here is a solution that works for adding to the top of the list - I have no idea if it is the best solution but it works. There are lots of other ways to approach the issue, including things like making your own binding collection that allows inserting into a sorted list, that could give better code form certain points of view, but this is simple at least.

下面是代码,其中包括创建一个数据表。

Here is the code, which includes creating a datatable.

public partial class Form1 : Form
{
    BindingSource bs;
    DataTable dt;
    public Form1()
    {
        InitializeComponent();

        dt = new DataTable("Customers");
        DataColumn dc;

        dc = new DataColumn();
        dc.DataType = typeof(int);
        dc.ColumnName = "CustomerID";

        dt.Columns.Add(dc);
        dt.Columns.Add(new DataColumn("LastName"));
        dt.Columns.Add(new DataColumn("FirstName"));
        // Concatenation of first and last names 
        dt.Columns.Add(new DataColumn("FullName"));
        dt.Columns.Add(new DataColumn("Address"));
        dt.Columns.Add(new DataColumn("City"));
        dt.Columns.Add(new DataColumn("State"));
        dt.Columns.Add(new DataColumn("Zip"));
        dt.Columns.Add(new DataColumn("Phone"));

        dc = new DataColumn();
        dc.DataType = typeof(DateTime);
        dc.ColumnName = "LastPurchaseDate";
        dt.Columns.Add(dc);

        dc = new DataColumn();
        dc.DataType = typeof(int);
        dc.ColumnName = "CustomerType";
        dt.Columns.Add(dc);
        dt.Columns.Add("HiddenSort", typeof(bool));

        // Populate the table 
        dt.Rows.Add(2, "Baggins", "Bilbo", "Baggins, Bilbo", "Bagshot Row #1", "Hobbiton", "SH", "00001", "555-2222", DateTime.Parse("9/9/2008"), 1, false);
        dt.Rows.Add(1, "Baggins", "Frodo", "Baggins, Frodo", "Bagshot Row #2", "Hobbiton", "SH", "00001", "555-1111", DateTime.Parse("9/9/2008"), 1, false);
        dt.Rows.Add(6, "Bolger", "Fatty", "Bolger, Fatty", "ProudFeet Creek", "Hobbiton", "SH", "00001", "555-1111", DateTime.Parse("9/9/2008"), 1); dt.Rows.Add(4, "Elessar", "Aragorn", "Elessar, Aragorn", "Citadel", "Minas Tirith", "Gondor", "00000", "555-0000", DateTime.Parse("9/9/2008"), 4, false);
        dt.Rows.Add(5, "Evenstar", "Arwin", "Evenstar, Arwin", "Citadel", "Minas Tirith", "Gondor", "00000", "555-0001", DateTime.Parse("9/9/2008"), 4, false);
        dt.Rows.Add(3, "Greyhame", "Gandalf", "Grayhame, Gandalf", DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value, DBNull.Value, 3, false);

        BindingSource bs = new BindingSource();
        bs.DataSource = dt;

        dataGridView1.DataSource = bs;
        dataGridView1.Columns["HiddenSort"].Visible = false;
        dataGridView1.Sorted += new EventHandler(dataGridView1_Sorted);
    }

    void dataGridView1_Sorted(object sender, EventArgs e)
    {
        foreach (DataRow dr in dt.Rows)
        {
            dr["HiddenSort"] = false;
        }
    }

    private void button1_Click(object sender, EventArgs e)
    {

        DataRow dr = dt.Rows.Add(2, "Testing", "Bilbo", "Baggins, Bilbo", "Bagshot Row #1", "Hobbiton", "SH", "00001", "555-2222", DateTime.Parse("9/9/2008"), 1, true);
        DataView dv = dt.DefaultView;
        if (dataGridView1.SortedColumn == null)
        {
            dv.Sort = "[HiddenSort] desc";
        }
        else
        {
            string sortname = dataGridView1.SortedColumn.Name;

            dv.Sort = "[HiddenSort] desc, [" + sortname + "] asc";
        }
    }

}



诀窍这里增加一个新列HiddenSort来,使我们能够进行排序基于行是否是新的或不DataTable中的数据表。这再加上抓住了排序列从DataGridView中最多允许保留旧的排序顺序,保持新行的位置。

The trick here is adding a new column HiddenSort to the datatable that allows us to sort the datatable based on whether a row is new or not. That coupled with grabbing the sort column from the DataGridView allows up to keep the old sort order and maintain the position of new rows.

这篇关于如何BindingSource的排序后加上给定位置的行? C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆