Simple multi-case Switch block implementation

Hi forum.
New to Radzen here.
Wondering what the best way would be to assign a value of a grid's cell depending on another value in the same row/record? Kind of calculated column.
For example, we have country_extended_code field with values "01-123" "02-234" "02-123" "03-567" etc. And country_names output "Spain", "Germany", "Germany", "France", depending on the first two letters in the code field ("01","02","02","03").
I haven't dabbled into Invoke Custom Method yet to add a custom .cs method controller and I'm not sure if it'll be necessary for this simple task, or just Handler with Execute C# (where?) would do the trick? Some type of Template holder too.
Thanks!
Sime

Hello Sime,
If I understood your question correctly, you are trying to calculate the corresponding country depending on the first two chars for a specific code. Right?

Well I believe this should not be handled in the code as it is basically a lookup-like operation.
Please let me know what is your database engine, and I would suggest how you can handle this on Database level.

Hello there Hany - thanks for the reply!

Correct, I have a set of records (already grabbed with a lookup condition from a MySQL table), displaying read-only grid rows looking like this:

Fld1----Fld2----------Fld3
1----"01-123"----""
2----"02-234"----""
3----"02-123"----""
4----"03-567"----""
.........
.........

How can I calculate and display the value of Fld3, depending on the first left chars of the Fld2 in the same record? Fairly common thing to do in most of the languages.

Or, in a simplest form for a rookie like me, how can I assign a value to mvar in Execute C# handler code and make a textbox or label get that value? Ex. mvar="abc" and Label1 in the Page shows "abc".

Many thanks!

Alright, Thanks for the info!
I believe this should not be handled via code. instead, I suggest that you should harvest the capabilities of your database engine to make life a little bit easier.

Here is a crazy idea :slight_smile:

What if you added a simple table to your DB. That table will have only two columns

CREATE TABLE `DatabaseName`.`countrycodes` (
  `id` INT NOT NULL AUTO_INCREMENT,
  `code` VARCHAR(45) NULL,
  `country` VARCHAR(45) NULL,
  PRIMARY KEY (`id`));

In this table, you should populate all countries with their corresponding two-digit codes.

Then go to Functions and create a scalar function that accepts the code as a parameter and returns the country name from the code like this

USE `DatabaseName`;
DROP function IF EXISTS `getcountry`;

DELIMITER $$
USE `DatabaseName`$$
CREATE FUNCTION `getcountry` (countrycode VARCHAR(45))
RETURNS VARCHAR(45)
BEGIN
	Declare countryname VARCHAR(45);
	Select countryname = country from countrycodes where (code = countrycode);
	RETURN countryname;
END$$

DELIMITER ;

Now, in the main table or view/query that you use to populate the grid add this simple column definition :slight_smile:

..............
Fld3 VARCHAR(45) NULL DEFAULT getcountry (LEFT(Fld2,2))
..............

Now whenever you will change Fld2, the database will autopopulate Fld3 without the need to write a single line of code or execute any custom methods.

I hope this helps :slight_smile:

Thanks again Hany!

And that idea not far-fetched :slight_smile:
I do something similar now: the data is coming from a Foxpro DBF table into MySQL. So, on the MySQL side table, I've created an extra field CountryName (out of CountryCode), which gets calculated and populated with each INSERT/UPDATE command when the records are going from Foxpro to MySQL cloud. Very common "Do Case ... Endcase" block in Visual Foxpro. While it's working without a hiccup, it's still an extra field that needs to be maintained on both of those back-end sides.
Your approach sounds better, as the quick work is done at the MySQL intermediate point, between VFP and Radzen.
I was still hoping there is a quick and dirty solution right in the Radzen's Execute C# for this, without any additional travels to the server.

I appreciate your suggestions and your effort on helping me in this regard.

Sime

Well, Try watching this

Hany,
Talk about not seeing the forest for the trees!
The price I pay for not going through the documentation and the instructional video.
Thanks for the link and guidance. I zipped through it quickly but will go again and implement it.
Cheers and have a great rest of the weekend!
Sime