date的范围是在前台任意选取的区间
mc的数目是固定的22个
数据库里查询出来后像下面这样,现在想转换后显示出来
mc                                            sj         date                                            
一、主要销售指标                         3.0 2008年10月
一、主要销售指标                         0.0 2008年11月
一、主要销售指标                         0.0 2008年12月
一、主要销售指标                         0.0 2009年01月
一、主要销售指标                         0.0 2009年02月
一、主要销售指标                         0.0 2009年03月
其中:一次单机销售量                     4.0 2008年10月
其中:一次单机销售量                     0.0 2008年11月
其中:一次单机销售量                     0.0 2008年12月
其中:一次单机销售量                     0.0 2009年01月
其中:一次单机销售量                     0.0 2009年02月
其中:一次单机销售量                     0.0 2009年03月
一次配套销售量                           5.0 2008年10月
一次配套销售量                           0.0 2008年11月
一次配套销售量                           0.0 2008年12月
一次配套销售量                           0.0 2009年01月
一次配套销售量                           0.0 2009年02月
一次配套销售量                           0.0 2009年03月
我想转换成 mc                         2008年10月    2008年11月  2008年12月  2009年01月  2009年02月 2009年03月一、主要销售指标           3.0            0.0… … … …其中:一次单机销售量       4.0            0.0 … … … …一次配套销售量             5.0            0.0 … … … …或者换种查询方式直接显示成这样?

解决方案 »

  1.   

    贴一段行转列的代码你参考
                DataTable DataTable1 = new DataTable();
                DataTable1.Columns.AddRange(new DataColumn[] { new DataColumn("ID"), new DataColumn("Name"), new DataColumn("Age"), new DataColumn("Mail") });
                DataTable1.Rows.Add(1, "张三", 100, " [email protected]");
                DataTable1.Rows.Add(2, " test", 100, " [email protected] ");
                DataTable1.Rows.Add(3, "test1", 100, "[email protected] ");
                DataTable DataTable2 = new DataTable();            for (int j = 0; j < DataTable1.Rows.Count; j++)
                {
                    if (j == 0)
                    {
                        DataTable2.Columns.Add(new DataColumn());
                        DataTable2.Columns[0].ColumnName = DataTable1.Columns[0].ColumnName;
                    }
                    DataTable2.Columns.Add(new DataColumn());
                    DataTable2.Columns[j + 1].ColumnName = DataTable1.Rows[j][0].ToString();
                }            for (int i = 1; i < DataTable1.Columns.Count; i++)
                {
                    DataRow row = DataTable2.NewRow();
                    for (int j = 0; j < DataTable1.Rows.Count; j++)
                    {
                        row[0] = DataTable1.Columns[i].ColumnName.ToString();
                        row[j + 1] = DataTable1.Rows[j][i].ToString();
                    }
                    DataTable2.Rows.Add(row);
                }            dataGrid1.DataSource = DataTable1;
                dataGridView2.DataSource = DataTable1;
                dataGridView3.DataSource = DataTable2;
      

  2.   

    行列转换: 
    http://topic.csdn.net/u/20080614/17/22e73f33-f071-46dc-b9bf-321204b1656f.html?33238(总结帖子) 
    http://topic.csdn.net/u/20090912/14/25d2e1b2-f352-4713-8618-d3433ba27bef.html?99104(经典帖子)参考
      

  3.   

    如果有SQL Analysis就可以建立方体 查询。如果没有        public void Test()
            {
                DataTable srcData = new DataTable(); //数据源table
                DataTable dt = new DataTable();
                string[] cArr = new String[] { "主要销售指标" }; //列
                dt.Columns.Add("Name");
                for (int i = 1990; i < 2010; i++)
                {
                    for (int c = 1; c < 13; c++)
                    {
                        dt.Columns.Add(i + "年" + c + "月", typeof(double));
                    }
                }
                foreach (String n in cArr)
                {
                    DataRow dr = dt.NewRow();
                    dr["Name"] = n;                for (int i = 1990; i < 2010; i++)
                    {
                        for (int c = 1; c < 13; c++)
                        {
                            //这里要计算出值 
                            double totals = 0D;
                            DataRow[] drs = srcData.Select("date='" + i + "年" + c + "月" + "'");
                            foreach (DataRow tdr in drs)
                                totals += double.Parse(tdr["sj"].ToString());
                            dr[i + "年" + c + "月"] = totals;
                        }
                    }
                }
            }
      

  4.   

    能不能把表以C#中的datatable形式贴出来啊?
      

  5.   

    http://topic.csdn.net/u/20090822/14/914C5D40-6E67-4323-9CB6-070D95605D99.html