As stated in the title, I was unable to right-align my devexpress grid view columns after I assigned values to the rows, especially the columns with value 0 assigned to it. (Shown below)
Where I assign values to rows
public DataTable CalculateMasterFields(DataTable masterTable, string fileFormat)
{
DataTable masterFields = masterTable.Clone();
foreach (DataRow row in masterTable.Rows)
{
masterFields.ImportRow(row);
}
decimal finalTotalRow = row["FinalTotal"] == DBNull.Value ? 0m : Math.Round(Convert.ToDecimal(row["FinalTotal"]), 2);
bool voided = row["Cancelled"] == DBNull.Value ? false : Converter.TextToBoolean(row["Cancelled"]);
decimal finalTotal = voided ? 0m : Math.Round(finalTotalRow, 2);
decimal voidedAmt = voided ? Math.Round(finalTotalRow, 2) : 0;
bool refund = (!voided && finalTotal <= 0);
decimal refundAmt = (!voided && finalTotal <= 0) ? finalTotal : 0;
decimal tax = row["Tax"] == DBNull.Value ? 0m : Math.Round(Convert.ToDecimal(row["Tax"]), 2);
decimal serviceCharge = row["ServiceCharge"] == DBNull.Value ? 0m : Math.Round(Convert.ToDecimal(row["ServiceCharge"]), 2);
decimal tipAmount = row["TipAmount"] == DBNull.Value ? 0m : Math.Round(Convert.ToDecimal(row["TipAmount"]), 2);
row["ServiceCharge"] = serviceCharge;
row["TipAmount"] = tipAmount;
row["Tax"] = tax;
row["FinalTotal"] = finalTotal;
row["Void"] = voided ? 1 : 0;
row["VoidAmount"] = voidedAmt;
row["Refund"] = refund ? 1 : 0;
row["RefundAmount"] = refundAmt;
return masterFields;
}
I've tried to align the columns using column.AppearanceCell.Options.UseTextOptions = true; column.AppearanceCell.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Far;
but so far there is no changes to the alignment. (Shown below)
How I align my columns
private void AlignGridColumns()
{
GridView[] gridViews = { gvDailySales, gvDetail, gvItemDetail };
foreach (var gridView in gridViews)
{
if (gridView != null && gridView.GridControl.DataSource is DataTable dt)
{
foreach (GridColumn column in gridView.Columns)
{
if (dt.Columns.Contains(column.FieldName))
{
AlignColumns(column, dt.Columns[column.FieldName].DataType);
}
}
}
}
}
private void AlignColumns(GridColumn column, Type columnType)
{
if (columnType == typeof(decimal) || columnType == typeof(double) || columnType == typeof(float) || columnType == typeof(long) || columnType == typeof(int)
&& column.FieldName != "Void" && column.FieldName != "Refund")
{
column.AppearanceCell.Options.UseTextOptions = true;
column.AppearanceCell.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Far;
column.DisplayFormat.FormatType = DevExpress.Utils.FormatType.Numeric;
column.DisplayFormat.FormatString = "N2";
}
}
By removing the if condition from AlignColumns
, all other columns will be aligned to right, only the ones which were assigned with values from CalculateMasterFields
will be left-aligned.
I've also checked the datatypes of the columns that I want to edit, and it is indeed in Decimals or Int format, not string (String will be automatically left-aligned as from what I saw from Devexpress community).
And below is how I initiate and create the grid columns:
How I create my grid columns
private void btnSearch_Click(object sender, EventArgs e)
{
myMasterTable = myListing.CalculateMasterFields(mySalesListingTable, myFileFormat);
myDetailTable = myListing.CalculateDetailFields(mySalesListingDetailTable, myFileFormat);
// Create DataRelation for Master-Detail1 if detail table has rows
if (mySalesListingDetailTable.Rows.Count > 0)
{
DataSet dataSet = new DataSet();
dataSet.Tables.Add(myMasterTable);
dataSet.Tables.Add(myDetailTable);
dataSet.Relations.Add("MasterDetailRelation1",
myMasterTable.Columns["DocNo"],
myDetailTable.Columns["DocNo"]);
gridControl1.DataSource = dataSet.Tables[0];
gridControl1.LevelTree.Nodes.Add("MasterDetailRelation1", gvDetail);
AlignGridColumns();
gvDailySales.OptionsDetail.ShowDetailTabs = false;
}
else
{
gridControl1.DataSource = myMasterTable;
AlignGridColumns();
}
}
private void CreateGridColumn(string caption, string fieldName, bool isVisible, bool isMaster)
{
GridColumn gridColumn = new GridColumn();
gridColumn.FieldName = fieldName;
gridColumn.Caption = caption;
gridColumn.AppearanceCell.Font = new System.Drawing.Font("Tahoma", 9.75F);
gridColumn.AppearanceCell.Options.UseFont = true;
gridColumn.AppearanceHeader.Font = new System.Drawing.Font("Tahoma", 9.75F, System.Drawing.FontStyle.Bold);
gridColumn.AppearanceHeader.Options.UseFont = true;
gridColumn.OptionsColumn.AllowEdit = false;
gridColumn.Visible = isVisible;
if (isMaster)
{
if (gvDailySales.Columns.ColumnByName(fieldName) == null)
gvDailySales.Columns.Add(gridColumn);
}
else
{
if (gvDetail.Columns.ColumnByName(fieldName) == null)
gvDetail.Columns.Add(gridColumn);
}
}
private void CreateMasterTableColumns(string myFileFormat)
{
CreateMasterColumns(CreateGridColumn);
CreateDetailColumns(CreateGridColumn);
}
Would appreciate any help or advice given. Thanks in advance!
The logic of columns alignment using AlignGridColumns
and AlignColumns
is interfering with the workings of my InitializeFormControl
, which used to define the fields' data types for them to be aligned in the gridview.
InitializeFormControl()
private void InitializeFormControl()
{
FormControlUtil f = new FormControlUtil(myDBSetting);
// Currency fields
f.AddField("TotalExTax", FormControlUtil.CURRENCY_FIELD);
f.AddField("ServiceCharge", FormControlUtil.CURRENCY_FIELD);
f.InitControls(this);
}
So, removing both AlignGridColumns
and AlignColumns
solved the issue.
Credits to my senior.