如题。我写的代码是这样的:private void btnToUp_Click(object sender, EventArgs e)
{
DataGridViewRowCollection rows = this.gvBottom.Rows;
DataGridViewSelectedRowCollection selectRows = this.gvBottom.SelectedRows;
if (rows.Count > 0)
{
int rowIndex = selectRows[selectRows.Count - 1].Index;
if (rowIndex == rows.Count)
{
MessageBox.Show("当前已是第一行了!");
return;
}
int Rorder = int.Parse(selectRows[0].Cells["rorder"].Value.ToString());
rows[rowIndex - 1].Cells["rorder"].Value = Rorder;
for (int i = 0; i < selectRows.Count; i++)
{
string num = (Rorder - (i + 1) * 10).ToString();
if (num.Length == 2)
{
num = "00" + num;
}
if (num.Length == 3)
{
num = "0" + num;
}
selectRows[i].Cells["rorder"].Value = num; }
this.gvBottom.Sort(this.gvBottom.Columns["rorder"], ListSortDirection.Ascending);
isModify = 1;
}
else {
MessageBox.Show("请选择您要调整的行!");
}
}
代码在运行过后,选择几行数据,第一次点击"向上调",调的是正确的,不关闭窗体,在重新选择几行数据,在点一次'向上调',调整过后就乱七八糟的了,比如说第一次,我选择序号为"50,60,70"的行,按向上调,这选择的3行序号就变成了40,50,60,而以前的40这个行序号就变成70,这就是我想要的结果,但是不关闭窗体,在重新选择几行数据,"60,70,80",点向上调,结果就乱七八糟的了,有时候还有重复的序号,这三行也没有调到50上面去,有时候可能是1行,或者2行跳的别的地方去了。有没有人能告诉我这是为什么,有什么办法解决。
还有,最号让光标也随着选择的行上调!谁能帮我解决了这个问题,80分全部给他。
灌水的不要来!
{
DataGridViewRowCollection rows = this.gvBottom.Rows;
DataGridViewSelectedRowCollection selectRows = this.gvBottom.SelectedRows;
if (rows.Count > 0)
{
int rowIndex = selectRows[selectRows.Count - 1].Index;
if (rowIndex == rows.Count)
{
MessageBox.Show("当前已是第一行了!");
return;
}
int Rorder = int.Parse(selectRows[0].Cells["rorder"].Value.ToString());
rows[rowIndex - 1].Cells["rorder"].Value = Rorder;
for (int i = 0; i < selectRows.Count; i++)
{
string num = (Rorder - (i + 1) * 10).ToString();
if (num.Length == 2)
{
num = "00" + num;
}
if (num.Length == 3)
{
num = "0" + num;
}
selectRows[i].Cells["rorder"].Value = num; }
this.gvBottom.Sort(this.gvBottom.Columns["rorder"], ListSortDirection.Ascending);
isModify = 1;
}
else {
MessageBox.Show("请选择您要调整的行!");
}
}
代码在运行过后,选择几行数据,第一次点击"向上调",调的是正确的,不关闭窗体,在重新选择几行数据,在点一次'向上调',调整过后就乱七八糟的了,比如说第一次,我选择序号为"50,60,70"的行,按向上调,这选择的3行序号就变成了40,50,60,而以前的40这个行序号就变成70,这就是我想要的结果,但是不关闭窗体,在重新选择几行数据,"60,70,80",点向上调,结果就乱七八糟的了,有时候还有重复的序号,这三行也没有调到50上面去,有时候可能是1行,或者2行跳的别的地方去了。有没有人能告诉我这是为什么,有什么办法解决。
还有,最号让光标也随着选择的行上调!谁能帮我解决了这个问题,80分全部给他。
灌水的不要来!
解决方案 »
- 托管C++如何将对象的成员变量拷贝到数组中?
- CheckBoxList按数据库中数据选中
- 想要一个Oracle的特殊查询,大家帮忙看看
- C#可以实现DLL库的动态调用吗?
- 如何在.NET中运行IE控件里HTML页面的脚本函数?
- c#中使用fastreprot问题
- 谁有帮个忙好伐?买了本书WROX的C# Web服务高级编程,但是到WROX上一找没有书上的源码的,谁手头有这本书的源码发给我吧?chy_5393_cn@yahoo.
- 更急,无法下载http://www.downcode.com/down/list.asp?id=1207;
- 各位高手们,请教教小弟如何在C#下使用SetSystemTime这个API函数?(在线等待)
- 如何使用C# 开发XP风格的WinForm呀!
- .net 代码保护
- winform中的水晶报表导出PDF
private void Form3_Load(object sender, EventArgs e)
{
for (int i = 0; i < 10; i++)
{
DataGridViewRow row = dataGridView1.Rows[dataGridView1.Rows.Add()];
row.Cells[0].Value = i;
row.Cells[1].Value = i + "B";
row.Cells[2].Value = i + "C";
} } private void btnToUp_Click(object sender, EventArgs e)
{
DataGridViewSelectedRowCollection rows = dataGridView1.SelectedRows;
if (rows.Count == 0)
{
MessageBox.Show("未选择行!");
return;
} if (rows[0].Index == 0)
{
MessageBox.Show("已是第一行!");
return;
} RowUp(rows, dataGridView1.Rows, "Column1"); } private void RowUp(DataGridViewSelectedRowCollection selectRows2, DataGridViewRowCollection rows, string colName)
{
DataGridViewRow[] selectRows = new DataGridViewRow[selectRows2.Count]; for (int i = 0; i < selectRows2.Count; i++)
{
selectRows[i] = selectRows2[i];
} for (int i = 0; i < selectRows.Length; i++)
{
for (int j = i + 1; j < selectRows.Length; j++)
{
if (selectRows[i].Index > selectRows[j].Index)
{
DataGridViewRow rwo = selectRows[i];
selectRows[i] = selectRows[j];
selectRows[j] = rwo;
}
}
}
int motionNumber = selectRows[0].Index - selectRows.Length < 0 ? 0 : selectRows[0].Index - selectRows.Length;
if (motionNumber != 0)
motionNumber = selectRows[0].Index - motionNumber;
else
motionNumber = selectRows[0].Index; motionNumber = selectRows[0].Index - motionNumber;
foreach (DataGridViewRow row in selectRows)
{
for (int i = row.Index; i >= motionNumber; i--)
{
object value = row.Cells[colName].Value;
row.Cells[colName].Value = rows[i].Cells[colName].Value;
rows[i].Cells[colName].Value = value;
}
motionNumber++; dataGridView1.Sort(Column1, ListSortDirection.Ascending);
Application.DoEvents();
} }
但是不知道为什么,在我工作写的项目中,用楼上的代码就不行了,完全没效果,我用的是dataTable数据源的。
使用
dataGridView1.Sort(dataGridView1.Columns[colName], ListSortDirection.Ascending);
选中行的选中状态变为-1;暂时没解决方法。但是你可以把绑定写成不绑定这样就能用上面的方法了。
你的代码有点复杂,我看的不是很明白,能不能给该下啊!
private void Form3_Load(object sender, EventArgs e)
{
for (int i = 0; i < 10; i++)
{
DataGridViewRow row = dataGridView1.Rows[dataGridView1.Rows.Add()];
row.Cells[0].Value = i;
row.Cells[1].Value = i + "B";
row.Cells[2].Value = i + "C";
} } private void btnToUp_Click(object sender, EventArgs e)
{
DataGridViewSelectedRowCollection rows = dataGridView1.SelectedRows;
if (rows.Count == 0)
{
MessageBox.Show("未选择行!");
return;
} RowUp(rows, dataGridView1.Rows, "Column1"); } private void button2_Click(object sender, EventArgs e)
{
DataGridViewSelectedRowCollection rows = dataGridView1.SelectedRows;
if (rows.Count == 0)
{
MessageBox.Show("未选择行!");
return;
} RowNext(rows, dataGridView1.Rows, "Column1");
} private void RowUp(DataGridViewSelectedRowCollection selectRows2, DataGridViewRowCollection rows, string colName)
{
DataGridViewRow[] selectRows = new DataGridViewRow[selectRows2.Count]; for (int i = 0; i < selectRows2.Count; i++)
{
selectRows[i] = selectRows2[i];
} for (int i = 0; i < selectRows.Length; i++)
{
for (int j = i + 1; j < selectRows.Length; j++)
{
if (selectRows[i].Index > selectRows[j].Index)
{
DataGridViewRow rwo = selectRows[i];
selectRows[i] = selectRows[j];
selectRows[j] = rwo;
}
}
} if (selectRows[0].Index == 0)
{
MessageBox.Show("已是第一行!");
return;
} int motionNumber = selectRows[0].Index - 1;
foreach (DataGridViewRow row in selectRows)
{
for (int i = row.Index - 1; i >= motionNumber; i--)
{
object value = row.Cells[colName].Value;
row.Cells[colName].Value = rows[i].Cells[colName].Value;
rows[i].Cells[colName].Value = value;
}
motionNumber++; dataGridView1.Sort(dataGridView1.Columns[colName], ListSortDirection.Ascending);
Application.DoEvents();
}
} private void RowNext(DataGridViewSelectedRowCollection selectRows2, DataGridViewRowCollection rows, string colName)
{
DataGridViewRow[] selectRows = new DataGridViewRow[selectRows2.Count]; for (int i = 0; i < selectRows2.Count; i++)
{
selectRows[i] = selectRows2[i];
} for (int i = 0; i < selectRows.Length; i++)
{
for (int j = i + 1; j < selectRows.Length; j++)
{
if (selectRows[i].Index < selectRows[j].Index)
{
DataGridViewRow rwo = selectRows[i];
selectRows[i] = selectRows[j];
selectRows[j] = rwo;
}
}
} if (selectRows[0].Index == rows.Count - 1)//这里的表格未启用添加,所以-1 启动的话得-2
{
MessageBox.Show("已是最后一行!");
return;
} int motionNumber = selectRows[0].Index + 1;
foreach (DataGridViewRow row in selectRows)
{
for (int i = row.Index + 1; i <= motionNumber; i++)
{
object value = row.Cells[colName].Value;
row.Cells[colName].Value = rows[i].Cells[colName].Value;
rows[i].Cells[colName].Value = value;
}
motionNumber--; dataGridView1.Sort(dataGridView1.Columns[colName], ListSortDirection.Ascending);
Application.DoEvents();
}
}