如何使用Winforms中的c#代码将数据绑定到Winforms中的数据透视网格?

时间:2017-08-14 09:32:29

标签: c# winforms devexpress pivot-grid

我想使用代码而不是SqlDataSource将数据绑定到数据透视网格。

private void Report_Summary_Load(object sender,EventArgs e)
{
    string cs= configuraitonManager.connectionString["connectionStringDatabase"].connectionString;

    using(SqlConnection con = new SqlConnection(cs))
    {
         SqlCommand cmd = new SqlCommand("spStoredProcedureTest",con);
         cmd.CommandType = CommandType.StoredProcedure;
         con.open();

        SqlDataReader sda = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        sda.Fill(ds);
        pivotGrid.DataSource = ds;

        PivotGridField Name = new PivotGridField("Name",PivotArea.RowArea);
        PivotGrid.Fields.AddRange(new PivotGridField[] {Name});

        Name.AreaIndex = 0;

        // My pivot grid name is  "pivotGridDemo"

        //How to bind the data to the data area,column area and Row area
     }
}

我有5个字段

姓名,标准,城市,年龄,DobYear

我想设置行区域中的名称和城市列区域中的城市数据区域中的年龄和DobYear

更新

  

以上代码为添加名称为行字段。但不显示记录

1 个答案:

答案 0 :(得分:1)

试试这个:

 pivotGrid.DataSource = ds.Tables[0];
 PivotGridField fieldCity  = new PivotGridField("City", PivotArea.ColumnArea);
 fieldCity.Caption = "City";

 PivotGridField fieldName = new PivotGridField("Name", PivotArea.RowArea);
 fieldBureau.Caption = "Name";

 PivotGridField fieldCity = new PivotGridField("City", PivotArea.RowArea);
 fieldBureau.Caption = "City";

 PivotGridField fieldAge  = new PivotGridField("Age", PivotArea.DataArea);   
 fieldQte.CellFormat.FormatType = DevExpress.Utils.FormatType.Numeric;
 fieldAge.AreaIndex = 0;
 PivotGridField fieldDobYear  = new PivotGridField("DobYear ", PivotArea.DataArea);  

  pivotGrid.Fields.AddRange(new PivotGridField[] { fieldCity, fieldName , fieldAge  , fieldDobYear   });