Reputation: 271
I have the following code to fill datagridview from a returning datatable.
DataTable dtCourse = Course.GetAllCourses();
for (int i = 0; i < dt.Rows.Count; i++)
{
dgvAvailableCourses.Rows.Add();
dgvAvailableCourses[0, i].Value = dtCourse.Rows[i][0];
//set image
dgvAvailableCourses[1, i].Value = SMSV100.Properties.Resources.media_playback_stop_6;
dgvAvailableCourses[2, i].Value = dtCourse.Rows[i][1];
dgvAvailableCourses[3, i].Value = dtCourse.Rows[i][2];
dgvAvailableCourses[4, i].Value = dtCourse.Rows[i][3];
dgvAvailableCourses[5, i].Value = dtCourse.Rows[i][4];
dgvAvailableCourses[6, i].Value = dtCourse.Rows[i][5];
dgvAvailableCourses[7, i].Value = dtCourse.Rows[i][6];
Console.WriteLine(dgvAvailableCourses.DataSource);
}
dgvAvailableCourses
is the DataGridview
dtCourses
is the returned datatable
from the GetAllCourses()
As you can see I have mapped the columns of the datagridview
to the columns of the datatable
and added rows. Funny things is when I tried to read the datasource
using the console.writeline()
I've used a break point, it showed null for the datasource
.
How come this is possible why this datasource showed as null while it already assigned with data through the loops?
How do I overcome this, I need to access the underlying table of this datagridview
in order to do some calculations but I cannot do that because it shows as null?
PS- Datagridview
shows data without any problems but it shows null
for the datasource
thanks
Upvotes: 0
Views: 1139
Reputation: 141
I'm guessing that you didn't bind the DataGridView to your source, you're just adding rows manually. If you do dgvAvailableCourses.DataSource = dtCourse
, I think that you will have the binding done "correcty" (I'm assuming that you had a good reason to use a loop instead of just settng the DataSource property).
Upvotes: 3