C# Winform 对用BindingList(List)作为数据源绑定的datagridview进行上下行移动操作

遇到的问题:当datagridview被数据源绑定时,是不能用编程直接对datagridview的数据进行换行的!

搜索网上资料发现,几乎全是用DateTable进行的数据操作  但是我遇到的问题是List(List可以转成BindingList)

于是,自己写了一段代码 进行换行

当前选中行向下移动

//下移动
        private void button7_Click(object sender, EventArgs e)
        {
            BindingList<Entry> newEntry = new BindingList<Entry>();
            
            for (int i = 0; i < dataGridView1.RowCount; i++)
            {
                Entry old = new Entry();
                if (dataGridView1.Rows[i].Selected)
                {
                    if (i + 1 == dataGridView1.RowCount)
                    {
                        MessageBox.Show("已经是最后一行!");
                    }
                    else
                    {
                        //上下两行进行交换
                        Entry old1 = new Entry();
                        old1._FAccountNumber = dataGridView1.Rows[i + 1].Cells[0].Value.ToString();
                        old1._FAccountName = dataGridView1.Rows[i + 1].Cells[1].Value.ToString();
                        old1._FDCSTR = dataGridView1.Rows[i + 1].Cells[2].Value.ToString();
                        old1._MoneyName = dataGridView1.Rows[i + 1].Cells[3].Value.ToString();
                        old1._FExplanation = dataGridView1.Rows[i + 1].Cells[4].Value.ToString();
                        old1._FAcctSourceSTR = dataGridView1.Rows[i + 1].Cells[5].Value.ToString();
                        newEntry.Add(old1);
                        old._FAccountNumber = dataGridView1.Rows[i].Cells[0].Value.ToString();
                        old._FAccountName = dataGridView1.Rows[i].Cells[1].Value.ToString();
                        old._FDCSTR = dataGridView1.Rows[i].Cells[2].Value.ToString();
                        old._MoneyName = dataGridView1.Rows[i].Cells[3].Value.ToString();
                        old._FExplanation = dataGridView1.Rows[i].Cells[4].Value.ToString();
                        old._FAcctSourceSTR = dataGridView1.Rows[i].Cells[5].Value.ToString();
                        newEntry.Add(old);
                        i = i + 1;//因为i+1 的数据已被获取
                        continue;
                    }
                }
                old._FAccountNumber = dataGridView1.Rows[i].Cells[0].Value.ToString();
                old._FAccountName = dataGridView1.Rows[i].Cells[1].Value.ToString();
                old._FDCSTR = dataGridView1.Rows[i].Cells[2].Value.ToString();
                old._MoneyName = dataGridView1.Rows[i].Cells[3].Value.ToString();
                old._FExplanation = dataGridView1.Rows[i].Cells[4].Value.ToString();
                old._FAcctSourceSTR = dataGridView1.Rows[i].Cells[5].Value.ToString();
                newEntry.Add(old);
               
            }
            dataGridView1.DataSource = newEntry;
            
        }

向上移动

 //上移动
        private void button8_Click(object sender, EventArgs e)
        {
            BindingList<Entry> newEntry = new BindingList<Entry>();

            for (int i = 0; i < dataGridView1.RowCount; i++)
            {
                Entry old = new Entry();
                if (dataGridView1.Rows[0].Selected)
                {
                    MessageBox.Show("已经是第一行!");
                    break;
                }
                if (i + 1 < dataGridView1.RowCount)
                {
                    //上下两条数据交换
                    if (dataGridView1.Rows[i + 1].Selected)
                    {
                        // old._FEntryID = Convert.ToInt32(dataGridView1.Rows[i].Cells[0].Value.ToString());
                        Entry old1 = new Entry();
                        old1._FAccountNumber = dataGridView1.Rows[i + 1].Cells[0].Value.ToString();
                        old1._FAccountName = dataGridView1.Rows[i + 1].Cells[1].Value.ToString();
                        old1._FDCSTR = dataGridView1.Rows[i + 1].Cells[2].Value.ToString();
                        old1._MoneyName = dataGridView1.Rows[i + 1].Cells[3].Value.ToString();
                        old1._FExplanation = dataGridView1.Rows[i + 1].Cells[4].Value.ToString();
                        old1._FAcctSourceSTR = dataGridView1.Rows[i + 1].Cells[5].Value.ToString();
                        newEntry.Add(old1);
                        Entry old2 = new Entry();
                        old2._FAccountNumber = dataGridView1.Rows[i].Cells[0].Value.ToString();
                        old2._FAccountName = dataGridView1.Rows[i].Cells[1].Value.ToString();
                        old2._FDCSTR = dataGridView1.Rows[i].Cells[2].Value.ToString();
                        old2._MoneyName = dataGridView1.Rows[i].Cells[3].Value.ToString();
                        old2._FExplanation = dataGridView1.Rows[i].Cells[4].Value.ToString();
                        old2._FAcctSourceSTR = dataGridView1.Rows[i].Cells[5].Value.ToString();
                        newEntry.Add(old2);
                        if (i + 1 == dataGridView1.RowCount - 1)
                            break;
                        i = i + 1;
                        continue;

                    }
                }
                old._FAccountNumber = dataGridView1.Rows[i].Cells[0].Value.ToString();
                old._FAccountName = dataGridView1.Rows[i].Cells[1].Value.ToString();
                old._FDCSTR = dataGridView1.Rows[i].Cells[2].Value.ToString();
                old._MoneyName = dataGridView1.Rows[i].Cells[3].Value.ToString();
                old._FExplanation = dataGridView1.Rows[i].Cells[4].Value.ToString();
                old._FAcctSourceSTR = dataGridView1.Rows[i].Cells[5].Value.ToString();
                newEntry.Add(old);

            }
            if (!dataGridView1.Rows[0].Selected)
            {
                dataGridView1.DataSource = newEntry;
            }
        }
PS:各位可以加楼主的技术交流群,不分语言哦,BUG制作者协会:121942786


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值