【问题标题】:Winform Datagridview cellclick error - Index was out of rangeWinform Datagridview cellclick 错误 - 索引超出范围
【发布时间】:2017-08-16 21:16:57
【问题描述】:

我有一个简单的数据网格,它列出了 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#.. 数据网格视图有数据.. 但它说索引超出范围。请有人指出我正确的方向吗?

这就是我填充网格的方式

    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;
        }
    }

【问题讨论】:

  • 嗨,Harry,您的数据网格中有多少行?
  • 数据集返回大约 25 行,永远不会为空。谢谢
  • 作为一个单独的问题,你需要 '+ string.Empty' 做什么?
  • 感谢大家的帮助。不管我做了什么..我无法让它工作。所以我只是从头开始做同样的事情,现在效果很好。 !!!

标签: c# sql-server winforms


【解决方案1】:

在您的 cellClick 事件处理程序中检查是否处理这样的空值

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

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

希望对你有帮助!

【讨论】:

  • 这不会解决问题,因为它是 SelectedRows 集合,它是空的,而不是 Cells
  • 谢谢..我知道如何避免出错..但这让我没有任何工作..为什么我遇到这个问题是我的问题..
【解决方案2】:
Index was out of range

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

如果index existscolumn 相同,请检查数据网格的行。

【讨论】:

  • 谢谢 Reds,我明白了这个问题。问题是,当数据明确可用时,索引何时不存在?我希望有人能帮助我理解这一点。
  • 例如..你指的是这样的“字符串价格 = dataGridVieworderitems.SelectedRows[0].Cells[3].Value + string.Empty;”。但是您的数据网格单元格确实只包含两列..它将返回错误“索引超出范围”。索引以 0 开头,依此类推...
  • 感谢 Reds。我仍在努力解决这个问题,但无法解决这个问题。我可以保证至少有 20 行 5 列的数据。它们都显示在网格中.. 只是我试图触发的事件在抱怨它!
  • 你能在这里展示你的样本数据吗?并返回您的代码错误。
  • 样本数据“代码”1 03100 局部臀部 VP 8.25 美元普通 2 03108 转向卡盘辊 VP 7.75 美元普通 3 03154 转向块 IW 7.50 美元普通仅冷冻 4 03204 转向点末端 6.50 美元仅普通冷冻
【解决方案3】:

已编辑: 哈,得到了确切的问题来源:
必须将 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();
        }

【讨论】:

  • 感谢陪审团,尝试了 selectionchanged 事件,但不能使用 RowIndex,因为 eventargs 没有 RowIndex 的定义。我很困惑,因为同样的事情在另一个项目上运行良好。但在这个项目上不行!
  • RowIndex 在 CellClick 事件中。 SelectionChanged 事件有 SelectedRows 集合
  • 不幸的是,仍然没有运气。我已经尝试了所有我能想到的组合,我开始拔头发了..它是如此简单..但似乎如此遥不可及!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-09-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-03-02
相关资源
最近更新 更多