[英]How to move datagridview rows up or down based on a button click
我有与数据表绑定的 DataGridView。 我从数据库中填充这个数据表。 我在 datagridview 旁边有 2 个按钮,向上和向下。 行应根据我单击的任何按钮上下移动。 我知道类似问题有很多答案,但如果您的 gridview 不受限制,它们就会起作用。 我还有一个序列列,其中包含从 0 开始的数字。 当行向上或向下移动时,顺序也应该固定。 例如,如果我向上移动 row[12],row[12] 的序列应该更新为 row[11],row[11] 应该向下移动,row[11] 序列变成 row[12]。 (如果这令人困惑,我很抱歉)。 我也有 2 个事件函数附加到向上和向下按钮。
编辑:这个 gridview 的主要目标是用户也可以添加新行。 因此,连同上述信息,如果有 5 行序列为 1 到 5,并且用户添加了序列为 2 的另一行。我可以对行进行排序,但是如何制作序列为 2、3、4 的原始行, 5 降档并将其顺序更改为 3、4、5、6?
private void moveRow(int position)
{
DataRow selectedDataRow = null;
DataRow newDataRow = null;
int sequence = -1;
int newSequence = -1;
DataGridViewRow selectedRow = dataGridView.SelectedRows[0];
sequence = (int)selectedRow.Cells[sequenceDataGridViewTextBoxColumn.Index].Value;
newSequence = sequence + position;
if (newSequence <= 0 || newSequence > dataGridView.Rows.Count)
{
return;
}
//below code doesnt work at all maybe cuz it isnt right
//How do i select the current row and the row at the new sequence?????
if (selectedDataRow != null && newDataRow != null)
{//below i try to assign sequences to the rows
selectedDataRow["Sequence"] = newSequence;
newDataRow["Sequence"] = sequence;
}
dataGridViewRed.Sort(dataGridViewRed.Columns[buildSequenceDataGridViewTextBoxColumn.Index], ListSortDirection.Ascending);// i sort it again based on sequence
dataGridViewRed.CurrentCell = dataGridViewRed.Rows[selectedRow.Index + position].Cells[buildSequenceDataGridViewTextBoxColumn.Index];//highlight the current selected cell
//below functions are the events attached to the buttons for up and down
private void UpBtn_Click(object sender, EventArgs e)
{
moveRow(-1);
}
private void DownBtn_Click(object sender, EventArgs e)
{
moveRow(+1);
}
我的解释肯定有一些漏洞,所以请善待。 如果需要,我会尽力解释任何混淆。
如果行已绑定,则以下示例适用于向上移动行。 Prober Bounding应该注意在屏幕上对DataGridView
进行排序。 如果您在数据绑定源中对它进行排序,那么我认为它应该在没有.Sort
情况下也可以工作(我没有对此进行测试)。
如果行编号可能不一致(c#):
DataGridViewRow SelectedDataRow = ...;
if (SelectedDataRow.Index > 0)
{
DataGridViewRow PrevDataRow = DataGridView1.Rows(SelectedDataRow.Index - 1);
Int16 PrevSequence = PrevDataRow.Cells("Sequence").Value; // buffer the previous sequence
PrevDataRow.Cells("Sequence").Value = SelectedDataRow.Cells("Sequence").Value; // set previous sequence to selected
SelectedDataRow.Cells("Sequence").Value = PrevSequence; // set selected to previous
}
VB.NET:
Dim SelectedDataRow As DataGridViewRow = ...
If SelectedDataRow.Index > 0 Then
Dim PrevDataRow As DataGridViewRow = DataGridView1.Rows(SelectedDataRow.Index - 1)
Dim PrevSequence As Int16 = PrevDataRow.Cells("Sequence").Value ' buffer the previous sequence
PrevDataRow.Cells("Sequence").Value = SelectedDataRow.Cells("Sequence").Value ' set previous sequence to selected
SelectedDataRow.Cells("Sequence").Value = PrevSequence ' set selected to previous
End If
如果行编号是一致的(从1到N连续),则可以简化:
DataGridViewRow SelectedDataRow = ...;
if (SelectedDataRow.Index > 0)
{
DataGridViewRow PrevDataRow = DataGridView1.Rows(SelectedDataRow.Index - 1);
PrevDataRow.Cells("Sequence").Value = SelectedDataRow.Index + 1;
SelectedDataRow.Cells("Sequence").Value = SelectedDataRow.Index; // set selected to previous
}
VB.NET:
Dim SelectedDataRow As DataGridViewRow = ...
If SelectedDataRow.Index > 0 Then
Dim PrevDataRow As DataGridViewRow = DataGridView1.Rows(SelectedDataRow.Index - 1)
PrevDataRow.Cells("Sequence").Value = SelectedDataRow.Index + 1
SelectedDataRow.Cells("Sequence").Value = SelectedDataRow.Index ' set selected to previous
End If
向下移动类似,只是条件考虑了行数的限制。
尽管这在类似情况下对我有用,但通常在这种情况下不使用绑定,而是将DataTable用作数据中介器。
在我的中,我使用 mysql 查询从 datagridview 加载,我无法使其逐行下降或上升
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.