0

我有这样的数据网格视图

在此处输入图像描述

当我尝试单击列标题以尝试对数据进行排序时,程序会在线中断

DataGridViewRow row = dataGridView1.Rows[rowIndex];

ArgumentOutOfRangeException 未处理。指数超出范围。必须是非负数且小于集合的大小。参数名称:索引

这是 .designer.cs 中的代码

this.dataGridView1.AutoSizeColumnsMode = System.Windows.Forms.DataGridViewAutoSizeColumnsMode.AllCells;
this.dataGridView1.AutoSizeRowsMode = System.Windows.Forms.DataGridViewAutoSizeRowsMode.AllCells;
this.dataGridView1.BackgroundColor = System.Drawing.Color.Azure;
this.dataGridView1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
this.dataGridView1.Location = new System.Drawing.Point(21, 62);
this.dataGridView1.Name = "dataGridView1";
this.dataGridView1.Size = new System.Drawing.Size(586, 381);
this.dataGridView1.TabIndex = 9;
this.dataGridView1.CellContentClick += new System.Windows.Forms.DataGridViewCellEventHandler(this.dataGridView1_CellContentClick_1);

这是我的 .cs 中的代码

private void dataGridView1_CellContentClick_1(object sender, DataGridViewCellEventArgs e)
        {
            int rowIndex = e.RowIndex;
            DataGridViewRow row = dataGridView1.Rows[rowIndex];
            groupBoxPenghuni.Visible = false;
            groupBoxStaff.Visible = false;
            groupBoxRoom.Visible = false;
            groupBoxDPenghuni.Visible = true;
            groupBoxPenghasilan.Visible = false;
            GroupBox_AddResident_Resident.Visible = false;
            GroupBox_AddResident_Room.Visible = false;
            GroupBox_AddResident1.Visible = false;
            GroupBox_DeleteResident_Resident.Visible = false;
            GroupBox_DeleteResident1.Visible = false;
            GroupBox_Resident.Visible = false;
            GroupBox_Update_Room.Visible = false;
            GroupBox_UpdateResident1.Visible = false;
        }

有什么问题?我该怎么办?

4

2 回答 2

4

它失败的原因是因为您dataGridView1_CellContentClick_1在排序时正在触发,而Index was out of range. 您应该在继续之前检查它是否有效。

这些是声明的,但从未使用过。你真的需要这些线吗?

int rowIndex = e.RowIndex;
DataGridViewRow row = dataGridView1.Rows[rowIndex];

如果确实需要这些行,则必须在尝试声明它们之前检查索引是否超出范围。

private void dataGridView1_CellContentClick_1(object sender, DataGridViewCellEventArgs e)
{
    if (e.RowIndex >= 0)
    {
        int rowIndex = e.RowIndex;
        DataGridViewRow row = dataGridView1.Rows[rowIndex];
        groupBoxPenghuni.Visible = false;
        groupBoxStaff.Visible = false;
        groupBoxRoom.Visible = false;
        groupBoxDPenghuni.Visible = true;
        groupBoxPenghasilan.Visible = false;
        GroupBox_AddResident_Resident.Visible = false;
        GroupBox_AddResident_Room.Visible = false;
        GroupBox_AddResident1.Visible = false;
        GroupBox_DeleteResident_Resident.Visible = false;
        GroupBox_DeleteResident1.Visible = false;
        GroupBox_Resident.Visible = false;
        GroupBox_Update_Room.Visible = false;
        GroupBox_UpdateResident1.Visible = false;
    }
}
于 2013-04-12T06:34:59.897 回答
0

您必须检查 CellContentClick 事件:

if (e.RowIndex != -1 && e.ColumnIndex!=-1)
{
// do something
}

如果 e.RowIndex 为 -1,则表示您单击了列标题 对于行标题也是如此

于 2013-04-12T06:35:28.367 回答