想要实现的效果如下:表格每一行都有一个曲线
TreeList与GridControl的设置方法类似。
1、先创建控件的数据源,我使用的是DataTable
/// <summary>
/// 生成一个DataTable
/// </summary>
/// <returns></returns>
public static DataTable GetDataSource()
{
DataTable dt = new DataTable();
dt.Columns.Add(new DataColumn("ID", typeof(Int32))
{
AllowDBNull = false,
AutoIncrement = true,
AutoIncrementSeed = 1,
AutoIncrementStep = 1
});
dt.Columns.Add(new DataColumn("Name", typeof(string)));
dt.Columns.Add(new DataColumn("Year", typeof(int)));
for (int i = 1; i <=12 ; i++)
{
dt.Columns.Add(new DataColumn($"{i}月", typeof(double)));
}
dt.Columns.Add(new DataColumn("RowSum", typeof(double)));
dt.Columns.Add(new DataColumn("RowAvr", typeof(double)));
int index = 1;
List<string> names = new List<string> { "张三","李四","王五"};
foreach (var name in names)
{
for (int year = 2021; year <= 2024; year++)
{
var random = new Random(year);
double sum = 0;
double[] numbs = new double[12];
for (int i = 0; i < numbs.Length; i++)
{
numbs[i] = random.NextDouble() * 100;
sum += numbs[i];
}
var itemArr = new List<object>() { index++, name, year };
Array.ForEach(numbs, x => itemArr.Add(x));
itemArr.Add(sum);
itemArr.Add(sum / 12 );
dt.Rows.Add(itemArr.ToArray());
}
}
return dt;
}
2、创建Column(与平时使用方法相同)
这里要注意的是:
(1)要显示曲线的那列需要指定ColumnEdit使用的是SparklineEdit控件;
(2)曲线列的UnboundType的Object
3、创建曲线列RepositoryItemSparklineEdit的View对象,具体代码如下:
//绑定数据源
gridControl1.DataSource = Bll.BllMain.GetDataSource();
//创建view对象
var view = SparklineViewBase.CreateView(SparklineViewType.Line) ;
if(view is LineSparklineView lineSparklineView)
{
//设置lineView上的每个点都有Marker
lineSparklineView.ShowMarkers = true;
//设置Marker的颜色
lineSparklineView.MarkerColor = Color.Gold;
}
//设置view的颜色
view.Color = Color.Blue;
//设置点高亮显示
view.HighlightEndPoint = true;
view.HighlightStartPoint = true;
view.HighlightMaxPoint = true;
view.HighlightMinPoint = true;
//设置点的颜色
view.MaxPointColor = Color.Red;
view.MinPointColor = Color.Orange;
view.StartPointColor = Color.DarkGreen;
view.EndPointColor = Color.Purple;
//把view对象绑定到单元格控件上
repositoryItemSparklineEdit1.View = view;
4、RepositoryItemSparklineEdit列对应的数据是Object,我们需要通过代码指定这列具体单元格对应的的数据源
private void gridView1_CustomUnboundColumnData(object sender, DevExpress.XtraGrid.Views.Base.CustomColumnDataEventArgs e)
{
if (e.IsGetData)
{
List<double> values = new List<double>();
DataRowView row = (DataRowView)e.Row;
for (int i = startIndex; i < startIndex+12; i++)
{
values.Add((double)row.Row.ItemArray[i]);
}
e.Value = values;//指定单元格的数据源
}
}
通过以上代码及设置即可实现想要的效果