Category / Section
How to set a column format in GridDataBoundGrid?
2 mins read
To set the column format of the GridDataBoundGrid control, you can use the QueryCellInfo event. In this event handler, you can set the format for the particular column.
C#
//Hooks the event in Form_Load to set the format for the columns
this.gridDataBoundGrid1.Model.QueryCellInfo += new GridQueryCellInfoEventHandler(Model_QueryCellInfo);
void Model_QueryCellInfo(object sender, GridQueryCellInfoEventArgs e)
{
if (e.ColIndex > 0 && this.gridDataBoundGrid1.Binder.InternalColumns[e.ColIndex - 1].HeaderText == "Number")
{
//sets the cellvaluetype as double
e.Style.CellValueType = typeof(double);
//sets the format for the column
e.Style.Format = "#0.00%";
}
}
VB
'Hooks the event Form_Load to set the format for the columns Private Me.gridDataBoundGrid1.Model.QueryCellInfo += New GridQueryCellInfoEventHandler(AddressOf Model_QueryCellInfo) Private Sub Model_QueryCellInfo(ByVal sender As Object, ByVal e As GridQueryCellInfoEventArgs) If e.ColIndex > 0 AndAlso Me.gridDataBoundGrid1.Binder.InternalColumns(e.ColIndex - 1).HeaderText = "Number" Then 'sets the cellvaluetype as double e.Style.CellValueType = GetType(Double) 'sets the format for the column e.Style.Format = "#0.00%" End If End Sub
The following screenshot describes the format applied for the Number column.

Sample Links: