简体   繁体   English

怎么得到? 更新表错误:参数名称:索引

[英]How get? Updating table error: Parameter name: index

I have two tables: 我有两个表:

TotalItems : TotalItems

ItemID (int)
ItemName (Nvarchar)
ItemNumbers (int)

Items : 项目:

ID (int)
ItemName (Nvarchar)
Numbers (int)
ItemID (int)

My Items in TotalItems as: 我在TotalItems Items为:

ItemID ItemName ItemNumbers 
1001    Item1       200 
1002    Item2       220
1003    Item3       230
1004    Item4       220
1005    Item5       200

I select 3 Items from TotalItems and add them in wpf DataGrid as: 我从TotalItems选择3个Items ,并将它们添加到wpf DataGrid如下所示:

ID  ItemName    Numbers     ItemID
1   Item1         2          1001
2   Item2         3          1003
3   Item5         6          1005

I want to save them into Items so that ItemNumbers must be updated as: 我想把它们保存到Items ,使ItemNumbers必须为更新:

Item1: 200 - 2 = 198
Item2: 220 - 3 = 217 
Item5: 200 - 6 = 194 

Finally, in TotalItems we must have: 最后,在TotalItems我们必须具有:

ItemID  ItemName    ItemNumbers
1001    Item1       198
1002    Item2       217
1003    Item3       230 
1004    Item4       220 
1005    Item5       194 

// snippet for update of TotalItems
for (int i = 0; i < gridItem.Items.Count - 1; i++)
    {
    DataBase db = new DataBase();
    DataTable dt = new DataTable();

    string a = gridItem.SelectedCells[2].ToString();
    int b=0;
    b = Convert.ToInt32(gridItem.SelectedCells[3].IsValid.ToString());

    int count, c;
    c = int.parse(comboBox1.Text);
    count = c-b;
    //Updating TotalItems table:
    db.DoCommand("update TotalItems set ItemNumbers='" + count.ToString() + "'where ItemName='" + a + "'");
    }

My problem is ( Specified argument was out of the range of valid values. Parameter name: index ): 我的问题是(( 指定的参数超出了有效值的范围。参数名称:index ):

string a = gridItem.SelectedCells[2].ToString();

and

b = Convert.ToInt32(gridItem.SelectedCells[3].IsValid.ToString());

Your main problem is that you're trying to access the selected cells in every row, which will not work unless the user has selected every individual cell; 您的主要问题是您试图访问每一行中的选定单元格,除非用户选择了每个单独的单元格,否则该行将不起作用。 this is why you're getting an invalid index exception. 这就是为什么您得到无效的索引异常的原因。

Instead of looping through rows this way, why not just look at the actual underlying data structure? 与其以这种方式遍历行,不如不看看实际的基础数据结构? Since you included a DataTable in your question, try something like: 由于您在问题中包含了DataTable表,因此请尝试以下操作:

private void ButtonBase_OnClick(object sender, RoutedEventArgs e)
{
    try
    {
        DataBase db = new DataBase();
        foreach (var row in gridItems.Items.Cast<DataRowView>())
        {
            int count;
            if (!int.TryParse(comboBox1.Text, out count))
            {
                // Log error
            }

            int b = (int) row["PropertyNameNotIndex"];
            int totalCount = count - b;

            int itemId = (int) row["ItemId"];
            db.DoCommand("update TotalItems set ItemNumbers='" + count.ToString() + "'where ItemName='" + row["ItemName"].ToString() + "'");
        }
    }
    catch (Exception ex)
    {
        // Handle DB connection errors, etc
    }
}

Notice that indexing is done on property/column name, which is going to be a lot more readable and maintainable. 注意,索引是在属性/列名上完成的,这将更加可读和易于维护。 I'm not exactly sure what your logic is with checking IsValid, other than it shouldn't make it to the row if it doesn't pass validation. 我不确定自己检查IsValid的逻辑是什么,除非它不通过验证就不应该进入行。

I used this: 我用这个:

 private void btnSave_Click(object sender, RoutedEventArgs e)
        {
            for (int i = 0; i < gridItem.Items.Count - 1; i++)
            {
                DataRowView drv = gridItem.Items[i] as DataRowView;

                string a = drv[3].ToString();                
                dt = db.MySelect("select ItemNumbers from TotalItems where ItemName='" + a + "'");
                comboBox1.ItemsSource = dt.DefaultView;
                comboBox1.DisplayMemberPath = "ItemNumbers";
                comboBox1.SelectedValuePath = "ItemNumbers";

                int b = 0;
                b = Convert.ToInt32(drv[4]);
                int c, count;
                int.TryParse(comboBox1.Text, out c);
                count = c - b;                
                db.DoCommand("update TotalItems set ItemNumbers='" + count.ToString() + "' where ItemName='" + a + "'");
            }
            MessageBox.Show("Inserted");
        }

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

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