gridControl.QueryCellInfo += GridControl_QueryCellInfo;
private void GridControl_QueryCellInfo(object sender, GridQueryCellInfoEventArgs e)
{
if (e.Cell.RowIndex > 0 && e.Cell.ColumnIndex > 0)
{
if (dataSource.Count > e.Cell.RowIndex)
e.Style.CellValue = this.GetCellValue(dataSource[e.Cell.RowIndex - 1], e.Cell.ColumnIndex - 1);
}
else if (e.Cell.RowIndex == 0 && e.Cell.ColumnIndex > 0)
{
if (dataSource.Count > e.Cell.RowIndex)
e.Style.CellValue = this.GetColumnHeader(dataSource[0], e.Cell.ColumnIndex - 1);
}
}
private object GetCellValue(object data, int colIndex)
{
var properties = data.GetType().GetProperties(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.CreateInstance | System.Reflection.BindingFlags.Instance);
if (properties.Count() > colIndex)
{
var value = properties[colIndex].GetValue(data, null);
return value;
}
return null;
}
private string GetColumnHeader(object data, int colIndex)
{
var properties = data.GetType().GetProperties(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.CreateInstance | System.Reflection.BindingFlags.Instance);
if (properties.Count() > colIndex)
{
return properties[colIndex].Name;
}
return string.Empty;
} |
gridControl.SaveCellFormattedText += GridControl_SaveCellFormattedText;
private void GridControl_SaveCellFormattedText(object sender, GridCellTextEventArgs e)
{
if (e.Style.RowIndex > 0 && e.Style.ColumnIndex > 0)
{
if (dataSource.Count > e.Style.RowIndex)
this.SetCellValue(dataSource[e.Style.RowIndex - 1], e.Text, e.Style.ColumnIndex - 1);
}
}
private void SetCellValue(object data,object value, int colIndex)
{
var properties = data.GetType().GetProperties(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.CreateInstance | System.Reflection.BindingFlags.Instance);
if (properties.Count() > colIndex)
{
properties[colIndex].SetValue(data, value, null);
}
} |