# Using custom code in datagrid component

**URL:** <https://forum.radzen.com/t/using-custom-code-in-datagrid-component/10473>\
**Category:** Angular\
**Created:** [March 29, 2022, 4:12pm UTC](https://forum.radzen.com/t/using-custom-code-in-datagrid-component/10473 "2022-03-29T16:12:37Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![rbock](https://forum.radzen.com/letter_avatar_proxy/v4/letter/r/2acd7d/32.png) [@rbock](https://forum.radzen.com/u/rbock)\
**Post date:** [March 29, 2022, 4:12pm UTC](https://forum.radzen.com/t/using-custom-code-in-datagrid-component/10473/1 "2022-03-29T16:12:37Z")

</div>

Hi,

I need to manipulate a column before it is displayed in a datagrid. The column is numeric and I need to convert it to text and pad the beginning with spaces or an underscore to simulate an indentation scheme. I was able to create custom code that worked fairly well for the indentation but need to apply it to a column in the datagrid and cannot figure out how to do that.

Here is my custom code:

```auto
        public IActionResult FormatNum(double x)
        {
            string inString = x.ToString();
            int len = inString.Length;
            string formattedNum = inString.PadLeft(len*2,'_');
            return Json(new { formattedNum });
        }

```

This may not be exactly what I am looking for but something close to this anyway. I tried to access the custom code from the column's template but it does not seem like that is possible. Is there any way to accomplish this?

---

<div class="post-metadata">

**Author:** ![enchev](https://forum.radzen.com/user_avatar/forum.radzen.com/enchev/32/16_2.png) [@enchev](https://forum.radzen.com/u/enchev)\
**Post date:** [March 29, 2022, 4:29pm UTC](https://forum.radzen.com/t/using-custom-code-in-datagrid-component/10473/2 "2022-03-29T16:29:58Z")

</div>

You can define template for desired column:

> [@Datagrid template](http://forum.radzen.com/t/datagrid-template/3192):
>
> Hi, I have a database table with records which have a field (NoteDisplayColour) which contains a Hex colour code. This is shown in the DataGrid component. Is it possible to change the colour (background or text) of the column showing the colour code to actually display using the colour in the Hex field? I've tried using this template but, it doesn't seem to like using expressions within inline styles: \<span style="color: ${data.NoteDisplayColour};"\> ${data.NoteDisplayColour} \</span\>

Check this thread for more info about the same in JavaScript:

> <https://stackoverflow.com/questions/2686855/is-there-a-javascript-function-that-can-pad-a-string-to-get-to-a-determined-leng>

---

<div class="post-metadata">

**Author:** ![rbock](https://forum.radzen.com/letter_avatar_proxy/v4/letter/r/2acd7d/32.png) [@rbock](https://forum.radzen.com/u/rbock)\
**Post date:** [March 29, 2022, 5:19pm UTC](https://forum.radzen.com/t/using-custom-code-in-datagrid-component/10473/3 "2022-03-29T17:19:04Z")

</div>

Enchev,

Thanks for the reply. I did manage to get something working but for some reason the datagrid column automatically removes leading spaces. If I pad the beginning with "\_", I can see that my logic is working but spaces seem to be getting trimmed automatically. Is there a way to prevent this?

---

<div class="post-metadata">

**Author:** ![rbock](https://forum.radzen.com/letter_avatar_proxy/v4/letter/r/2acd7d/32.png) [@rbock](https://forum.radzen.com/u/rbock)\
**Post date:** [March 30, 2022, 9:15pm UTC](https://forum.radzen.com/t/using-custom-code-in-datagrid-component/10473/4 "2022-03-30T21:15:25Z")

</div>

Nevermind - got it down using the `<pre>` tags in the template.
