DevExpress Gridcontrol and the Demo header

Recommended for you: Get network issues from WhatsUp Gold. Not end users.

Written on the front.:

  Stroll in the garden for a while, always want to write something, but suffer from their own level is limited, afraid to write to waste the reader's time. I have the honor to participate in the training of company DevExpress controls, not only happy people happy, attached to the readers of Demo!



using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Text;

using System.Windows.Forms;

using DevExpress.XtraEditors;


using DevExpress.XtraGrid.Columns;

using DevExpress.XtraGrid.Views.Base;

using DevExpress.XtraGrid.Views.BandedGrid;

using DevExpress.XtraEditors.Repository;


namespace XtraGridDemo1

{

public partial class XtraForm1 : DevExpress.XtraEditors.XtraForm

{

public XtraForm1()

{

InitializeComponent();


//First into a form on the GridControl, click on “ Click here to change view” link in the pop-up menu, select“convert to”-->“AdvBandedGridView”.


InitGrid();

}


private void XtraForm1_Load(object sender, EventArgs e)

{


}

///Initialization table

private void InitGrid()

{


// AdvBandedGridView1 is the default view on the form, pay attention to this statement is: BandedGridView

BandedGridView view = advBandedGridView1 as BandedGridView;


view.BeginUpdate(); //Begin to view editor, prevent triggering other events

view.BeginDataUpdate(); //Start data editor


view.Bands.Clear();


//Modify additional options

view.OptionsView.ShowColumnHeaders = false;

//Because of the Band column, so the ColumnHeader.

view.OptionsView.ShowGroupPanel = false;

//If you don't need to block, just remove it

view.OptionsView.EnableAppearanceEvenRow = false;

//Whether to enable the even line appearance

view.OptionsView.EnableAppearanceOddRow = true;

//Whether to enable the odd appearance

view.OptionsView.ShowFilterPanelMode = ShowFilterPanelMode.Never;   //Whether to display the filter panel

view.OptionsCustomization.AllowColumnMoving = false;

//Whether to allow the mobile column

view.OptionsCustomization.AllowColumnResizing = false;

//Whether to allow to adjust the column widths

view.OptionsCustomization.AllowGroup = false;

//Whether to allow grouping

view.OptionsCustomization.AllowFilter = false;

//Whether to allow filtering

view.OptionsCustomization.AllowSort = true;

//Whether to allow sorting

view.OptionsSelection.EnableAppearanceFocusedCell = true;

//???

view.OptionsBehavior.Editable = false;

//If the user is allowed to edit cell


//Add the column headings

GridBand bandID = view.Bands.AddBand("ID");

bandID.Visible = false; //Hide ID column

GridBand bandName = view.Bands.AddBand("Full name");

GridBand bandSex = view.Bands.AddBand("Gender");

GridBand bandBirth = view.Bands.AddBand("Date of birth");

GridBand bandScore = view.Bands.AddBand("Fraction");

GridBand bandMath = bandScore.Children.AddBand("Mathematics");

GridBand bandChinese = bandScore.Children.AddBand("Chinese");

GridBand bandEnglish = bandScore.Children.AddBand("English");

GridBand bandSubTotal = bandScore.Children.AddBand("Subtotal");

GridBand bandRemark = view.Bands.AddBand("Remarks");


//The column title alignment

bandName.AppearanceHeader.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Center;

bandSex.AppearanceHeader.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Center;

bandBirth.AppearanceHeader.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Center;

bandScore.AppearanceHeader.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Center;

bandMath.AppearanceHeader.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Center;

bandChinese.AppearanceHeader.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Center;

bandEnglish.AppearanceHeader.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Center;

bandSubTotal.AppearanceHeader.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Center;

bandRemark.AppearanceHeader.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Center;


//Simulation of several data

List<Record> listDataSource = new List<Record>();

listDataSource.Add(new Record(1, "Zhang San "," male", Convert.ToDateTime("1989-5-6"), 115.5f, 101, 96, ""));

listDataSource.Add(new Record(2, "Li Si "," female", Convert.ToDateTime("1987-12-23"), 92, 85, 87, ""));

listDataSource.Add(new Record(3, "Wang Wu "," female", Convert.ToDateTime("1990-2-11"), 88, 69, 41.5f, ""));

listDataSource.Add(new Record(4, "Zhao Liu "," male ", Convert.ToDateTime (" 1988-9-1 "), 119, 108, 110," remarks."));

//The bound data source and display

gridControl1.DataSource = listDataSource;

gridControl1.MainView.PopulateColumns();


//[subtotal] this row because no bound data source, so the need to manually add

//(a bit complicated, slowly see)

string[] fieldNames = new string[] { "SubTotal" };

GridColumn column;   //State statement

column = view.Columns.AddField(fieldNames[0]);  //Add a data field

column.VisibleIndex = view.Columns.Count - 1;  //Set the column shows the position in the edit view (second to last column). 

column.UnboundType = DevExpress.Data.UnboundColumnType.Decimal;

column.OptionsColumn.AllowEdit = false;

//This column cannot be edited

column.Visible = true;

view.Columns.Add(column);   //Add a list view


//Binding events, when [scores] changing [subtotal] has changed

//(if only for binding events can change the cell value, directly modify the invalid)

view.CustomUnboundColumnData += new

DevExpress.XtraGrid.Views.Base.CustomColumnDataEventHandler(advBandedGridView1_CustomUnboundColumnData);


//[sex] column binding ComboBox

RepositoryItemComboBox riCombo = new RepositoryItemComboBox();

riCombo.Items.AddRange(new string[] { "Male "," female" });

gridControl1.RepositoryItems.Add(riCombo);

view.Columns["Sex"].ColumnEdit = riCombo;


//[date] column binding Date

RepositoryItemDateEdit riDate = new RepositoryItemDateEdit();

gridControl1.RepositoryItems.Add(riDate);

view.Columns["Birth"].ColumnEdit = riDate;


//[scores] column binding SpinEdit

RepositoryItemSpinEdit riSpin = new RepositoryItemSpinEdit();

gridControl1.RepositoryItems.Add(riSpin);

view.Columns["Math"].ColumnEdit = riSpin;

view.Columns["Chinese"].ColumnEdit = riSpin;

view.Columns["English"].ColumnEdit = riSpin;


//[note] column binding MemoExEdit

RepositoryItemMemoExEdit riMemoEx = new RepositoryItemMemoExEdit();

gridControl1.RepositoryItems.Add(riMemoEx);

view.Columns["Remark"].ColumnEdit = riMemoEx;



//Subtotal column add summary

view.OptionsView.ShowFooter = true;

//To display the table footer

view.Columns["SubTotal"].SummaryItem.FieldName = "SubTotal";

view.Columns["SubTotal"].SummaryItem.DisplayFormat = "{0:f2}";

view.Columns["SubTotal"].SummaryItem.SummaryType = DevExpress.Data.SummaryItemType.Average;


//The column headers and data column

view.Columns["ID"].OwnerBand = bandID;

view.Columns["Name"].OwnerBand = bandName;

view.Columns["Sex"].OwnerBand = bandSex;

view.Columns["Birth"].OwnerBand = bandBirth;

view.Columns["Math"].OwnerBand = bandMath;

view.Columns["Chinese"].OwnerBand = bandChinese;

view.Columns["English"].OwnerBand = bandEnglish;

view.Columns["SubTotal"].OwnerBand = bandSubTotal;

view.Columns["Remark"].OwnerBand = bandRemark;


view.EndDataUpdate();//End of data editing

view.EndUpdate();   //The end view of the editor



}


// Calculate subtotals

private float calcSubTotal(float math, float chinese, float english)

{

return math + chinese + english;

}



private void advBandedGridView1_CustomUnboundColumnData(object sender, CustomColumnDataEventArgs e)

{

ColumnView colView = sender as ColumnView;

if (e.Column.FieldName == "SubTotal" && e.IsGetData) e.Value = calcSubTotal(

Convert.ToSingle(colView.GetRowCellValue(e.RowHandle, colView.Columns["Math"])),

Convert.ToSingle(colView.GetRowCellValue(e.RowHandle, colView.Columns["Chinese"])),

Convert.ToSingle(colView.GetRowCellValue(e.RowHandle, colView.Columns["English"])));

}


#Data source implementation of the Ilist interface to the binding of the region runtime


public class Record

{

int id;

DateTime birth;

string name, sex, remark;

float math, chinese, english;

public Record(int id, string name, string sex, DateTime birth, float math, float chinese, float english, string remark)

{

this.id = id;

this.name = name;

this.sex = sex;

this.birth = birth;

this.math = math;

this.chinese = chinese;

this.english = english;

this.remark = remark;

}

public int ID { get { return id; } }

public string Name

{

get { return name; }

set { name = value; }

}

public string Sex

{

get { return sex; }

set { sex = value; }

  }

public DateTime Birth

{

get { return birth; }

set { birth = value; }

}

public float Math

{

get { return math; }

set { math = value; }

}

public float Chinese

{

get { return chinese; }

set { chinese = value; }

}

public float English

{

get { return english; }


set { english = value; }

}

public string Remark

{

get { return remark; }

set { remark = value; }

}



}


#endregion


}

}

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download

Posted by Howard at November 16, 2013 - 2:53 PM