We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date
close icon

Alignment and editing

Hi,

We''re using a grid in virtual mode, with certain cells (those containing numbers) right-aligned. We use

event.Style.HorizontalAlignment = GridHorizontalAlignment.Right

for this, and it works fine. However, when we try to edit the cells, we would like them to be left-aligned in the text box - pretty much like Excel.

What do we need to do? We''ve tried messing around with the grid.CurrentCell.Renderer.Control in the CurrentCellStartEditing event handler, but to no avail.


Regards,

Giles

10 Replies

AD Administrator Syncfusion Team October 6, 2006 05:21 AM UTC

Excel moves numbers to the right, but keeps non-numveric values to the left even when directly editing in the cell. (But if you click the formulabar in excel, both numbers and non-numerics move to the left).

One way you can try to do this is in your QueryCellInfo, conditionally set the alignment depending upon whether the value you are setting in the e.Style.CellValue is numeric or not. Below is a code snippet showing how you might do this. This snippet does not try to conditionally movew numbers to the left if you are allowing the use of a GridAwareTextBox to serve as a formulabar. But such a condition code also be added checking whether the GridAwareTextBox had focus or not.

void gridControl1_QueryCellInfo(object sender, GridQueryCellInfoEventArgs e)
{
if (e.ColIndex > 0 && e.RowIndex > 0)
{
double d;
object cellValue = GetCellValue(e.RowIndex, e.ColIndex);
if (cellValue == null || cellValue.ToString().Length == 0
|| !double.TryParse(cellValue.ToString(), System.Globalization.NumberStyles.Any, null, out d)
)
{
e.Style.HorizontalAlignment = GridHorizontalAlignment.Left;
}
else
{
e.Style.HorizontalAlignment = GridHorizontalAlignment.Right;
}
e.Style.CellValue = cellValue;
}
}


GT Giles Thomas October 6, 2006 09:12 AM UTC

Hi Clay,

Yes, that''s what we are doing. The problem is that when we are editing the cell - in-place, not in a formula bar - we want it to be left-aligned. How do we achieve that?


Thanks in advance for any help,

Giles


>Excel moves numbers to the right, but keeps non-numveric values to the left even when directly editing in the cell. (But if you click the formulabar in excel, both numbers and non-numerics move to the left).

One way you can try to do this is in your QueryCellInfo, conditionally set the alignment depending upon whether the value you are setting in the e.Style.CellValue is numeric or not. Below is a code snippet showing how you might do this. This snippet does not try to conditionally movew numbers to the left if you are allowing the use of a GridAwareTextBox to serve as a formulabar. But such a condition code also be added checking whether the GridAwareTextBox had focus or not.

void gridControl1_QueryCellInfo(object sender, GridQueryCellInfoEventArgs e)
{
if (e.ColIndex > 0 && e.RowIndex > 0)
{
double d;
object cellValue = GetCellValue(e.RowIndex, e.ColIndex);
if (cellValue == null || cellValue.ToString().Length == 0
|| !double.TryParse(cellValue.ToString(), System.Globalization.NumberStyles.Any, null, out d)
)
{
e.Style.HorizontalAlignment = GridHorizontalAlignment.Left;
}
else
{
e.Style.HorizontalAlignment = GridHorizontalAlignment.Right;
}
e.Style.CellValue = cellValue;
}
}


AD Administrator Syncfusion Team October 6, 2006 10:36 AM UTC

You can try this code to see if it gives you what you want.

void gridControl1_QueryCellInfo(object sender, GridQueryCellInfoEventArgs e)
{
if (e.ColIndex > 0 && e.RowIndex > 0)
{
object cellValue = GetCellValue(e.RowIndex, e.ColIndex);
GridCurrentCell cc = this.gridControl1.CurrentCell;
if(cc.HasCurrentCellAt(e.RowIndex, e.ColIndex))
{
e.Style.HorizontalAlignment = GridHorizontalAlignment.Left;
}
else
{
e.Style.HorizontalAlignment = GridHorizontalAlignment.Right;
}
e.Style.CellValue = cellValue;
}
}


GT Giles Thomas October 6, 2006 12:25 PM UTC

Clay,

Thanks for the response. That''s almost what we want, but not quite - we only want it to be left-aligned when it''s being edited, not when it is the current cell.

Any other thoughts?


Regards,

Giles


AD Administrator Syncfusion Team October 6, 2006 12:50 PM UTC

You can try using the CurrentCellGotFocus and CurrentCellLostFocus to keep track of what cell has input focus. Then in QueryCellInfo, you can use this information to decide how to align the text.

int focusRow = -2;
int focusCol = -2;
void gridControl1_CurrentCellControlLostFocus(object sender, ControlEventArgs e)
{
focusRow = -2;
focusCol = -2;
}

void gridControl1_CurrentCellControlGotFocus(object sender, ControlEventArgs e)
{
if (focusRow == -2)
{
GridCurrentCell cc = this.gridControl1.CurrentCell;
focusRow = cc.RowIndex;
focusCol = cc.ColIndex;
cc.Refresh();
}
}

void gridControl1_QueryCellInfo(object sender, GridQueryCellInfoEventArgs e)
{
if (e.ColIndex > 0 && e.RowIndex > 0)
{
object cellValue = GetCellValue(e.RowIndex, e.ColIndex);
if (focusRow == e.RowIndex && focusCol == e.ColIndex)
{
e.Style.HorizontalAlignment = GridHorizontalAlignment.Left;
}
else
{
e.Style.HorizontalAlignment = GridHorizontalAlignment.Right;
}
e.Style.CellValue = cellValue;
}
}


JH Jonathan Hartley October 11, 2006 02:42 PM UTC

Hey there Clay, thanks for all your help to date. I''ve working on the same code as Giles (above)

Your last suggestion worked a treat. We modified it to remember the currently edited cell in our onCurrentCellStartEditing handler, instead of onCurrentCellControlGotFocus, simply because StartEditing gets called sooner, and we wanted the value to be available before GotFocus gets called.

However, with that minor tweak, it works brilliantly.

However, now we have a new, related problem. We''d like to programmatically determine what alignment a cell-being-edited has been set to.

We thought we could simply test the value of:

grid.CurrentCell.Renderer.CurrentStyle.HorizontalAlignment

but when we try to do so, for many seconds after the double-click, it returns the same old value, as if the cell alignment was not changed by the event handlers (even though, on screen, we can see that it was changed)

Mysteriously, after X seconds, where 15 < X < 90, the value returned by

grid.CurrentCell.Renderer.CurrentStyle.HorizontalAlignment

spontaneously changes (eg. from Left to Right) to match the value which was assigned by the event handler.

We don''t understand why this value doesn''t change immediately after the event handlers run.

Any suggestions? Thanks very much.


AD Administrator Syncfusion Team October 12, 2006 07:58 PM UTC

Hi Jonathan,

Please try the attached sample that is working fine, let me know if this helps.
Here is a sample
http://www.syncfusion.com/Support/user/uploads/GridAlignment_cd96d43d.zip

Regards,
Rajagopal


JH Jonathan Hartley November 13, 2006 03:44 PM UTC

Thank you Rajagopal very much for the sample.

I tried it out (it works, as you already knew :) and had a browse through the code for my edification.

Unfortunately, we can't set the horizontal alignment directly on the model as this sample code does, since we're using the grid in virtual mode - instead we set it on members in the event object during a QueryCellInfo event.



JH Jonathan Hartley November 23, 2006 11:25 AM UTC

I started a new thread for this:
http://www.syncfusion.com/support/Forums/message.aspx?&MessageID=52203


AD Administrator Syncfusion Team November 24, 2006 06:09 AM UTC

Hi Jonathan,

Your new thread has been updated.

Thanks,
Haneef

Loader.
Live Chat Icon For mobile
Up arrow icon