C# 在C列中订购数据时中断#
我有这样的数据网格视图 当我尝试单击列标题尝试对数据进行排序时,程序在第行中断C# 在C列中订购数据时中断#,c#,datagridview,break,C#,Datagridview,Break,我有这样的数据网格视图 当我尝试单击列标题尝试对数据进行排序时,程序在第行中断 DataGridViewRow row = dataGridView1.Rows[rowIndex]; ArgumentOutOfRange异常未处理。 索引超出范围。 必须为非负数且小于集合的大小。 参数名称:索引 下面是.designer.cs中的代码 this.dataGridView1.AutoSizeColumnsMode = System.Windows.Forms.DataGridViewAutoS
DataGridViewRow row = dataGridView1.Rows[rowIndex];
ArgumentOutOfRange异常未处理。
索引超出范围。
必须为非负数且小于集合的大小。
参数名称:索引
下面是.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);
这是my.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;
}
什么毛病?我该怎么办呢?它失败的原因是,当您排序时,
索引超出范围时,您的dataGridView1\u CellContentClick\u 1
会触发。在继续之前,您应该检查它是否有效
这些是声明的,但从未使用过。你真的需要这些线路吗
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;
}
}
它失败的原因是您的dataGridView1\u CellContentClick\u 1
在排序时触发,并且索引超出范围。在继续之前,您应该检查它是否有效
这些是声明的,但从未使用过。你真的需要这些线路吗
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;
}
}
您必须签入CellContentClick事件:
if (e.RowIndex != -1 && e.ColumnIndex!=-1)
{
// do something
}
如果e.RowIndex为-1,则表示单击了列标题
行标题也是如此,您必须签入CellContentClick事件:
if (e.RowIndex != -1 && e.ColumnIndex!=-1)
{
// do something
}
如果e.RowIndex为-1,则表示单击了列标题
行标题也是如此您遇到了什么异常?@RajeevKumar ArgumentOutOfRangeException未处理。索引超出范围。必须为非负数且小于集合的大小。参数名称:索引。我已经编辑了我的帖子。抱歉。您遇到了什么异常?@RajeevKumar ArgumentOutOfRangeException未处理。索引超出范围。必须为非负数且小于集合的大小。参数名称:索引。我已经编辑了我的帖子。对不起,我从来没有使用过这个代码;DataGridViewRow行=dataGridView1.Rows[rowIndex]代码>然后删除它,最后程序没有中断。谢谢你的帮助!我从不使用这个代码introwindex=e.rowIndex;DataGridViewRow行=dataGridView1.Rows[rowIndex]代码>然后删除它,最后程序没有中断。谢谢你的帮助!