C# foreach statement - c#

I need some help with a for each statement, basically what happens is when a user edits a value within a cell, my foreach will apply this to all cells within the datagrid and change the value of them all, i need my foreach statement to work by iterating through the datagrid but only change the selected row that has been edited
try
{
//loop through each of the rows in the dgv
foreach (DataGridViewRow row in dgvDetials.SelectedRows)
{
int intQtyInsp = 0;
//if quantity inspected is empty:
if (row.Cells[2].Value.ToString() == "")
{
//quantity inspected is 0. Prevents null value errors:
intQtyInsp = 0;
}
intQtyInsp =
Int32.Parse(dgvDetials.CurrentRow.Cells[2].Value.ToString());
if (intQtyInsp < 0) // checks the cell for a negative value
{
intQtyInsp = 0; // if cells is negative submits value as Zero
}
else
{
//sets quantity inspected to value entered
intQtyInsp = Int32.Parse(row.Cells[2].Value.ToString());
}
if (intQtyInsp == 0) //if quantity inspected is 0. Ignore row.
{
}
else //else gather details and insert row as production.
{
area = dtArea2.Rows[0]["area_code"].ToString();
inspDate = dtpInspectionDate.Value.ToString("MM/dd/yyyy");
inspShift = cbShift.Text;
partNo = row.Cells[0].Value.ToString();
// dieCode = row.Cells[0].Value.ToString();
dieCode = "";
machine = "";
qtyInsp = intQtyInsp;
qtyInspRecorded = Int32.Parse(row.Cells[5].Value.ToString());
comment = "";
//machine = row.Cells[3].Value.ToString();
if (qtyInspRecorded == 0)
{
SQLMethods.insertProduction(area,
inspDate,
inspShift,
partNo,
dieCode,
qtyInsp,
comment,
machine);
}
else
{
SQLMethods.updateProduction(area,
inspDate,
inspShift,
partNo,
dieCode,
(qtyInspRecorded + qtyInsp),
comment,
machine);
}
}
}
retrieveData(); //reset values
}
catch (Exception ex)
{
MessageBox.Show(
"Error instering production values. Processed with error: "
+ ex.Message);
}

First of all, I would simplify the code here a little by splitting it into several methods that may be called from the For-loop. That would make it easier to read, and thereby easier to help you too. Just to provide an example, the following:
if (intQtyInsp < 0) // checks the cell for a negative value
{
intQtyInsp = 0; // if cells is negative submits value as Zero
}
else
{
//sets quantity inspected to value entered
intQtyInsp = Int32.Parse(row.Cells[2].Value.ToString());
}
could be replaced with something like:
int intQtyInsp = SetQuantityInspected();
Then that method could contain the if-structure. Repeat this for other parts of the code in the loop too. Trust me, this will make your life easier.
Also, it seems as if the result of this section is never used; the value of intQtyInsp is overwritten right afterwards!:
if (row.Cells[2].Value.ToString() == "")
{
//quantity inspected is 0. Prevents null value errors:
intQtyInsp = 0;
}
As for your question: I'm not sure how you would get the id of the row that is currently being edited. (If possible (?), it might be getter to loop through the table / data source behind the datagrid?).
In any case, what you need to do is something like the following inside your loop:
if(IsCurrentlyEditedRow(row)){
...
// (all the stuff currently in the body of your loop goes here)
...
}
Now you can implement the method IsCurrentlyEditedRow() to return True or False depending on whether or not the id of the current row is the the same as that of the one you are editing.
Sorry if this is not a very specific and detailed answer, hope it is of some use anyway.

Related

Get last column in Google Sheets API v4 in c#

I have seen many posts about finding the last row of a given column for Google Sheets API v4 in C#, but I can't seem to find anything about finding the last column of a given row. I didn't find any questions about this specifically - but if I'm mistaken please direct me to the right place.
In my sheet, I have headers for each column. Over time, I anticipate I will need to add or remove columns as needed - it would be great to not have to update my code every time this happens.
I'm at the beginning stages of writing my code that gathers my data from Google Sheets - but here is what I have so far. I know that I will need to change the way my variable "range" is written, just don't know what.
static void ReadEntries()
{
var range = $"{sheet}!A1:ET";
var request = service.Spreadsheets.Values.Get(SpreadsheetId, range);
var response = request.Execute();
var values = response.Values;
if(values != null && values.Count>0)
{
foreach (var row in values)
{
System.Diagnostics.Debug.WriteLine("{0} | {1} | {2}", row[0], row[1], row[2]);
}
}
else
{
System.Diagnostics.Debug.WriteLine("No data found.");
}
}
EDIT: SOLVED
I used the pseudo code provided by Nazi A for this. I was having issues with the if(row[col]) piece with casting and other system exceptions. It turns out foreach allows for us to not have to check if that row[col] is in range. Below is my final code in case anyone needs it in the future. I plan to let column "ET" declared in var range = $"{sheet}!A1:ET; be big enough to accommodate any future columns being added to my spreadsheet. Thanks for your help!
static void ReadEntries()
{
var range = $"{sheet}!A1:ET";
var request = service.Spreadsheets.Values.Get(SpreadsheetId, range);
var response = request.Execute();
var values = response.Values;
int max = 0;
int currMax;
if (values != null && values.Count>0)
{
foreach(var row in values)
{
currMax = 0;
foreach(var col in row)
{
currMax++;
}
if (max < currMax)
{
max = currMax;
}
}
}
else
{
System.Diagnostics.Debug.WriteLine("No data found.");
}
System.Diagnostics.Debug.WriteLine(max);
}
So basically, you need to have a nested loop to traverse all rows and columns in values.
I have tested this psuedo code and worked but since I have no any means to run a C# code, this is all I can give to you. This is a pseudo code that should be readable to you.
var max = 0;
foreach(var row in values){
var currMax = 0;
foreach(var col in row){
if(row[col]){ // as long as data exists, currMax will increment
currMax++;
continue;
}
break; // stop loop if last cell being checked is empty
}
if(max < currMax){ // assign the largest currMax to max
max = currMax;
}
}
So in this psuedo code, max will contain the value of the largest column of all rows in the range. this code above should replace your foreach call
If you have any questions, feel free to clarify below.

How to update data in database using Entity Framework

I want to update data in a database using values from datagridview but I have not succeeded. My aim is to search through my datagrid view and if my product name exist in gridview, then I update the quantity.
if (bunifuDataGridView1.Rows.Count > 0)
{
foreach (DataGridViewRow row in bunifuDataGridView1.Rows)
{
if (Convert.ToString(row.Cells[2].Value) == bunifuTextBox11.Text)
{
row.Cells[5].Value = Convert.ToString(Convert.ToInt32(bunifuTextBox10.Text) + Convert.ToInt32(row.Cells[5].Value));
found = true;
obj5.ProductName = Convert.ToString(row.Cells[2].Value);
obj5.CostPricePerProduct = Convert.ToInt32(row.Cells[3].Value);
obj5.SellingPricePerProduct = Convert.ToInt32(row.Cells[4].Value);
obj5.Quantity = Convert.ToInt32(row.Cells[5].Value);
obj5.ExpiryDate = Convert.ToString(row.Cells[6].Value);
obj5.ProductNumber = Convert.ToInt32(obj2.ProductNumber);
obj5.Quantity = Convert.ToInt32(row.Cells[5].Value);
context.Entry.state = Entrystate.modified;
context.SaveChanges();
inboundgoods();
refreshcustomergrid();
}
}
if (!found)
{
inboundgoods();
}
}
else
{
inboundgoods();
}
I wish for my code to be able to search through datagridview for product name, and if there is a match, it should update that record by incrementing the stock quantity and save in stock database.
This is hard to debug without having the full app in front of us, but we can recommend some code changes that will assist with debugging:
if (bunifuDataGridView1.Rows.Count > 0)
{
foreach (DataGridViewRow row in bunifuDataGridView1.Rows)
{
// Compare the Product on each row, add a watch to this value to assist debugging
var product = Convert.ToString(row.Cells[2].Value);
if (product == bunifuTextBox11.Text) // consider rename bunfuTextBox11 to something meaningful, like 'ProductNameTextBox'
{
row.Cells[5].Value = Convert.ToString(Convert.ToInt32(bunifuTextBox10.Text) + Convert.ToInt32(row.Cells[5].Value)); // consider rename bunifuTextBox10 to something more meaningful like 'ProductQuantityTextBox'
found = true;
obj5.ProductName = Convert.ToString(row.Cells[2].Value);
obj5.CostPricePerProduct = Convert.ToInt32(row.Cells[3].Value);
obj5.SellingPricePerProduct = Convert.ToInt32(row.Cells[4].Value);
obj5.Quantity= Convert.ToInt32(row.Cells[5].Value);
obj5.ExpiraryDate = Convert.ToString(row.Cells[6].Value);
obj5.ProductNumber = Convert.ToInt32(obj2.ProductNumber);
obj5.Quantity = Convert.ToInt32(row.Cells[5].Value);
//context.Entry.state=Entrystate.modified;
// If your context has automatic change tracking enabled, this following line is not necessary
// But you need to make sure you are setting the State on the correct object tracker instance by passing it in to the Entry method.
var dbEntry = g.Entry(obj5);
if (dbEntry.State == EntryState.Detached)
dbEntry.State = EntryState.Added;
else
dbEntry.State = EntryState.Modified;
context.SaveChanges();
inboundgoods();
refreshcustomergrid();
}
}
if (!found)
{
inboundgoods();
}
}
else
{
inboundgoods();
}
If you are not getting to the found = true; line of code during debugging then review your comparison logic, look for spelling and whitespace issues, you may want to change the comparison to something like this if your inputs or stored data might have blank spaces or inconsistent letter casing.
if (product.Trim().Equals(bunifuTextBox11.Text.Trim(), StringComparison.OrdinalIgnoreCase))
Take the time to use meaningful names for your data entry field controls, it will make you code easier to read and understand, especially when you post code examples to forums like SO!

for statement inside a for each in c#

What is wrong?
Basically want to extract each code in each row of table "Service"
And if it is equal to specific text then set each corresponding row with matching text.
foreach (DataRow code in dsAuthors.Tables["Service"].Rows)
{
for (int i = 0; i < dsAuthors.Tables[1].Rows.Count; i++)
{
if (code[1].ToString() == "01")
{
Shipment.Rows[i][0] = "Service 1";
}
else if (code[1].ToString() == "02")
{
Shipment.Rows[i][0] = "Service 2";
}
else if (code[1].ToString() == "03")
{
Shipment.Rows[i][0] = "Service 3";
}
}
}
It just fills all rows in with Service 1 but i don't want it to.
Sorry was NOT meant to have both tables be the same i have updated the code to be more accurate i believe.
You're looping on the same thing twice, then poking a value into shipment rows unbounded. It doesn't look very safe and given you're looping and setting the shipment rows through the entire collection inside the foreach, it means the last value will determine the content of ALL shipment rows.
Check it out in a debugger, you'll see that when you hit a Service 2 value, all shipment rows will be set to 2 in the loop etc.
You're iterating over the list of rows twice. You're reading data from the variable from the outer loop, and writing to the index from the inner loop. It's writing "Service 1" to all rows because the last row is "01", and the inner loop writes that to all rows.
Try this instead:
var Service = dsAuthors.Tables["Service"];
for (int i = 0; i < Service.Rows.Count; i++)
{
if (Service.Rows[i][1].ToString() == "01")
{
Shipment.Rows[i][0] = "Service 1";
}
else if (Service.Rows[i][1].ToString() == "02")
{
Shipment.Rows[i][0] = "Service 2";
}
}

ComboBox.ObjectCollection doesn't update if the new value's string representation is case-insensitively equal to the current value

If I try to change a value in a ComboBox's Items, it will only actually update if the new value is different from the current value after a case-insensitive compare.
Let's make a ComboBox with one item:
ComboBox cboBox = new ComboBox();
cboBox.Items.Add("Apple");
The following code will make the ComboBox still show "Apple", even though the string should look different:
cboBox.Items[0] = "APPLE";
And the naive workaround that I've been using, which will make it display correctly:
cboBox.Items[0] = "";
cboBox.Items[0] = "APPLE";
I wanted to figure out how this was happening, so I dug around with a reflector and found this. This is the ComboBox.ObjectCollection.SetItemInternal method that gets called when you try to modify a value:
internal void SetItemInternal(int index, object value)
{
...
this.InnerList[index] = value;
if (this.owner.IsHandleCreated)
{
bool flag = index == this.owner.SelectedIndex;
if (string.Compare(this.owner.GetItemText(value), this.owner.NativeGetItemText(index), true, CultureInfo.CurrentCulture) != 0)
{
this.owner.NativeRemoveAt(index);
this.owner.NativeInsert(index, value);
if (flag)
{
this.owner.SelectedIndex = index;
this.owner.UpdateText();
}
if (this.owner.AutoCompleteSource == AutoCompleteSource.ListItems)
{
this.owner.SetAutoComplete(false, false);
return;
}
}
else
{
if (flag)
{
this.owner.OnSelectedItemChanged(EventArgs.Empty);
this.owner.OnSelectedIndexChanged(EventArgs.Empty);
}
}
}
}
That true in string.Compare is telling it to ignore the case of the string. Why was this method chosen for deciding whether or not to update the value? And why didn't they expose the case sensitivity?
Is there an alternative way to update an item in an ObjectCollection so that I don't have to guess whether or not it actually gets updated?
EDIT: I should note that the DropDownStyle is set to DropDownList: this is a read-only ComboBox that occasionally needs to be updated due to actions elsewhere in the program.
Try this, add a SelectedIndexChanged event, and inside it put:
int index = cboBox.SelectedIndex;
if (index - 1 >= 0) {
cboBox.SelectedIndex = index - 1;
cboBox.SelectedIndex = index;
}
else if (index + 1 < cboBox.InnerList.Count) {
cboBox.SelectedIndex = index + 1;
cboBox.SelectedIndex = index;
}
This is probably as "naive" as your work around, but maybe worth a try?
After submitting a report to the MSDN, it was marked as "by-design" and nothing more, so that's that.

C# Updating Data Issue

I am currently using one button for inserting/updating content within a table. It then takes the uploaded CSV and inserts or updates it into a data table depending on whether the row exists or not.
Here is the code fired after the button's OnClick:
if (ExcelDDL.SelectedValue == "Time Points" && fileName == "TimePoints.csv")
{
var GetTPoints = (SEPTA_DS.TimePointsTBLDataTable)tpta.GetDataByCategory(CategoryDDL.SelectedItem.ToString());
//Loop through each row and insert into database
int i = 0;
foreach (DataRow row in TempRouteDataTable.Rows)
{
//Gather column headers
var category = Convert.ToString(CategoryDDL.SelectedItem);
var agency = Convert.ToString(row["Agency"]);
if (agency == null || agency == "")
{
//If row is empty skip it entirely
goto skipped;
}
var route = Convert.ToString(row["Route"]);
var GetRShortName = (SEPTA_DS.RoutesTBLDataTable)rta.GetDataByRouteID(route);
var newRoute = "";
if (GetRShortName.Rows.Count > 0)
{
newRoute = Convert.ToString(GetRShortName.Rows[0]["route_short_name"]);
}
var direction = Convert.ToString(row["Direction"]);
var serviceKey = Convert.ToString(row["Service Key"]);
var language = Convert.ToString(row["Language"]);
var stopID = Convert.ToString(row["Stop ID"]);
var stopName = Convert.ToString(row["Stop Name"]);
if (stopName.Contains("accessible"))
{
string[] noHTML = stopName.Split('>');
int insertH = Convert.ToInt32(hta.InsertHandicapRow(newRoute,noHTML[2]));
}
var sequence = Convert.ToString(row["Sequence"]);
var origID = -1;
if (GetTPoints.Rows.Count > 0)
{
origID = Convert.ToInt32(GetTPoints.Rows[i]["TPointsID"]);
var GetID = (SEPTA_DS.TimePointsTBLDataTable)tpta.GetDataByID(origID);
if (GetID.Rows.Count < 1)
{
origID = -1;
}
}
if (origID == -1)
{
int insertData = Convert.ToInt32(tpta.InsertTimePoints(category, agency, newRoute, direction, serviceKey, language, stopID, stopName, sequence));
}
else
{
int updateData = Convert.ToInt32(tpta.UpdateTimePoints(category, agency, newRoute, direction, serviceKey, language, stopID, stopName, sequence, origID));
}
skipped:
i++;
}
}
You can see how I check whether to insert or update around the bottom. I am using this method across other sections of this program and it works just fine. But in this case it is distorting my datatable immensely and I can't figure out why.
This is the bottom part of my table after inserting [no items currently within the database]:
This is the table after reuploading the CSV with data already existing within the table:
I am also getting this error when updating There is no row at position 2230.
What is going wrong in the code to cause this huge shift? I am just checking to see if the ID exists and if it does update rather than insert.
Also the reason i am using goto is because there are blank rows in the document that need to be skipped.
Is your TPointsID column, a auto-generated number? If so, since you are skipping the empty row, some referential integrity problem might be occuring,because of empty data in the skipped rows in the database.
From the error : There is no row at position 2230 , it is also understood that, because of the skipping you might be trying to access some non existent row in the datatable.
Also, if possible consider using the ADO.NET DataAdapter which has got the CRUD operation capability. You can find more about it at : http://support.microsoft.com/kb/308507

Categories