DataGridView中實現上一頁,上一行,下一行,下一頁的功能:
屬性設置:
SelectionMode——FullRowSelect
MultiSelct——false
AllowUserToResizeRows——false
AllowUserToResizeColumns——false
//以下的DataGridView在窗口中可視的行數爲15。
private void btnNextRow_Click(object sender, EventArgs e)
{
if (dgvPlaneInfo.CurrentCell.RowIndex < dgvPlaneInfo.Rows.Count - 1)
{
dgvPlaneInfo.CurrentCell = dgvPlaneInfo[0, dgvPlaneInfo.CurrentCell.RowIndex + 1];
}
else
{
MessageBox.Show("當前已是最後一行", "提示");
}
}
private void btnLastRow_Click(object sender, EventArgs e)
{
if (dgvPlaneInfo.CurrentCell.RowIndex != 0)
{
dgvPlaneInfo.CurrentCell = dgvPlaneInfo[0, dgvPlaneInfo.CurrentCell.RowIndex - 1];
}
else
{
MessageBox.Show("當前已是第一行", "提示");
}
}
private void btnNextPage_Click(object sender, EventArgs e)
{
if (dgvPlaneInfo.Rows.Count - dgvPlaneInfo.CurrentCell.RowIndex > 15)
{
dgvPlaneInfo.CurrentCell = dgvPlaneInfo[0, dgvPlaneInfo.CurrentCell.RowIndex + 15];
}
else if (dgvPlaneInfo.Rows.Count - dgvPlaneInfo.CurrentCell.RowIndex > 1)
{
dgvPlaneInfo.CurrentCell = dgvPlaneInfo[0, dgvPlaneInfo.Rows.Count - 1];
}
else
{
MessageBox.Show("當前已是最後一頁", "提示");
}
}
private void btnLastPage_Click(object sender, EventArgs e)
{
if (dgvPlaneInfo.CurrentCell.RowIndex > 14)
{
dgvPlaneInfo.CurrentCell = dgvPlaneInfo[0, dgvPlaneInfo.CurrentCell.RowIndex - 15];
}
else if (dgvPlaneInfo.CurrentCell.RowIndex <= 14 && dgvPlaneInfo.CurrentCell.RowIndex != 0)
{
dgvPlaneInfo.CurrentCell = dgvPlaneInfo[0, 0];
}
else
{
MessageBox.Show("當前已是第一頁", "提示");
}
}