遇到的问题:当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