Link to home
Start Free TrialLog in
Avatar of lankapala
lankapala

asked on

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

Avatar of Ryan Chong
Ryan Chong
Flag of Singapore image

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

Avatar of lankapala
lankapala

ASKER

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

 dataGridJobView.Rows.Add(j.Id.ToString(), j.Message, j.VehicleId.ToString());
ASKER CERTIFIED SOLUTION
Avatar of Ryan Chong
Ryan Chong
Flag of Singapore image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
do you need further clarifications or assistance here?
Proposed solutions worked well.