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

ASP.NETProgramming.NET ProgrammingC#Scripting Languages

Avatar of undefined
Last Comment
Ryan Chong

8/22/2022 - Mon
Ryan Chong

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

ASKER
lankapala

No it's not working
ASKER
lankapala

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

 dataGridJobView.Rows.Add(j.Id.ToString(), j.Message, j.VehicleId.ToString());
Your help has saved me hundreds of hours of internet surfing.
fblack61
ASKER CERTIFIED SOLUTION
Ryan Chong

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
SOLUTION
Log in to continue reading
Log In
Sign up - Free for 7 days
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
Ryan Chong

do you need further clarifications or assistance here?
Ryan Chong

Proposed solutions worked well.