如何绑定DevExpress PivotGridControl到数据库

如何绑定DevExpress PivotGridControl到数据库,第1张

本文通过一个具体的事例,为大家阐述如何绑定DevExpress的 PivotGridControl 到数据库

事例数据库如下:

代码如下:

C#

using DevExpress.LookAndFeel

using DevExpress.XtraPivotGrid

using System.Data.OleDb

// Create a connection object.

OleDbConnection connection =

new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0Data Source=C:\\DB\\NWIND.MDB")

// Create a data adapter.

OleDbDataAdapter adapter = new OleDbDataAdapter("SELECT * FROM SalesPerson", connection)

// Create and fill a dataset.

DataSet sourceDataSet = new DataSet()

adapter.Fill(sourceDataSet, "SalesPerson")

// Assign the data source to the XtraPivotGrid control.

pivotGridControl1.DataSource = sourceDataSet.Tables["SalesPerson"]

// Create a row PivotGridControl field bound to the Country datasource field.

PivotGridField fieldCountry = new PivotGridField("Country", PivotArea.RowArea)

// Create a row PivotGridControl field bound to the Sales Person datasource field.

PivotGridField fieldCustomer = new PivotGridField("Sales Person", PivotArea.RowArea)

fieldCustomer.Caption = "Customer"

// Create a column PivotGridControl field bound to the OrderDate datasource field.

PivotGridField fieldYear = new PivotGridField("OrderDate", PivotArea.ColumnArea)

fieldYear.Caption = "Year"

// Group field values by years.

fieldYear.GroupInterval = PivotGroupInterval.DateYear

// Create a column PivotGridControl field bound to the CategoryName datasource field.

PivotGridField fieldCategoryName = new PivotGridField("CategoryName", PivotArea.ColumnArea)

fieldCategoryName.Caption = "Product Category"

// Create a filter PivotGridControl field bound to the ProductName datasource field.

PivotGridField fieldProductName = new PivotGridField("ProductName", PivotArea.FilterArea)

fieldProductName.Caption = "Product Name"

// Create a data PivotGridControl field bound to the 'Extended Price' datasource field.

PivotGridField fieldExtendedPrice = new PivotGridField("Extended Price", PivotArea.DataArea)

fieldExtendedPrice.CellFormat.FormatType = DevExpress.Utils.FormatType.Numeric

// Specify the formatting setting to format summary values as integer currency amount.

fieldExtendedPrice.CellFormat.FormatString = "c0"

// Add the fields to the control's field collection.

pivotGridControl1.Fields.AddRange(new PivotGridField[] {fieldCountry, fieldCustomer,

fieldCategoryName, fieldProductName, fieldYear, fieldExtendedPrice})

// Arrange the row fields within the Row Header Area.

fieldCountry.AreaIndex = 0

fieldCustomer.AreaIndex = 1

// Arrange the column fields within the Column Header Area.

fieldCategoryName.AreaIndex = 0

fieldYear.AreaIndex = 1

// Customize the control's look-and-feel via the Default LookAndFeel object.

UserLookAndFeel.Default.UseWindowsXPTheme = false

UserLookAndFeel.Default.Style = LookAndFeelStyle.Skin

UserLookAndFeel.Default.SkinName = "Money Twins"

这里,我的解决办法是,建两个map,一个从index 到uid,一个相反。

然后,初始化时,以数据库里的信息来装载。

用户需要改变时,在d出的对话框前,对数据做点手脚,这样看起来就一直对了。

代码

declare two map:

[csharp] view plain copy

int[] arLabelToWorkTypeId

Dictionary<int, int>mapWorktypeId2LabelIndex=null

load labes from database when from load:

[csharp] view plain copy

private void Your_Load(object sender, EventArgs e)

{

// TODO: This line of code loads data into the 'schedulerDBDataSet.Resources' table. You can move, or remove it, as needed.

this.resourcesTableAdapter.Fill(this.schedulerDBDataSet.Resources)

// TODO: This line of code loads data into the 'schedulerDBDataSet.Appointments' table. You can move, or remove it, as needed.

this.appointmentsTableAdapter.Fill(this.schedulerDBDataSet.Appointments)

InitializeLabels()

//////////////////////////////////////////////////////////////////////////

schedulerControl.ActiveViewType = DevExpress.XtraScheduler.SchedulerViewType.Timeline

schedulerControl.GroupType = SchedulerGroupType.Resource

AdjustResourceHeaders()

cbView.EditValue = schedulerControl.ActiveViewType

cbGrouping.EditValue = schedulerControl.GroupType

}

private void InitializeLabels()

{

this.workTypeTableAdapter.Fill(this.schedulerDBDataSet.WorkType)

DataTable labels = this.schedulerDBDataSet.WorkType

if (labels.Rows.Count == 0)

return

schedulerControl.Storage.Appointments.Labels.Clear()

schedulerControl.Storage.Appointments.Labels.BeginUpdate()

arLabelToWorkTypeId = new int[labels.Rows.Count]

mapWorktypeId2LabelIndex = new Dictionary<int, int>()

for (int i = 0i <labels.Rows.Counti++)

{

Color color = Color.FromArgb(Int32.Parse(labels.Rows[i]["Color"].ToString()))

string dislayName = labels.Rows[i]["name"].ToString()

string menuCaption = labels.Rows[i]["name"].ToString()

AppointmentLabel aptLabel = new AppointmentLabel(color, dislayName, menuCaption)

schedulerControl.Storage.Appointments.Labels.Add(aptLabel)

arLabelToWorkTypeId[i] = int.Parse(labels.Rows[i]["WorktypeID"].ToString())

mapWorktypeId2LabelIndex.Add(arLabelToWorkTypeId[i],i)

}

schedulerControl.Storage.Appointments.Labels.EndUpdate()

}

[csharp] view plain copy

private void schedulerControl_EditAppointmentFormShowing(object sender, AppointmentFormEventArgs e)

{

DevExpress.XtraScheduler.SchedulerControl scheduler = ((DevExpress.XtraScheduler.SchedulerControl)(sender))

//appoint make a copy

DevExpress.XtraScheduler.Appointment tmpAppointment = e.Appointment

//converty to worktype

int originalId = tmpAppointment.LabelId

int labelIdx=0

if (true == mapWorktypeId2LabelIndex.TryGetValue(tmpAppointment.LabelId,out labelIdx))

{

tmpAppointment.LabelId = labelIdx

}

DevExpress.XtraScheduler.Demos.Modules.CustomAppointmentForm form = new DevExpress.XtraScheduler.Demos.Modules.CustomAppointmentForm(scheduler, tmpAppointment, e.OpenRecurrenceForm)

try

{

e.DialogResult = form.ShowDialog()

if (DialogResult.OK == e.DialogResult)

{

e.Appointment.LabelId = arLabelToWorkTypeId[tmpAppointment.LabelId]

}

else

{

e.Appointment.LabelId = originalId

}

e.Handled = true

}

finally

{

form.Dispose()

}

}

//新生成一个

private void schedulerControl_InitNewAppointment(object sender, AppointmentEventArgs e)

{

//赋到第一个值

e.Appointment.LabelId = arLabelToWorkTypeId[0]

}

场景部署设计如下:1.数据库库字段设计设计如下:数据库中有一个User(用户表), 里面有一个Status字段 bigint类型 代表用户状态 1=启用 Active 2=禁用 Inactive2.实体类设计如下: [Serializable] publicclass UserInfo { privatelong status = 1///<summary>/// 用户状态。

1)获取数据库中的数据radioGroupStatus.SelectedIndex = (int)userInfo.Status-1备注:radioGroupStatus是RadioGroup 控件name userInfo是实体类的 *** 作对象 radioGroupStatus.Properties.Item中的值value分别是0,1 2)保存选取中的值userInfo.Status = radioGroupStatus.SelectedIndex+1杂记:GridView.CustomDrawCell事件可以对GridView展示的列进行处理. gridView1.CustomDrawCell += gridView1_CustomDrawCellvoid gridView1_CustomDrawCell(object sender, DevExpress.XtraGrid.Views.Base.RowCellCustomDrawEventArgs e) { if (e.Column.FieldName == "Status") { switch (e.CellValue.ToInt()) { case1: e.DisplayText = "启用"breakcase2: e.DisplayText = "禁用"break} } }


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/sjk/10869501.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-11
下一篇 2023-05-11

发表评论

登录后才能评论

评论列表(0条)

保存