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.
Unfortunately, activation email could not send to your email. Please try again.

Alignment and editing

Thread ID:

Created:

Updated:

Platform:

Replies:

50103 Oct 5,2006 10:58 AM Nov 24,2006 01:09 AM Windows Forms 10
loading
Tags: GridControl
Giles Thomas
Asked On October 5, 2006 10:58 AM

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

Administrator [Syncfusion]
Replied On October 6, 2006 01:21 AM

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;
}
}

Giles Thomas
Replied On October 6, 2006 05:12 AM

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;
}
}

Administrator [Syncfusion]
Replied On October 6, 2006 06:36 AM

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;
}
}

Giles Thomas
Replied On October 6, 2006 08:25 AM

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

Administrator [Syncfusion]
Replied On October 6, 2006 08:50 AM

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;
}
}

Jonathan Hartley
Replied On October 11, 2006 10:42 AM

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.

Administrator [Syncfusion]
Replied On October 12, 2006 03:58 PM

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

Jonathan Hartley
Replied On November 13, 2006 10:44 AM

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.


Jonathan Hartley
Replied On November 23, 2006 06:25 AM

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

Administrator [Syncfusion]
Replied On November 24, 2006 01:09 AM

Hi Jonathan,

Your new thread has been updated.

Thanks,
Haneef

CONFIRMATION

This post will be permanently deleted. Are you sure you want to continue?

Sorry, An error occured while processing your request. Please try again later.

You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.

;