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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
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 ChongBusiness Systems Analyst , ex-Senior Application EngineerCommented:
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

lankapalaAuthor Commented:
No it's not working
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());
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

Ryan ChongBusiness Systems Analyst , ex-Senior Application EngineerCommented:
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

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 ChongBusiness Systems Analyst , ex-Senior Application EngineerCommented:
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

Ryan ChongBusiness Systems Analyst , ex-Senior Application EngineerCommented:
do you need further clarifications or assistance here?
Ryan ChongBusiness Systems Analyst , ex-Senior Application EngineerCommented:
Proposed solutions worked well.
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.