繁体   English   中英

Winform Datagridview cellclick错误 - 索引超出范围

[英]Winform Datagridview cellclick error - Index was out of range

我有一个简单的数据网格,列出了SQLSERVER表中的一堆记录。 数据网格填充没有任何问题。 我想单击一行并将相应的数据加载到我旁边创建的文本框中。 到目前为止这么简单。

这是我的cellclick事件的代码

    private void dataGridVieworderitems_CellClick(object sender, DataGridViewCellEventArgs e)
    {
        {
            //try
            //{
                //if (dataGridVieworderitems.SelectedRows.Count > 0) // make sure user select at least 1 row 
                {
                    string jobId = dataGridVieworderitems.SelectedRows[0].Cells[0].Value + string.Empty;
                    string standpack = dataGridVieworderitems.SelectedRows[0].Cells[1].Value + string.Empty;
                    string description = dataGridVieworderitems.SelectedRows[0].Cells[2].Value + string.Empty;
                    string price = dataGridVieworderitems.SelectedRows[0].Cells[3].Value + string.Empty;
                    string itemType = dataGridVieworderitems.SelectedRows[0].Cells[4].Value + string.Empty;
                    string notes = dataGridVieworderitems.SelectedRows[0].Cells[5].Value + string.Empty;

                    labelidvalue.Text = jobId;
                    labelstandpackvalue.Text = standpack;
                    labeldescriptionvalue.Text = description;
                    textBoxprice.Text = price;
                    labeltypevalue.Text = itemType;
                    textBoxnotes.Text = notes;
                }
            //}
            //catch (Exception)
            //{
            //    MessageBox.Show("something went wrong!");
            //}
        }
    }

我故意注释掉了If语句并尝试使用catch块来生成错误..我收到以下错误..

System.ArgumentOutOfRangeException未处理HResult = -2146233086消息=索引超出范围。 必须是非负数且小于集合的大小。 参数名称:index ParamName = index ....

它是WINFORM和c#.. datagrid视图有数据..但它说索引超出范围。 有人可以指出我正确的方向吗?

这就是我填充网格的方式

    public DataTable GetStaffCurrentOrderItems()
    {
        try
        {
            DataTable dtstaffcurrentorderlist = new DataTable();

            string connString = System.Configuration.ConfigurationManager.ConnectionStrings["nav"].ConnectionString;
            using (SqlConnection con = new SqlConnection(connString))
            {
                using (SqlCommand cmd = new SqlCommand("select [ID],standpack as [Item], item_description as [Description], '$'+convert(varchar(5),price) as Price,item_type as [Item Type],notes as [Notes] from tbl_staff_orders_items", con))
                {
                    if (con.State == ConnectionState.Open)
                    {
                        con.Close();
                    }
                    con.Open();
                    SqlDataReader reader = cmd.ExecuteReader();
                    dtstaffcurrentorderlist.Load(reader);
                }
                con.Close();
            }
            return dtstaffcurrentorderlist;
        }
        catch (Exception)
        {
            return null;
        }
    }

在您的cellClick事件处理程序中进行检查,以处理这样的空值

if (dataGridVieworderitems.CurrentCell == null ||
    dataGridVieworderitems.CurrentCell.Value == null ||
    e.RowIndex == -1) return;

这将解决您的问题,因为它在单击GridView的单元格时检查所有可能的空值。 如果有除null之外的任何内容,else部分将获取数据。

希望能帮助到你!

Index was out of range

这意味着您的数据网格单元格上找不到索引。

如果index existscolumn相同,请检查datagrid的行。

编辑:哈,得到了确切的问题来源:
必须将SelectionMode属性设置为FullRowSelect ,才能使用选定的行填充SelectedRows属性。

否则,您可以使用以下选项:

private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            string jobId = dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString();

        }

要么

private void dataGridView1_SelectionChanged(object sender, EventArgs e)
        {
            DataGridViewRow selectedRow = dataGridView1.Rows[dataGridView1.SelectedCells[0].RowIndex];
            string jobId = selectedRow.Cells[0].Value.ToString();
        }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM