datagridview索引不足 [英] datagridview out of index

查看:87
本文介绍了datagridview索引不足的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我创建了一个Windows窗体来存储和检索mysql数据库中的数据.该表单将数据显示在dataGridView对象(默认视图)上,用户可以选择其他条目以获取更多信息.
部分代码如下:

Hi,
I''ve created a windows form to store and retrieve data from a mysql database. The form displays the data on a dataGridView object (defaultview) and the user can select different entries for further information.
Part of the code follows:

private void dataGridView1_DoubleClick(object sender, EventArgs e)
        {
            int selection = dataGridView1.Rows.GetFirstRow(DataGridViewElementStates.Selected);            MessageBox.Show(dataGridView1.Rows[selection].Cells[2].Value.ToString());
            manageSelection(selection);
        }


private void dataGridView1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                e.Handled = true;
                int selection = dataGridView1.Rows.GetFirstRow(DataGridViewElementStates.Selected);               MessageBox.Show(dataGridView1.Rows[selection].Cells[2].Value.ToString());
                manageSelection(selection);
            }
        }


void manageSelection(int selection)     
        {
            nameBox.Text = dataGridView1.Rows[selection].Cells[1].Value.ToString();
            addressBox.Text = dataGridView1.Rows[selection].Cells[2].Value.ToString();
            tkBox.Text = dataGridView1.Rows[selection].Cells[11].Value.ToString();
            telBox.Text = dataGridView1.Rows[selection].Cells[3].Value.ToString();
            cellBox.Text = dataGridView1.Rows[selection].Cells[4].Value.ToString();
            tameioBox.Text = dataGridView1.Rows[selection].Cells[5].Value.ToString();
            amBox.Text = dataGridView1.Rows[selection].Cells[6].Value.ToString();
            amkaBox.Text = dataGridView1.Rows[selection].Cells[7].Value.ToString();
            idcardBox.Text = dataGridView1.Rows[selection].Cells[10].Value.ToString();
            cityBox.Text = dataGridView1.Rows[selection].Cells[12].Value.ToString();
        }


该消息框仅用于调试目的.我发现消息框显示了正确的内容,但是在manageSelection()中执行相同的指令将不起作用,并且出现异常(索引不足).为什么?


The message boxes are used for debugging purposes only. I found out that the message boxes display the correct content but the same instruction when executed inside manageSelection() won''t work and I get an exception (out of index). Why is that?

推荐答案

请在manageSelection 方法内放置一个断点并运行程序.当执行在断点处停止时,检查selection 值是否小于Rows.Count并检查为Cells 给出的所有索引是否均有效,即小于Rows[selection].Cells.Count.
由于会抛出out of index错误,或者Row index大于行数,或者Cell index可能大于单元格数.

用于说明DataGridViewCurrentRow 方法用法的代码[/编辑]

Please put a break point inside the manageSelection method and run the program. When execution stops at the break point, check whether selection value is less than Rows.Count and check whether all the indices given for Cells are valid i.e. less than the Rows[selection].Cells.Count.
Since, out of index error is throw either the Row index is more than the number of rows or the Cell index may be more the number of cells.

Code to illustrate usage of CurrentRow method of DataGridView[/Edit]

private void dataGridView1_DoubleClick(object sender, EventArgs e)
{
    DataGridViewRow currentRow = dataGridView1.CurrentRow;
    if (currentRow != null)
        manageSelection(currentRow);
}


将manageSelection方法定义为


Define manageSelection method as

void manageSelection(DataGridViewRow row){
    nameBox.Text = row.Cells[1].Value.ToString();
    //Similary others are set
}


这篇关于datagridview索引不足的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆