当前位置: 首页>>代码示例>>C#>>正文


C# DataTable.SetColumnsOrder方法代码示例

本文整理汇总了C#中DataTable.SetColumnsOrder方法的典型用法代码示例。如果您正苦于以下问题:C# DataTable.SetColumnsOrder方法的具体用法?C# DataTable.SetColumnsOrder怎么用?C# DataTable.SetColumnsOrder使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在DataTable的用法示例。


在下文中一共展示了DataTable.SetColumnsOrder方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: SearchDataTableToCSV

    protected void SearchDataTableToCSV()
    {
        GetFinancialYear();
        GetActualMonths();
        GetForecastMonths();

        SqlCommand cmd = new SqlCommand("SELECT DISTINCT TM.Resource_ID, TM.Project_ID, TM.Client_Name, TM.Sector_Name, TM.Project_Name, TM.Role_Name, TM.Resource_Name, TM.Type_Name,  TM.Source_Name, TM.Resource_Buy, TM.Resource_Sell" + strActualMonths + strForecastMonths + " FROM (SELECT TL.Resource_ID, PI.Project_ID, CL.Client_Name, RC.Resource_Name, PR.Resource_Buy, PR.Resource_Sell, ER.Role_Name, ES.Source_Name, PI.Project_Name, PS.Sector_Name, PT.Type_Name" + strActualMonths + " FROM  ProjectInformation as PI, RateCard as RC, TotalLookup as TL, ProjectResources as PR, ProjectSector as PS, ProjectType as PT, EmployeeRole as ER, EmployeeSource as ES, MonthlyTotals as TM, Clients as CL WHERE  PI.Project_ID = TL.Project_ID AND PI.Project_Client = CL.Client_ID AND  TL.Resource_ID = RC.Resource_ID  AND  PR.Project_ID = TL.Project_ID  AND  PR.Resource_ID = RC.Resource_ID  AND  PI.Project_Sector = PS.Sector_ID AND  PI.Project_Type = PT.Type_ID  AND  TL.IsActual = 'true'  AND  TM.Total_ID = TL.Total_ID  AND RC.Resource_Type = 'Employee' AND ER.Role_ID = PR.Employee_Role AND ES.Source_ID = PR.Employee_Source AND (RC.Resource_Name LIKE '%" + strSearchTerm + "%' OR PI.Project_Name LIKE '%" + strSearchTerm + "%' OR PT.Type_Name LIKE '%" + strSearchTerm + "%' OR PS.Sector_Name LIKE '%" + strSearchTerm + "%')) as TM  LEFT OUTER JOIN(SELECT  PI.Project_ID, RC.Resource_Name" + strForecastMonths + "  FROM  ProjectInformation as PI, RateCard as RC, TotalLookup as TL, ProjectResources as PR, ProjectSector as PS, ProjectType as PT, MonthlyTotals as SM  WHERE  PI.Project_ID = TL.Project_ID AND  TL.Resource_ID = RC.Resource_ID AND  PR.Project_ID = PI.Project_ID AND  PR.Project_ID = TL.Project_ID AND  PR.Resource_ID = RC.Resource_ID AND  PI.Project_Sector = PS.Sector_ID AND  PI.Project_Type = PT.Type_ID AND  TL.IsActual = 'false' AND  SM.Total_ID = TL.Total_ID AND RC.Resource_Type = 'Employee' AND (RC.Resource_Name LIKE '%" + strSearchTerm + "%' OR PI.Project_Name LIKE '%" + strSearchTerm + "%' OR PT.Type_Name LIKE '%" + strSearchTerm + "%' OR PS.Sector_Name LIKE '%" + strSearchTerm + "%')) as SM on TM.Resource_Name = SM.Resource_Name AND TM.Project_ID = SM.Project_ID ORDER BY TM.Client_Name, TM.Project_Name, TM.Resource_Name", SQLcon); SQLcon.Open();

        dt = new DataTable();
        SqlDataAdapter sqlAd = new SqlDataAdapter(cmd);
        sqlAd.Fill(dt);
        SQLcon.Close();

        ActualIfPresent();

        //Convert nulls to zero's
        foreach (DataRow row in dt.Rows)
        {
            foreach (DataColumn col in dt.Columns)
            {
                if (row.IsNull(col))
                    row.SetField(col, 0);
            }
        }


        //Remove unnecessary columns
        dt.Columns.Remove("Resource_ID");
        dt.Columns.Remove("Project_ID");

        dt.Columns.Add("Margin", typeof(double), "((Resource_Sell-Resource_Buy)/Resource_Sell)*100");
        dt.Columns.Add("Margin2");
        dt.Columns.Add("Resource_Sell_Euro");
        dt.Columns.Add("Blank_Column");
        dt.Columns.Add("Total_Days");
        dt.Columns.Add("Total_Days2");
        dt.SetColumnsOrder("Client_Name", "Sector_Name", "Project_Name", "Type_Name", "Role_Name", "Resource_Name", "Source_Name", "Resource_Sell_Euro", "Resource_Sell", "Blank_Column", "Resource_Buy", "Margin2", "Total_Days", "Total_Days2");

        int intRowIndex = 0;

        foreach (DataRow row in dt.Rows)
        {
            double dblRowTotal = 0;
            for (int i = 14; i <= 25; i++)
            {
                dblRowTotal += (double)(dt.Rows[intRowIndex][i]);
            }
            dt.Rows[intRowIndex]["Total_Days"] = dblRowTotal;
            dt.Rows[intRowIndex]["Total_Days2"] = dblRowTotal;
            dt.Rows[intRowIndex]["Margin2"] = (dt.Rows[intRowIndex]["Margin"]);
            if ((string)dt.Rows[intRowIndex]["Margin2"] == "∞" || (string)dt.Rows[intRowIndex]["Margin2"] == "-∞" || (string)dt.Rows[intRowIndex]["Margin2"] == "NaN" || (string)dt.Rows[intRowIndex]["Margin2"] == "Infinity" || (string)dt.Rows[intRowIndex]["Margin2"] == "-Infinity")
            {
                dt.Rows[intRowIndex]["Margin2"] = 0;
            }
            else
            {
                dt.Rows[intRowIndex]["Margin2"] = Math.Round(Convert.ToDouble(dt.Rows[intRowIndex]["Margin2"].ToString()), 0);
            }
            intRowIndex++;
        }

        dt.Columns.Remove("Margin");

        //Rename columns
        dt.Columns["Resource_Name"].ColumnName = "Name";
        dt.Columns["Project_Name"].ColumnName = "Project Name";
        dt.Columns["Sector_Name"].ColumnName = "Sector";
        dt.Columns["Client_Name"].ColumnName = "Client";
        dt.Columns["Type_Name"].ColumnName = "Type";
        dt.Columns["Role_Name"].ColumnName = "Role";
        dt.Columns["Source_Name"].ColumnName = "Source";
        dt.Columns["Resource_Buy"].ColumnName = "Pay (GBP)";
        dt.Columns["Resource_Sell"].ColumnName = "Sell (GBP)";
        dt.Columns["Resource_Sell_Euro"].ColumnName = "Sell (EUR)";
        dt.Columns["Blank_Column"].ColumnName = " ";
        dt.Columns["Total_Days"].ColumnName = "Total Days";
        dt.Columns["Total_Days2"].ColumnName = "Currently Mapped Out";
        dt.Columns["Margin2"].ColumnName = "Margin (%)";

        Response.Clear();
        Response.Buffer = true;
        Response.AddHeader("content-disposition", "attachment;filename=ResourceExport.csv");
        Response.Charset = "";
        Response.ContentType = "application/text";
        StringBuilder sBuilder = new System.Text.StringBuilder();
        IEnumerable<string> columnNames = dt.Columns.Cast<DataColumn>().Select(column => column.ColumnName);
        sBuilder.AppendLine(string.Join(",", columnNames));

        foreach (DataRow row in dt.Rows)
        {
            IEnumerable<string> fields = row.ItemArray.Select(field => field.ToString());
            sBuilder.AppendLine(string.Join(",", fields));
        }
        Response.Output.Write(sBuilder.ToString());
        Response.Flush();
        Response.End();
    }
开发者ID:neuJMulholland,项目名称:PMsystem,代码行数:97,代码来源:Resources.aspx.cs


注:本文中的DataTable.SetColumnsOrder方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。