datatable.columnchange event

  • Thread starter Thread starter Ashish Sheth
  • Start date Start date
A

Ashish Sheth

Hi Gurus,
I have implemented a ColumnChange event handler for my Datatable. When one
of the column is modified by the user, in the columnchange event handler, I
am setting values for other columns of the same row of the same datatable.
In this case i don't want the columnchange event to be fired for these other
columns. But when user directly modifies the column values than I want the
columnchange event to be fired How can I achieve this?
Please reply ASAP.
thanks and regards,

Ashish Sheth
 
U¿ytkownik "Ashish Sheth said:
Hi Gurus,
I have implemented a ColumnChange event handler for my Datatable. When one
of the column is modified by the user, in the columnchange event handler,
I
am setting values for other columns of the same row of the same datatable.
In this case i don't want the columnchange event to be fired for these
other
columns. But when user directly modifies the column values than I want the
columnchange event to be fired How can I achieve this?


private void table_ColumnChanging(object sender, DataColumnChangeEventArgs
e)
{
if(e.Column.ColumnName != "MyColumnThatCanFireTheEvent") return;
e.Row["OtherColumn"] = "User changed id!";
}

I hope that it helps.
Regards,
Grzegorz
 
Thanks Grzegorz. But this is not what i want. I want a functionality
something like this:
I have 4 textboxes on my form which will accept some integer amount from the
user. Initially these four textboxes will be populated from database values
and after that whatever amount user is entering will be saved in the
database. We are using databinding for this purpose and all the business
processing logic we are writing in the column change event. All the 4
textboxes are binded to 4 different column in the same datatable.

In some situation only first textbox will be enabled and other three will be
disabled. In this case whatever amount user is entering in the first textbox
will be distributed to other three textboxes equally. We are doing this in
column change event.

In some other situaltion the first textbox will be disabled and the other
three will be enabled... and whatever amount is entered by the user in this
three textbox... sum of these three values will be displayed by the first
textbox...

so when the first text box is enabled.. column change event for the other
three textboxes should not fire... and when the first textbox is disabled
column change event for it should not be fired. How can I achieve this?

thanks and regards,
Ashish
Grzegorz Danowski said:
U¿ytkownik "Ashish Sheth said:
Hi Gurus,
I have implemented a ColumnChange event handler for my Datatable. When one
of the column is modified by the user, in the columnchange event handler,
I
am setting values for other columns of the same row of the same datatable.
In this case i don't want the columnchange event to be fired for these
other
columns. But when user directly modifies the column values than I want the
columnchange event to be fired How can I achieve this?


private void table_ColumnChanging(object sender, DataColumnChangeEventArgs
e)
{
if(e.Column.ColumnName != "MyColumnThatCanFireTheEvent") return;
e.Row["OtherColumn"] = "User changed id!";
}

I hope that it helps.
Regards,
Grzegorz
 
U¿ytkownik "Ashish Sheth said:
Thanks Grzegorz. But this is not what i want. I want a functionality
something like this:
I have 4 textboxes on my form which will accept some integer amount from
the
user. Initially these four textboxes will be populated from database
values
and after that whatever amount user is entering will be saved in the
database. We are using databinding for this purpose and all the business
processing logic we are writing in the column change event. All the 4
textboxes are binded to 4 different column in the same datatable.

In some situation only first textbox will be enabled and other three will
be
disabled. In this case whatever amount user is entering in the first
textbox
will be distributed to other three textboxes equally. We are doing this in
column change event.

In some other situaltion the first textbox will be disabled and the other
three will be enabled... and whatever amount is entered by the user in
this
three textbox... sum of these three values will be displayed by the first
textbox...

so when the first text box is enabled.. column change event for the other
three textboxes should not fire... and when the first textbox is disabled
column change event for it should not be fired. How can I achieve this?


Why don't you use TextBox Leave event?


this.txtA.Leave += new System.EventHandler(this.txtABC_Leave);
this.txtB.Leave += new System.EventHandler(this.txtABC_Leave);
this.txtC.Leave += new System.EventHandler(this.txtABC_Leave);
this.txtSum.Leave += new System.EventHandler(this.txtSum_Leave);


private void txtSum_Leave(object sender, System.EventArgs e)
{
this.txtA.Text = Convert.ToInt32(this.txtSum.Text) -
Convert.ToInt32(this.txtB.Text) -
Convert.ToInt32(this.txtC.Text)).ToString();
//etc.

private void txtABC_Leave(object sender, System.EventArgs e)
{
TextBox t = (TextBox)sender;

this.txtSum.Text = (Convert.ToInt32(this.txtA.Text) +
Convert.ToInt32(this.txtB.Text) +
Convert.ToInt32(this.txtC.Text)).ToString();
//etc.
}

Regards,
Grzegorz
 
we r not doing this in textbox change or leave event becuase we r not
exactly distributing the amount but we have to perform some business logic
based on some other database value before distributing and displaying the
amount..
any way.. for now what we are doing in the column change event itself we are
unregistering the column change event first.. then performing our
modifications to other columns.. so that column change event will not be
fired for that columns..
and after doing the modifications we are again reregistering the column
change event....

something like this..

private void OnColumnChange(object sender, DataColumnChangeEventArg e)
{
switch(e.columnName)
{
case "PremierAmount":
this.CostingDataTable.ColumnChange -= new
DataColumnChangeEventHandler(OnColumnChanged) ;
//modify other columns

this.CostingDataTable.ColumnChange += new
DataColumnChangeEventHandler(OnColumnChanged) ;
break ;
case "OtherAmountA":
this.CostingDataTable.ColumnChange -= new
DataColumnChangeEventHandler(OnColumnChanged) ;
//modify PremierAmount
this.CostingDataTable.ColumnChange += new
DataColumnChangeEventHandler(OnColumnChanged) ;
break ;
}
}

but i don't know whether this is a good idea or not... but this is working
fine in the way we want

thanks and regards,
Ashish
 
U¿ytkownik "Ashish Sheth said:
we r not doing this in textbox change or leave event becuase we r not
exactly distributing the amount but we have to perform some business logic
based on some other database value before distributing and displaying the
amount..
any way.. for now what we are doing in the column change event itself we
are
unregistering the column change event first.. then performing our
modifications to other columns.. so that column change event will not be
fired for that columns..
and after doing the modifications we are again reregistering the column
change event....

something like this..

private void OnColumnChange(object sender, DataColumnChangeEventArg e)
{
switch(e.columnName)
{
case "PremierAmount":
this.CostingDataTable.ColumnChange -= new
DataColumnChangeEventHandler(OnColumnChanged) ;
//modify other columns

(...)

Ok, there is my suggestion to simplify it:

private bool isPerformChng = false
private void OnColumnChange(object sender, DataColumnChangeEventArg e)
{
if(isPerformChng) return;
//"disable" events
isPerformChng = true;

//your job

//"enable" events
isPerformChng = false;
}

Regards,
Grzegorz
 
Back
Top