We help IT Professionals succeed at work.

C# GridDataview Adding records

164 Views
Last Modified: 2017-12-04
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

Comment
Watch Question

CERTIFIED EXPERT

Commented:
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

Author

Commented:
No it's not working

Author

Commented:
I tryed add using following command but
it's adding last value

 dataGridJobView.Rows.Add(j.Id.ToString(), j.Message, j.VehicleId.ToString());
CERTIFIED EXPERT
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
CERTIFIED EXPERT
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
CERTIFIED EXPERT

Commented:
do you need further clarifications or assistance here?
CERTIFIED EXPERT

Commented:
Proposed solutions worked well.
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.