如何通过代码将绑定列添加到gridview

本文关键字:添加 gridview 绑定 何通过 代码 | 更新日期: 2023-09-27 18:17:04

我有以下添加gridview控件的web部件。现在我需要将绑定列添加到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();

        }

如何通过代码将绑定列添加到gridview

在网格视图读取代码项目文章中从代码后面添加绑定字段。