如何在排序后在给定位置的bindingsource中添加一行? C#

我使用绑定源构建我的数据网格:

    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

删除位置x的类似问题 – 这里已经解决了
>> stack overflow question

想到使用bs.insert(0,newRow),但它说外部对象无法添加到此列表中

如何在排序后在给定位置的bindingsource中添加一行?

解决方法:

这是一个解决方案,可以添加到列表的顶部 – 我不知道它是否是最好的解决方案,但它的工作原理.有很多其他方法可以解决这个问题,包括制作允许插入排序列表的自己的绑定集合,从而可以从某些观点提供更好的代码,但这至少很简单.

这是代码,包括创建数据表.

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,它允许我们根据行是否为新来对数据表进行排序.再加上从DataGridView中获取sort列,可以保留旧的排序顺序并保持新行的位置.

上一篇:绑定源正确时,DataGridView无法在C#中更新


下一篇:带有多个视图的Winforms MVP模式