指数数组的边界之外 。但是所有阵列都是正确的

时间:2015-08-14 02:50:35

标签: c# mysql visual-studio-2010 visual-studio c#-4.0

你好我得到“索引超出了数组的范围”,如果我把值(i)放在itemsWS.getItemInfo(itemLis [i] .ToString())

object[] itemDetails;
object[] itemLis = itemsWS.searchItem("", "bread", "all");
int xs = 35;
int clefts = 0;

for (int i = 0; i < itemLis.Length; i++)

{
    itemDetails = itemsWS.getItemInfo(itemLis[i].ToString());
    Button myButtons = new Button();
    myButtons.Click += delegate
    {

        dataGridView1.ColumnCount = 11;

        dataGridView1.Columns[0].Name = "Item Code";
        dataGridView1.Columns[1].Name = "Description";
        dataGridView1.Columns[2].Name = "Sale Price";
        dataGridView1.Columns[3].Name = "Category";
        dataGridView1.Columns[4].Name = "Type";
        dataGridView1.Columns[5].Name = "Status";
        dataGridView1.Columns[6].Name = "Low Count";
        dataGridView1.Columns[7].Name = "Medium Count";
        dataGridView1.Columns[8].Name = "High Count";
        dataGridView1.Columns[9].Name = "Item Picture";
        dataGridView1.Columns[10].Name = "TEST";

        //here is where i'm getting error if i put i on the []

        itemDetails = itemsWS.getItemInfo(itemLis[i].ToString());

        //

        dataGridView1.Rows.Add(itemDetails);
        MessageBox.Show("data grid displayed!");

    };


    myButtons.Text = itemDetails[1].ToString() + "\n  " + itemDetails[2].ToString();
    myButtons.Top = cleft * 180;
    myButtons.Left = 70;
    myButtons.Location = new Point(xs, clefts);
    myButtons.Size = new Size(100, 60);
    tabPage1.Controls.Add(myButtons);
    xs += 135;

    if (xs >= 537)
    {
        xs = 35;
        clefts += 80;
    }

(这是数据库)

enter image description here

问题是如果我用[0]替换[i]。它只显示数据库中的第一个值项

(如果我用[0]替换[i],这里是GUI)点击任何按钮都会重复相同的输出

enter image description here

请非常感谢任何帮助。我正在尝试使用生成的新按钮显示MySQL中的项目,这些按钮将显示在datagridview上。感谢

注意: itemsWS 是来自

的网络服务
  

私人KPWSItems itemsWS = new KPWSItems();

1 个答案:

答案 0 :(得分:1)

试试这个

for (int i = 0; i < itemLis.Length; i++)
{
    itemDetails = itemsWS.getItemInfo(itemLis[i].ToString());
    Button myButtons = new Button();
    //myButtons.Tag = i;
    myButtons.Tag = itemDetails;
    myButtons.Click += (sender, args) =>
    {
         var button = sender as Button;
         dataGridView1.ColumnCount = 11;
         dataGridView1.Columns[0].Name = "Item Code";
         dataGridView1.Columns[1].Name = "Description";
         dataGridView1.Columns[2].Name = "Sale Price";
         dataGridView1.Columns[3].Name = "Category";
         dataGridView1.Columns[4].Name = "Type";
         dataGridView1.Columns[5].Name = "Status";
         dataGridView1.Columns[6].Name = "Low Count";
         dataGridView1.Columns[7].Name = "Medium Count";
         dataGridView1.Columns[8].Name = "High Count";
         dataGridView1.Columns[9].Name = "Item Picture";
         dataGridView1.Columns[10].Name = "TEST";

         //dataGridView1.Rows.Add(itemsWS.getItemInfo(itemLis[(int)button.Tag].ToString())
         dataGridView1.Rows.Add((object[])button.Tag));

         MessageBox.Show("data grid displayed!");
    };


    myButtons.Text = itemDetails[1].ToString() + "\n  " + itemDetails[2].ToString();
    myButtons.Top = cleft * 180;
    myButtons.Left = 70;
    myButtons.Location = new Point(xs, clefts);
    myButtons.Size = new Size(100, 60);
    tabPage1.Controls.Add(myButtons);
    xs += 135;

    if (xs >= 537)
    {
       xs = 35;
       clefts += 80;
    }