如何通过代码将boundcolumns添加到gridview?

时间:2013-06-05 08:32:10

标签: c# asp.net sharepoint gridview webforms

我有以下webpart添加了gridview控件。 现在我需要将绑定列添加到gridview。我怎么能这样做?

protected override void CreateChildControls()
        {
            base.CreateChildControls();
            lastCreatedOpportunitiesGrid = new GridView();
            this.Controls.Add(lastCreatedOpportunitiesGrid); ///?here??
            RenderGrid();
        }

        #region Private methods
        private void RenderGrid()
        {
            string currentUrl = SPContext.Current.Site.Url;

            List<dynamic> opportunityInfo = new List<dynamic>();

            SPSecurity.RunWithElevatedPrivileges(delegate()
            {
                using (SPSite clientSiteCollection = new SPSite(currentUrl))
                {
                    foreach (SPWeb web in clientSiteCollection.AllWebs.Where(c => c.Properties["WebTemplate"] == "xx").OrderByDescending(d => d.Created).Take(5))
                    {
                        SPList opportunityInfoList = web.Lists.TryGetList(Constants.Lists.OpportunityInfoName);
                        if (opportunityInfoList != null)
                        {
                            opportunityInfo.Add(new
                            {
                                OpportunityName = opportunityInfoList.Items[0]["Opportunity Name"],
                                OpportunityCode = opportunityInfoList.Items[0]["Opportunity Code"],
                                CsLink = opportunityInfoList.Items[0]["CS Link"]
                            });
                        }

                        web.Dispose();
                    }
                }
            });

            lastCreatedOpportunitiesGrid.DataSource = opportunityInfo;
            lastCreatedOpportunitiesGrid.DataBind();


        }

1 个答案:

答案 0 :(得分:0)

在网格视图read code project article中的代码后面添加绑定字段。

希望它适合你。