1

Is there a way to rebind each loop in listview for a SharePoint web part ? because I have a foreach loop, and my listview binds all the iterations but delete and show the last iteration

I have a foreach (string sdc in sdcNames)

{ var financialStatementDisplayData = linq query } ,

for each iteration I want to bind financialStatementDisplayData in a listview, the problem is that I find only the last loop binded .

for example if my loop have 4 iteration, my listview will have the last iteration variable, instead of the 4 iterations together.

this is my code :

  private void BindFields()
    {
        string camlString = string.Format(CAML_ASSESSMENT, AssessmentStatus.READYTOPROCESS, AssessmentStatus.WORKINPROGRESS);
        DataTable countriesDataTable = new DataTable();
        DataTable templateDataTable = new DataTable();
        DateTime today = DateTime.Now;

         try
        {

            SPSecurity.RunWithElevatedPrivileges(delegate()
            {
                List<string> sdcNames = new List<string>();
                string rootSiteUrl = this.RootSiteUrl;

                using (SPSite rootSite = new SPSite(rootSiteUrl))
                {
                    using (SPWeb rootWeb = rootSite.OpenWeb())
                    {
                        sdcNames = SummaryReportUtilities.GetSDCList(rootWeb);
                    }
                }

            foreach (string sdc in sdcNames)
            {
                Uri siteUri = new Uri(SPUrlUtility.CombineUrl(rootSiteUrl, SITESURL + sdc));
                if (SPSite.Exists(siteUri))
                {
                    using (SPSite sdcSite = new SPSite(siteUri.AbsoluteUri))
                    {
                        using (SPWeb sdcWeb = sdcSite.OpenWeb())
                        {
                            SPQuery objQuery = new SPQuery();
                            objQuery.ViewFields = string.Format(DROPDOWNLIBRARY_FIELDS, SharePoint_Assessment_Fields.AGENTID_FIELD, SharePoint_Assessment_Fields.ID_FIELD,
                            SharePoint_Assessment_Fields.CREATED_FIELD, SharePoint_Assessment_Fields.SALESFORCECASENUMBER_FIELD, SharePoint_Assessment_Fields.COUNTRY_CODE_FIELD,
                            SharePoint_Assessment_Fields.FILELEAFREF_FIELD, SharePoint_Assessment_Fields.FILEREF_FIELD, SharePoint_Assessment_Fields.ASSESST_STATUS_FIELD,
                            SharePoint_Assessment_Fields.REVIEWTYPE_FIELD, SharePoint_Assessment_Fields.PAXORCARGO_FIELD, SharePoint_Assessment_Fields.SUBMITTEDDATE_FIELD
                            );
                            objQuery.Query = camlString;

                            SPList dropDownList = sdcWeb.GetList(SPUrlUtility.CombineUrl(sdcWeb.ServerRelativeUrl, DocumentLibrary.DROPDOWN));
                            if (dropDownList != null)
                            {

                                // Retrieves Financial Statement
                                using ( DataTable financialStatementDataTable = dropDownList.GetItems(objQuery).GetDataTable())
                                {

                                    // Retrieves Country Lists
                                 //   string rootUrl = this.currentWeb.Url.Replace(this.currentWeb.ServerRelativeUrl, string.Empty);
                                    using (SPSite RootSite = new SPSite(rootSiteUrl))
                                    {
                                        using (SPWeb RootWeb = RootSite.OpenWeb())
                                        {
                                            objQuery = new SPQuery();
                                            objQuery.ViewFields = string.Format(QUERY_FIELDS, SharePoint_Assessment_Fields.TITLE_FIELD, SharePoint_Assessment_Fields.COUNTRY_CODE_FIELD, SharePoint_Assessment_Fields.SDCNAME_FIELD);
                                            objQuery.Query = string.Empty;

                                            SPList countriesList = RootWeb.Lists.TryGetList(List.COUNTRIES);
                                            SPList templateList = RootWeb.Lists.TryGetList(List.FINANCIALASSESSMENT_TEMPLATECONFIG);

                                            if (countriesList != null)
                                            {
                                                countriesDataTable = countriesList.GetItems(objQuery).GetDataTable();
                                            }

                                            if (templateList != null)
                                            {
                                                objQuery = new SPQuery();
                                                objQuery.ViewFields = string.Format(QUERY_FIELDS, SharePoint_Assessment_Fields.COUNTRY_CODE_FIELD, SharePoint_Assessment_Fields.PAXORCARGO_FIELD, SharePoint_Assessment_Fields.TEMPLATELOCATION_FIELD);
                                                objQuery.Query = string.Empty;

                                                templateDataTable = templateList.GetItems(objQuery).GetDataTable();
                                            }
                                        }
                                    }

                                    // Joins Financial Statement and Country List using LINQ
                                    if (countriesDataTable != null && countriesDataTable.Rows.Count > 0 &&
                                        financialStatementDataTable != null && financialStatementDataTable.Rows.Count > 0)
                                    {

                                       var financialStatementDisplayData = from financialStatement in financialStatementDataTable.AsEnumerable()
                                                                            from template in templateDataTable.AsEnumerable().Where(templateItem => DataUtilities.GetSafeString(templateItem[SharePoint_Assessment_Fields.COUNTRY_CODE_FIELD])
                                                                                .Equals(DataUtilities.GetSafeString(financialStatement[SharePoint_Assessment_Fields.COUNTRY_CODE_FIELD]),
                                                                                StringComparison.OrdinalIgnoreCase) && DataUtilities.GetSafeString(templateItem[SharePoint_Assessment_Fields.PAXORCARGO_FIELD])
                                                                                .Equals(DataUtilities.GetSafeString(financialStatement[SharePoint_Assessment_Fields.PAXORCARGO_FIELD]),
                                                                                StringComparison.OrdinalIgnoreCase)).DefaultIfEmpty()
                                                                            join countries in countriesDataTable.AsEnumerable()
                                                                            on financialStatement[SharePoint_Assessment_Fields.COUNTRY_CODE_FIELD] equals countries[SharePoint_Assessment_Fields.COUNTRY_CODE_FIELD]
                                                                            select
                                                                            new
                                                                            {
                                                                                AgentId = DataUtilities.GetSafeString(financialStatement[SharePoint_Assessment_Fields.AGENTID_FIELD]),
                                                                                ItemID = financialStatement[SharePoint_Assessment_Fields.ID_FIELD],
                                                                                SalesforceCaseNumber = DataUtilities.GetSafeString(financialStatement[SharePoint_Assessment_Fields.SALESFORCECASENUMBER_FIELD]),
                                                                                CountryCode = DataUtilities.GetSafeString(financialStatement[SharePoint_Assessment_Fields.COUNTRY_CODE_FIELD]),
                                                                                FileLeafRef = DataUtilities.GetSafeString(financialStatement[SharePoint_Assessment_Fields.FILELEAFREF_FIELD]),
                                                                                FileRef = DataUtilities.GetSafeString(financialStatement[SharePoint_Assessment_Fields.FILEREF_FIELD]),
                                                                                AssStatus = DataUtilities.GetSafeString(financialStatement[SharePoint_Assessment_Fields.ASSESST_STATUS_FIELD]),
                                                                                CountryName = DataUtilities.GetSafeString(countries[SharePoint_Assessment_Fields.TITLE_FIELD]),
                                                                                SDCName = DataUtilities.GetSafeString(countries[SharePoint_Assessment_Fields.SDCNAME_FIELD]),
                                                                                ReviewType = DataUtilities.GetSafeString(financialStatement[SharePoint_Assessment_Fields.REVIEWTYPE_FIELD]),
                                                                                PaxOrCargo = DataUtilities.GetSafeString(financialStatement[SharePoint_Assessment_Fields.PAXORCARGO_FIELD]),
                                                                                Created = DataUtilities.GetSafeDateTime(financialStatement[SharePoint_Assessment_Fields.CREATED_FIELD]),
                                                                                SubmittedDate = DataUtilities.GetSafeDateTime(financialStatement[SharePoint_Assessment_Fields.SUBMITTEDDATE_FIELD]).ToString("MM/dd/yyyy", CultureInfo.InvariantCulture),


                                                                                DaysDiff = Math.Ceiling((today.Subtract(DataUtilities.GetSafeDateTime(financialStatement[SharePoint_Assessment_Fields.SUBMITTEDDATE_FIELD]))).TotalDays),
                                                                                DaysLeft =
                                                                              DataUtilities.GetSafeString(financialStatement[SharePoint_Assessment_Fields.REVIEWTYPE_FIELD]).Equals(AssessmentReviewType.NEW_APPLICATIONS, StringComparison.OrdinalIgnoreCase) ? (4 - Math.Ceiling((today.Subtract(DataUtilities.GetSafeDateTime(financialStatement[SharePoint_Assessment_Fields.SUBMITTEDDATE_FIELD]))).TotalDays)) :
                                                                              DataUtilities.GetSafeString(financialStatement[SharePoint_Assessment_Fields.REVIEWTYPE_FIELD]).Equals(AssessmentReviewType.MAJOR_CHANGES, StringComparison.OrdinalIgnoreCase) ? (4 - Math.Ceiling((today.Subtract(DataUtilities.GetSafeDateTime(financialStatement[SharePoint_Assessment_Fields.SUBMITTEDDATE_FIELD]))).TotalDays)) :
                                                                              DataUtilities.GetSafeString(financialStatement[SharePoint_Assessment_Fields.REVIEWTYPE_FIELD]).Equals(AssessmentReviewType.REINSTATEMENT, StringComparison.OrdinalIgnoreCase) ? (4 - Math.Ceiling((today.Subtract(DataUtilities.GetSafeDateTime(financialStatement[SharePoint_Assessment_Fields.SUBMITTEDDATE_FIELD]))).TotalDays)) :
                                                                              DataUtilities.GetSafeString(financialStatement[SharePoint_Assessment_Fields.REVIEWTYPE_FIELD]).Equals(AssessmentReviewType.ADHOC, StringComparison.OrdinalIgnoreCase) ? (4 - Math.Ceiling((today.Subtract(DataUtilities.GetSafeDateTime(financialStatement[SharePoint_Assessment_Fields.SUBMITTEDDATE_FIELD]))).TotalDays)) :
                                                                              DataUtilities.GetSafeString(financialStatement[SharePoint_Assessment_Fields.REVIEWTYPE_FIELD]).Equals(AssessmentReviewType.ANNUAL, StringComparison.OrdinalIgnoreCase) ? (11 - Math.Ceiling((today.Subtract(DataUtilities.GetSafeDateTime(financialStatement[SharePoint_Assessment_Fields.SUBMITTEDDATE_FIELD]))).TotalDays)) : 0
                                                                             ,
                                                                                EnableCreateButton = DataUtilities.GetSafeString(financialStatement[SharePoint_Assessment_Fields.ASSESST_STATUS_FIELD]).Equals(AssessmentStatus.READYTOPROCESS, StringComparison.OrdinalIgnoreCase) ? true : false,
                                                                                TemplateLocation = template != null ? DataUtilities.GetSafeString(template[SharePoint_Assessment_Fields.TEMPLATELOCATION_FIELD]) : string.Empty,
                                                                                OrderIndex =
                                                                              DataUtilities.GetSafeString(financialStatement[SharePoint_Assessment_Fields.REVIEWTYPE_FIELD]).Equals(AssessmentReviewType.NEW_APPLICATIONS, StringComparison.OrdinalIgnoreCase) ? 1 :
                                                                              DataUtilities.GetSafeString(financialStatement[SharePoint_Assessment_Fields.REVIEWTYPE_FIELD]).Equals(AssessmentReviewType.MAJOR_CHANGES, StringComparison.OrdinalIgnoreCase) ? 2 :
                                                                              DataUtilities.GetSafeString(financialStatement[SharePoint_Assessment_Fields.REVIEWTYPE_FIELD]).Equals(AssessmentReviewType.REINSTATEMENT, StringComparison.OrdinalIgnoreCase) ? 3 :
                                                                              DataUtilities.GetSafeString(financialStatement[SharePoint_Assessment_Fields.REVIEWTYPE_FIELD]).Equals(AssessmentReviewType.ADHOC, StringComparison.OrdinalIgnoreCase) ? 4 :
                                                                              DataUtilities.GetSafeString(financialStatement[SharePoint_Assessment_Fields.REVIEWTYPE_FIELD]).Equals(AssessmentReviewType.ANNUAL, StringComparison.OrdinalIgnoreCase) ? 5 :
                                                                              DataUtilities.GetSafeString(financialStatement[SharePoint_Assessment_Fields.REVIEWTYPE_FIELD]).Equals(AssessmentReviewType.CANCEL, StringComparison.OrdinalIgnoreCase) ? 6 : 7
                                                                            };

                                        financialStatementDisplayData = financialStatementDisplayData.OrderBy(category1 => category1.OrderIndex).ThenBy(category2 => category2.Created).ThenBy(category3 => category3.CountryCode).ThenBy(category4 => category4.SubmittedDate);


                                      //   Binds to Control                                               

                                        if (financialStatementDisplayData != null)
                                        {
                                            this.FinancialStatementListView.DataSource = financialStatementDisplayData;
                                            this.FinancialStatementListView.DataBind();
                                        }
                                    }
                                    else
                                    {
                                        this.FinancialStatementListView.DataSource = financialStatementDataTable;
                                        this.FinancialStatementListView.DataBind();
                                    }
                                }
                            }
                        }
                    }
                }
            } //end foreach


        }
              );


    }
    catch (Exception ex)
    {
        LoggingUtilities.LogToDatabase(LOG_TYPE.ERROR, ex.Message, ex.ToString(), this.currentWeb.Url, this.currentWeb.CurrentUser.LoginName);
    }
    finally
    {
        if (countriesDataTable != null)
        {
            countriesDataTable.Dispose();
        }

        if (templateDataTable != null)
        {
            templateDataTable.Dispose();
        }
    }

}

Any ideas ?

2
  • Hi , Should you share your code to can help you faster ? Commented Oct 16, 2016 at 16:51
  • @M.Qassas I share my code
    – BKChedlia
    Commented Oct 16, 2016 at 17:00

1 Answer 1

2

First of all, it's normal behavior to only bind the last iteration

because you set financialStatementDisplayData to your this.FinancialStatementListView.DataSource within foreach so it will set the last iteration.

To overcome this issue you should bind your query for each loop to one main data table then set this main data table to this.FinancialStatementListView.DataSource outside foreach

To set this query variable financialStatementDisplayData to data table do the following:

  • Before foreach define a one main data table with the expected column as the following:
DataTable dt = new DataTable(); 
dt.Columns.Add("first column name", typeof(int)); 
dt.Columns.Add("Second column name", typeof(string));
... etc for all columns with its type
  • Then at foreach after you get query variable financialStatementDisplayData do the following:
foreach (var element in financialStatementDisplayData)
{
    var row = dt.NewRow();
    row["first column name"] = element.Firstcolumnname;
    row["Second column name"] = element.Secondcolumnname;
    dt.Rows.Add(row);
}
  • After foreach completed set the data source for list view as the following
this.FinancialStatementListView.DataSource = dt;
this.FinancialStatementListView.DataBind();
1
  • you are the best. you help people without judgement. thank you a lot @M.Qassas
    – BKChedlia
    Commented Oct 17, 2016 at 11:08

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.