Free Support Forum - aspose.com

One more problem

Greetings!

So - I've got a problem with desktop grid - I don't want user to input anythin into cells

So I code like this

private void gridDesktop_CellDataChanged(object sender, Aspose.Grid.Desktop.CellEventArgs e)

{

this.gridDesktop.CellDataChanged -= new Aspose.Grid.Desktop.CellEventHandler(this.gridDesktop_CellDataChanged);

e.Cell.Value=e.Argument;

this.gridDesktop.CellDataChanged += new Aspose.Grid.Desktop.CellEventHandler(this.gridDesktop_CellDataChanged);

}

It works in all cases with exception for case when u try to clear cell - grid allows this operation for some reason Surprise [:O]

I debugged the code and found, that it fires the event 2(!) times when we clear the cell (instead of one time for any other op) and for some reason it works not correctly (or maybe I've missed the point).

I've told Nick.Liu 'bout these strange 2 times fired event, but it seemed to him then that everythin' is ok .

So, please, help me.

Hi Kola,

Style class provides CellLocked property to prevent user from inputing anything. You can set the style to a range of cells, rows or a columns. If you don't want user input anything in a worksheet, you can set Protected property equal true. Example:

Worksheet sheet = gridDesktop.Worksheets[0];
Style style = sheet.Cells["c2"].GetStyle();
style.CellLocked = true;
CellRange cr = new CellRange("c2", "f6");
sheet.SetStyle(cr, style);

Can these serve you?

OK, but the problem with inadequate behaviour still remains - check the problem mentioned above

Hi,

In GridDesktop control, there are 3 situations to raise CellDataChanged event:

First, call SetValue() method;

second, assign value to Cell.Value property.

last, user inputs value in a cell.

In CellDataChanged event handler, if you assign value to e.Cell.Value, the event will be raised again.

So, if you don't want user input in a cell, you can set cell style's CellLocked property.

If you want to check user input value, please use CustomValidation.

Are these helpful to you?

Yeah, thanks a lot Nick - I really appreciate your help!!

It's great support for me!!

But still, if u debug the code below - u'll see that when e.Cell.Value==null - behaviour becomes strange Surprise [:O]

private void gridDesktop_CellDataChanged(object sender, Aspose.Grid.Desktop.CellEventArgs e)

{

this.gridDesktop.CellDataChanged -= new Aspose.Grid.Desktop.CellEventHandler(this.gridDesktop_CellDataChanged);

e.Cell.Value=e.Argument;

this.gridDesktop.CellDataChanged += new Aspose.Grid.Desktop.CellEventHandler(this.gridDesktop_CellDataChanged);

}

Hi Kola,

I tried the code, anything can not be inputed. Does the strange behavior mean this?

No - try to input anyhin in any cell

Then apply event handler

Then try to clear this cell

IT GETS CLEARED

THAT IS STRANGE,INADEQUATE BEHAVIOUR, BECAUSE EVENT HANDLER IS AIMED TO PREVENT ANY CHANGES!!

Is the problem clear to u now?

Hi Kola,

I tried the code according to your steps.

First, in Form_Load event handler, set "aaaaa" string value to "b2" cell:

this.gridDesktop.CellDataChanged -= new Aspose.Grid.Desktop.CellEventHandler(this.gridDesktop_CellDataChanged);
gridDesktop.Worksheets[0].Cells["b2"].Value = "aaaaa";
this.gridDesktop.CellDataChanged += new Aspose.Grid.Desktop.CellEventHandler(this.gridDesktop_CellDataChanged);

Second, in CellDateChanged event handler, prevent any changes:

this.gridDesktop.CellDataChanged -= new Aspose.Grid.Desktop.CellEventHandler(this.gridDesktop_CellDataChanged);
e.Cell.Value=e.Argument;
this.gridDesktop.CellDataChanged += new Aspose.Grid.Desktop.CellEventHandler(this.gridDesktop_CellDataChanged);

Finally, run, and use context menu in "b2" cell, select "Clear Contents" command. but "aaaaa" is still in the cell. It's right.

Are my steps like yours?