I am new to SharePoint and I am developing a simple visual web element of World Clock, following this SharePoint Hero blog post.
In the blog post, the instructor describes how to add a GridView to the web element, but does not explain how to do it.
I took a guess and added a GridView to the UserControl file, but I can not figure out how to reference it to complete it.
My VisualWebPart.cs file:
void protected Page_Load (sender object, EventArgs e) {
SPTimeZoneCollection timeZoneColl = SPRegionalSettings.GlobalTimeZones;
// Creating the table
DataTable dt = new DataTable ();
// **** The two lines that do not work correctly ****
VisualWebPart1UserControl control = (VisualWebPart1UserControl) Page.LoadControl (_ascxPath);
GridView grdWorldClock = control.FindControl ("GridView1") as GridView;
// Adding columns
dt.Columns.Add ("Description");
dt.Columns.Add ("Time Zone");
foreach (SPTimeZone tz in timeZoneColl) {
DateTime currentLocalDateTime = DateTime.Now;
DateTime currentDestDateTime = tz.UTCToLocalTime (currentLocalDateTime.ToUniversalTime ());
// Adding rows
DataRow dr;
dr = dt.NewRow ();
Dr["Description"] = tz.Description.ToString ();
Dr["Time Zone"] = currentDestDateTime.ToString ();
dt.Rows.Add (dr);
}
// Connecting the data source to our GridView
grdWorldClock.DataSource = dt;
grdWorldClock.DataBind ();
K! Es EsK Es EsK Es EsK Es EsK Es EsK Es EsK Es EsK Es EsK Es EsK Es EsK Es EsK Es Es Es Es!
}
And the GridView that I have added to my VisualWebPartUserControl.ascx file:
In addition, I tried to populate the Data table
Object manually, in case it is for any reason. timeZoneColl
It was empty, but this did not work either:
DataTable dr = dt.newRow ();
Dr["Description"] = "The time for this area"
Dr["Time Zone"] = "EST"
dt.Rows.Add (dr);
The result:
What am I doing wrong here? Am I taking the GridView of the UserControl incorrectly, or maybe adding the DataSource to it incorrectly?