C#:GridControl之 bandedGridView中的单元格自动换行、动态设置单元格行高
1、自动换行
//自动换行
//RepositoryItemMemoEdit:一个编辑器,允许显示和编辑多行文本
RepositoryItemMemoEdit repositoryitemmemoedit = new RepositoryItemMemoEdit();
bandedGridView1.Columns["专业考核原因"].ColumnEdit = repositoryitemmemoedit;
bandedGridView1.Columns["工部考核原因"].ColumnEdit = repositoryitemmemoedit;
2、动态设置行高
//调用bandedGridView1_CalcRowHeight方法的方式,在绑定完数据源后调用
this.gridData.DataSource = dtZGXZ; //绑定数据源
this.bandedGridView1.CalcRowHeight += bandedGridView1_CalcRowHeight; //调用动态设置行高方法
/// <summary>
/// 动态设置行高
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void bandedGridView1_CalcRowHeight(object sender, DevExpress.XtraGrid.Views.Grid.RowHeightEventArgs e)
{
//获得字体的宽度和高度。
Graphics graphics = CreateGraphics();
//获取宽和高
SizeF s1 = graphics.MeasureString(bandedGridView1.GetRowCellValue(e.RowHandle, "专业考核原因").ToString(), bandedGridView1.Appearance.Row.Font);
SizeF s2 = graphics.MeasureString(bandedGridView1.GetRowCellValue(e.RowHandle, "工部考核原因").ToString(), bandedGridView1.Appearance.Row.Font);
//得到行数
double d1 = Math.Ceiling(s1.Width / bandedGridView1.Columns["专业考核原因"].Width);
double d2 = Math.Ceiling(s2.Width / bandedGridView1.Columns["工部考核原因"].Width);
//三名运算符:表示若d1 >= d2,则d = d1;反之则d = d2
Double d = d1 >= d2 ? d1 : d2;
//设置行高:行数 * 字体的高度
e.RowHeight = (int)(d * s1.Height);
}