Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Add datagridviewRows to List<>  c#

Posted on 2009-05-05
9
Medium Priority
?
712 Views
Last Modified: 2012-05-06
Hi all,

On my form I have a DataGridView wich the user can fill manuel with document lines (invoice).
When the user clicks the save doc button the document gets saved to the db and with that new record ID I then Save the documentlines into the db.
Only problem I have is that when there are say 3 rows in the DataGridView it save 3 times the last added row and not row 1,2 and 3.
Below is my code.

So I have a problem somewhere with iterating through the DataGridView but can't find what is wrong..

Thx for any help.

Grts.



#region AddLinesToList
          // Add lines to List and fill DocumentBLL.DocRegel
          // _localDocBLL is a class wich contains a List with _localRegelBLL items
          for (int i = 0; i < dataGridViewDocLijnen.Rows.Count; i++)
          {
           // Here i Fill the _localRegelBLL properties with the values of the datagridview
          // but something goes wrong here. It adds the last line as many times a there are rows...
            _localRegelBLL.Id = Convert.ToInt32(dataGridViewDocLijnen.Rows[i].Cells[0].Value);
            _localRegelBLL.Aantal = Convert.ToDouble(dataGridViewDocLijnen.Rows[i].Cells[1].Value);
            _localRegelBLL.Rgl_oms_id = Convert.ToInt32(dataGridViewDocLijnen.Rows[i].Cells[2].Value);
            _localRegelBLL.Eenheid = dataGridViewDocLijnen.Rows[i].Cells[4].Value.ToString();
            _localRegelBLL.EenheidsPrijs = Convert.ToDouble(dataGridViewDocLijnen.Rows[i].Cells[5].Value);
            _localRegelBLL.TotaalPrijs = Convert.ToDouble(dataGridViewDocLijnen.Rows[i].Cells[6].Value);
            _localRegelBLL.Rgl_doc_id = docId;
            _localDocBLL.DocRegel.Add(_localRegelBLL);
          }
          #endregion
 
          // Save document lines
          bool saveDocLines = _localDocBLL.SaveDocLines(_localDocBLL.DocRegel);

Open in new window

0
Comment
Question by:Pit76
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
9 Comments
 
LVL 9

Expert Comment

by:tculler
ID: 24309341
Where in your code is this code snippet? If it's in like a Confirm event (ie a button click or something), then it'll just append the same data 3 times (which is what's going wrong, correct?)
0
 

Author Comment

by:Pit76
ID: 24309391
Yes indeed. this is a part of the SaveButton_Click Event.
I first save the general info of the document (like customer, address etc) Then I get the new ID and then I want to save the doclines from the DataGridView. But indeed it saves the same line (lastline in DGV) 3 times or as much as there are lines in the DGV.

What a'm I doing wrong here?

Grts.
0
 
LVL 1

Expert Comment

by:pras_gupta
ID: 24309436
_localRegelBLL  is reference to the the object , Evenytime you assign the values to it  updates the reference and hence last object is reflected three time. What you need to do is Create a new object for _localRegelBLL in the for loop and then add it to the list. Hope this helps.
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 9

Expert Comment

by:tculler
ID: 24309452
This has to do with the nature of reference types vs. value types. The invocation of the "Add" method on line 15 adds the reference of the same object for as many times as there are rows, which is not quite what you want. May I also suggest using a foreach loop? Your code will look something like this:
void AddLinesToList(DataGridView myDataGridView)
{
     foreach(DataRow row in myDataGridView.Rows)
     {
          // Create a NEW object that contains the fields you
          // need (the type of this object is whatever you declared
          // "_localRegelBLL" as. Make sure it is declared
          // inside the loop.
          _localDocBLL.DocRegel.Add(_localRegelBLL);
     }
     // Save lines, etc.
}

Open in new window

0
 

Author Comment

by:Pit76
ID: 24309489
So something more like this?
Every time it passes the loop create a NEW RegelBLL instance, fill it's prperties, add it to the _localDocBLL.DocRegel List and then dispose the_localRegelBLL?

Grts and thx for your help!
void AddLinesToList(DataGridView myDataGridView)
{
     foreach(DataRow row in myDataGridView.Rows)
     {
          // Create a NEW object that contains the fields you
          // need (the type of this object is whatever you declared
          // "_localRegelBLL" as. Make sure it is declared
          // inside the loop.
          RegelBLL _localRegelBLL = new RegelBLL();
 
           _localRegelBLL.Id = Convert.ToInt32(dataGridViewDocLijnen.Rows[i].Cells[0].Value);
            _localRegelBLL.Aantal = Convert.ToDouble(dataGridViewDocLijnen.Rows[i].Cells[1].Value);
            _localRegelBLL.Rgl_oms_id = Convert.ToInt32(dataGridViewDocLijnen.Rows[i].Cells[2].Value);
            _localRegelBLL.Eenheid = dataGridViewDocLijnen.Rows[i].Cells[4].Value.ToString();
            _localRegelBLL.EenheidsPrijs = Convert.ToDouble(dataGridViewDocLijnen.Rows[i].Cells[5].Value);
            _localRegelBLL.TotaalPrijs = Convert.ToDouble(dataGridViewDocLijnen.Rows[i].Cells[6].Value);
            _localRegelBLL.Rgl_doc_id = docId;
 
          _localDocBLL.DocRegel.Add(_localRegelBLL);
          
          _localRegelBLL = null;
     }
     // Save lines, etc.
}

Open in new window

0
 
LVL 9

Expert Comment

by:tculler
ID: 24309491
Sorry, didn't check as well as I should have. In my post, DataRow should be DataGridViewRow. In the foreach loop, just mimic your code, except replace every instance of "dataGridViewDocLijnen.Rows[i]" with "row" (shortens the code quite a bit, eh? :)).

Let me know of further troubles, if any,
Nate
0
 

Author Comment

by:Pit76
ID: 24309527
K, thx for the help. Will test it tomorrow but it makes sence what you say so I think it'll work..

I'll let you know tomorrow.

Grts
0
 
LVL 9

Accepted Solution

by:
tculler earned 2000 total points
ID: 24309554
Here, I'll just re-write the code for you :) that should be easier. Just copy/paste this method into your event handling method.
private void SaveButton_Click( System.Object sender, System.EventArgs evArgs )
{
	foreach(DataGridViewRow row in dataGridViewDocLijnen.Rows)
	{
		RegelBLL temp = new RegelBLL();
 
		temp.Id = System.Convert.ToInt32(row.Cells[0].Value);
		temp.Aantal = System.Convert.ToDouble(row.Cells[1].Value);
		temp.Rgl_oms_id = System.Convert.ToInt32(row.Cells[2].Value);
		temp.Eenheid = row.Cells[4].Value.ToString();
		temp.EenheidsPrijs = System.Convert.ToDouble(row.Cells[5].Value);
		temp.TotaalPrijs = System.Convert.ToDouble(row.Cells[6].Value);
		temp.Rgl_doc_id = docId;
 
		_localDocBLL.DocRegel.Add(temp);
 
		temp = null;
	}
	// Save here
}

Open in new window

0
 

Author Closing Comment

by:Pit76
ID: 31578237
Thx! It works perfect. Thx for your help!

Grts.
0

Featured Post

Enroll in October's Free Course of the Month

Do you work with and analyze data? Enroll in October's Course of the Month for 7+ hours of SQL training, allowing you to quickly and efficiently store or retrieve data. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …

610 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question