0

I am creating a web part on SharePoint that shows site owners and their email addresses. I am able to show that information in a listbox, but I would rather do it in a gridview. When I change my code to display the info in a gridview, the gridview does not display.

Here is the code I have:

namespace DisplaySiteOwnersVWP.VisualWebPart1
{
[ToolboxItemAttribute(false)]
public partial class VisualWebPart1 : WebPart
{

    string mySiteURL = "my Site URL goes here";
    DataTable dtTable = new DataTable();


    public VisualWebPart1()
    {
    }


    protected override void OnInit(EventArgs e)
    {
        base.OnInit(e);
        InitializeControl();
    }


    protected void Page_Load(object sender, EventArgs e)
    {

        dtTable.Columns.Add("Name");
        dtTable.Columns.Add("Email");

        using (SPSite site = new SPSite(mySiteURL))
        {
            using (SPWeb web = site.RootWeb)
            {
                SPGroup ownerGroup = web.AssociatedOwnerGroup;

                if (ownerGroup != null)
                {
                    foreach (SPUser user in ownerGroup.Users)
                    {
                        if (user.IsSiteAdmin == true)
                        {
                            DataRow dtRow = dtTable.NewRow();
                            dtRow["Name"] = user.Name;
                            dtRow["Email"] = user.Email;
                        }
                    }
                }

                gvOwners.DataSource = dtTable;
                gvOwners.DataBind();

            }
        }

    } } }

1 Answer 1

1

After

dtRow["Name"] = user.Name;
dtRow["Email"] = user.Email;

Write

dtRow.update();

Well, speaking more broadly, whenever you make a change to a list item, make sure you cal the update method to actually make the changes to the list item.

11
  • I get the red squiggly under the word update. Intellisense doesn't pick it up. The error says "System.Data.DataRow does not contain a definition for 'update' and no extension 'update' accepting a first argument of type System.Data.DataRow could be found"
    – Mischa
    Jul 1, 2014 at 13:09
  • Yeah, just looked at the function again, seems to only work on a list item. Give me a bit, and I'll see if I can dig anything else up :)
    – Brandon C.
    Jul 1, 2014 at 13:13
  • I'm guessing gvOwners is a formfield you created somewere else?
    – Brandon C.
    Jul 1, 2014 at 13:15
  • Yes. I have it in the .ascx file like this: <asp:GridView ID="gvOwners" runat="server"></asp:GridView>
    – Mischa
    Jul 1, 2014 at 13:17
  • I can't seem to find any documentation on the .databind method you are using. There is .AddDataBinding for a method call, and there is ddrwt:DataBind in an aspx page, but I think you are confusing the two here.
    – Brandon C.
    Jul 1, 2014 at 13:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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