C# GridDataview Adding records

Hi,
I need to add records to dataGridview using loop

see below my code is not working
  dataGridJobView.ColumnCount = 3;
            dataGridJobView.Columns[0].Name = "Job Number";
           dataGridJobView.Columns[1].Name = "Message";
            dataGridJobView.Columns[2].Name = "Vehicle Number";
 if (jobs != null)
                 {
                     foreach (var j in jobs)
                     {
                         row = new string[] { j.Id.ToString(), j.Message.ToString(),j.VehicleId.ToString() };
                         //dataGridJobView.Rows.Add(row);
                         //this.dataGridJobView.Rows.Add(j.Id.ToString(), j.Message.ToString(), j.VehicleId.ToString());

                         
                         count = count + 1;
                         //MessageBox.Show(String.Format("id:{0} {1} {2}",j.Id,j.Message,j.VehicleId));
                     }
                    
                 }
                 dataGridJobView.DataSource = row;

Open in new window

LVL 2
lankapalaAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ryan ChongCommented:
try:

dataGridJobView.ColumnCount = 3;
            dataGridJobView.Columns[0].Name = "Job Number";
            dataGridJobView.Columns[1].Name = "Message";
            dataGridJobView.Columns[2].Name = "Vehicle Number";
            
            dataGridJobView.Columns[0].DataPropertyName = "Id";
            dataGridJobView.Columns[1].DataPropertyName = "Message";
            dataGridJobView.Columns[2].DataPropertyName = "VehicleId";
            dataGridJobView.DataSource = jobs;

Open in new window

0
lankapalaAuthor Commented:
No it's not working
0
lankapalaAuthor Commented:
I tryed add using following command but
it's adding last value

 dataGridJobView.Rows.Add(j.Id.ToString(), j.Message, j.VehicleId.ToString());
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

Ryan ChongCommented:
No it's not working
it's working for me.

class Job
    {
        public int Id { get; set; }
        public String Message { get; set; }
        public int VehicleId { get; set; }

        public Job(int Id, String Message, int VehicleId)
        {
            this.Id = Id;
            this.Message = Message;
            this.VehicleId = VehicleId;

        }
    }

Open in new window


List<Job> jobs = new List<Job>();
            jobs.Add(new Job(1, "test 1", 1234));
            jobs.Add(new Job(2, "test 2", 2234));
            jobs.Add(new Job(3, "test 3", 3234));
            jobs.Add(new Job(4, "test 4", 4234));
            jobs.Add(new Job(5, "test 5", 5234));
            

            dataGridJobView.ColumnCount = 3;
            dataGridJobView.Columns[0].Name = "Job Number";
            dataGridJobView.Columns[1].Name = "Message";
            dataGridJobView.Columns[2].Name = "Vehicle Number";
            
            dataGridJobView.Columns[0].DataPropertyName = "Id";
            dataGridJobView.Columns[1].DataPropertyName = "Message";
            dataGridJobView.Columns[2].DataPropertyName = "VehicleId";
            dataGridJobView.DataSource = jobs;

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Ryan ChongCommented:
this worked for me as well

List<Job> jobs = new List<Job>();
            jobs.Add(new Job(1, "test 1", 1234));
            jobs.Add(new Job(2, "test 2", 2234));
            jobs.Add(new Job(3, "test 3", 3234));
            jobs.Add(new Job(4, "test 4", 4234));
            jobs.Add(new Job(5, "test 5", 5234));
            string[] row = null;
            int count = 0;

            dataGridJobView.ColumnCount = 3;
            dataGridJobView.Columns[0].Name = "Job Number";
            dataGridJobView.Columns[1].Name = "Message";
            dataGridJobView.Columns[2].Name = "Vehicle Number";
            
            if (jobs != null)
            {
                foreach (var j in jobs)
                {
                    row = new string[] { j.Id.ToString(), j.Message.ToString(), j.VehicleId.ToString() };
                    dataGridJobView.Rows.Add(row);
                    count = count + 1;
                }
            }

Open in new window

0
Ryan ChongCommented:
do you need further clarifications or assistance here?
0
Ryan ChongCommented:
Proposed solutions worked well.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.