I have two tables:
TotalItems :
ItemID (int)
ItemName (Nvarchar)
ItemNumbers (int)
Items :
ID (int)
ItemName (Nvarchar)
Numbers (int)
ItemID (int)
My Items
in TotalItems
as:
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:
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:
Item1: 200 - 2 = 198
Item2: 220 - 3 = 217
Item5: 200 - 6 = 194
Finally, in TotalItems
we must have:
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 ):
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:
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.
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");
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.