Is there a way to set the DataFormatString in server-side code? I would like to see if it is possible to set the DataFormatString of a specific cell. I see an attribute FormatFieldMethod but dont understand how to set/change it. Thanks.
You can try this code, it works for me:
protected override void OnInit(EventArgs e)
{
_datagrid.Load += (grid, arg) =>
((iggrid.WebDataGrid)grid).Columns[0].FormatFieldMethod = (field, value) =>
return "Hello";
};
}
We just started looking into this FormatFieldMethod property to see if it could help us with some other issues we're having with the WebDataGrid. Ultimately I think we've found another solution that we're going to use for our issues, but wanted to mention that in our testing with the FormatFieldMethod property we initially discovered the same issue you have, namely that it ceases to function after the initial page load.
We were able to get around this problem during our initial testing by initializing the FormatFieldMethod property in the WebDataGrid's Load event instead of in the Page's Load event. Doing this seemed to make the property work correctly even after the first page load.
We haven't taken the time to test this solution thoroughly, but maybe it will help you.
Quick solution is to use Eval(). It suffers performance issue but handy if you need it in a hurry:
<ig:TemplateDataField Key="CR_SEND_TO_URL" Header-Text="DESTINATION"> <ItemTemplate> <a href="<%# Eval("CR_SEND_TO_URL") %>" target="_blank"> <%# Eval("CR_SEND_TO_URL") %></a> </ItemTemplate> <Header Text="DESTINATION" /></ig:TemplateDataField>
The problem is if we go to the 2nd page Page_Load() is not called then we lose these formattings. Or I am missing something?
The FormatFieldMethod takes a delegate of type FormatRecordItemValue. Basically this means that you get to define a method that will be called each time a cell is processed and you will have the opportunity to format the text. Here's an example which just returns "hello" as the formatted text for a column. There's no way to tell what row-index the cell is for, so you'll have to rely on the "value" parameter, and look for the string you want to format. Alternatively, you could add a counter in the "Format" method which you could use to keep track of how many times Format was called - which should be the same as the index of the row ..