1

I'm currently working with a utility that was written using the Infragistics WebDataGrid control. The tool allows a user to select a table from a drop-down list at which point it runs a stored procedure to get data from SQL Server which it then binds to the grid.

Everything gets a default format, which for datetime columns is simply MM/DD/YYYY in this case. There is a request to include the time component for at least one of these columns.

After much searching I've tried to add code to the _InitializeRow event handler for the grid, but I can't seem to get it quite right. Here's what I've come up with:

if (e.Row.Index == 0)
{
    foreach (GridRecordItem field in e.Row.Items)
    {
        if (field.Column.Key == "InsertedOnCC")
            field.Column.FormatValue = "{0:g}";
    }
}

This gives me an error that FormatValue cannot have a value assigned because it is a method group.

This was pieced together from several sources, none of which did quite what I wanted to do. For example, one piece of sample code just always assumed that the column in question was the first column, so I had to add the foreach loop.

I've seen a lot of mention of BoundDataField and DataFormatString, but I can't seem to actually find how to access those.

NOTE: I'm actually a SQL Developer, not a C# developer, so please be gentle. :)

Thanks!

Konstantin Dinev
  • 34,219
  • 14
  • 75
  • 100
Tom H
  • 46,766
  • 14
  • 87
  • 128

2 Answers2

2

I was able to sort this out by casting the Column as a BoundDataField then using the DataFormatString from there:

        if (e.Row.Index == 0)
        {
            foreach (GridRecordItem gri in e.Row.Items)
            {
                BoundDataField field = gri.Column as BoundDataField;

                if (field.Key == "InsertedOnCC")
                    field.DataFormatString = "{0:g}";
            }
        }
Konstantin Dinev
  • 34,219
  • 14
  • 75
  • 100
Tom H
  • 46,766
  • 14
  • 87
  • 128
0

Using the info above, I was able to get this to work. It's cleaner. It does not require the loop:

TryCast(.Rows(0).Items.FindItemByKey("MyColumnName").Column, Infragistics.Web.UI.GridControls.BoundDataField).DataFormatString = "{0:G}"
Cedric
  • 672
  • 7
  • 17
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Nov 25 '21 at 22:21