1

I'm new to SharePoint and am developing a simple World Clock Visual Web Part, following this blogpost from SharePoint Hero.

In the blogpost the instructor describes adding a GridView to the Web Part, but doesn't elaborate on how to do so.

I've taken a guess and added a GridView to the UserControl file, but can't figure out how to reference it in order for it to be populated.

My VisualWebPart.cs file:

protected void Page_Load(object sender, EventArgs e) {

    SPTimeZoneCollection timeZoneColl = SPRegionalSettings.GlobalTimeZones;

    // Creating the table
    DataTable dt = new DataTable();


    // **** The two lines that aren't working 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 Data Source to our GridView
    grdWorldClock.DataSource = dt;
    grdWorldClock.DataBind();

    dt.Dispose();
}

And the GridView I've added to my VisualWebPartUserControl.ascx file:

<asp:GridView ID="GridView1" runat="server"></asp:GridView>

Additionally, I attempted to populate the DataTable object manually, in case for whatever reason timeZoneColl was empty, but this didn't work either:

DataTable dr = dt.newRow();
dr["Description"] = "The time for this zone"
dr["Time Zone"] = "EST"
dt.Rows.Add(dr);

The result:

enter image description here

What am I doing wrong here? Am I grabbing the GridView from the UserControl incorrectly, or perhaps adding the DataSource to it wrong?

2

You have two options

Option 1: Change the grid view ID to grdWorldClock at

<asp:GridView ID="GridView1" runat="server"></asp:GridView>

To be

<asp:GridView ID="grdWorldClock" runat="server"></asp:GridView>

Option 2: Just change the below code to use the default grid view ID GridView1

GridView1.DataSource = dt;
GridView1.DataBind();

Note: you can also add the gridview control from the toolbox

enter image description here

  • Changing the ID of the GridView tag in the UserControl doesn't seem to have any effect. I've changed it to grdWorldClock and the GridView still doesn't populate. Changing the variable name in the .cs file from grdWorldClock to GridView1, as you demonstrate in Option 2, gives me a new error specific to that line: The name GridView1 does not exist in the current context. – gsamerica Nov 24 '18 at 0:42
  • Have you tried to remove your grid view tag and add a new grid view from the toolbox as above mentioned then in the code, replace the grdWorldClock with your new gridview ID? – Mohamed El-Qassas MVP Nov 24 '18 at 10:01
  • Yes, I've deleted the old tag and created a new one directly from the toolbox and the error persists. – gsamerica Nov 26 '18 at 14:56
  • Did you replaced the grdWorldClock with your new gridview ID? – Mohamed El-Qassas MVP Nov 27 '18 at 11:28
  • Apologies, yes I have. The variable is named GridView1, which is the same as the GridView tag ID GridView1 – gsamerica Nov 28 '18 at 14:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.