hubFS: THE place for F#

. . . are you on The Hub?
Welcome to hubFS: THE place for F# Sign in | Join | Help
in Search

DataGridViewRowCollection.Insert() and arrays

Last post 02-16-2010, 20:56 by gcarver. 3 replies.
Sort Posts: Previous Next
  •  02-04-2010, 21:40 13021

    DataGridViewRowCollection.Insert() and arrays

    In an attempt to learn F# I'm porting some code from IronPython (which was ported from C#).

    DataGridViewRowCollection.Insert(rowIndex: int, values: obj[]) method takes an array of System.Strings for both my IronPython and C# implementations. However the below code only inserts the text "System.String[]" into the 1st column instead of the expected string in each of my 2 columns.

    let GridView = new DataGridView()
    let newData = [| "Column1"; "Column2" |]
    GridView.Rows.Insert(0, newData)

    This is obviously the result of an obj[].ToString() call. I've tried and ArrayList and Generic.List as well as some others all with the result of ToString() being placed into the 1st column.

    I read in another post about someone else having a similar problem though the comment was not very specific as to the issue.

    I'm stumped (or stupid if you will). I hope someone has figured this out but I'm beginning to suspect it's bug in the F#->.Net interface even with my limited knowledge.
  •  02-04-2010, 22:19 13022 in reply to 13021

    Re: DataGridViewRowCollection.Insert() and arrays

    Well ok, I am stupid. The values: obj[] type for the 2nd parameter of Insert() is actually a parameter array which in F# must be passed to the function individually. So the insert function actually looks like this.

    GridView.Rows.Insert(0, "Column1", "Column2")
  •  02-04-2010, 22:20 13023 in reply to 13021

    Re: DataGridViewRowCollection.Insert() and arrays

    I believe that you need to pass an object array, not a string array (so just manually box each item in the array, or pass newData |> Array.map box as the second parameter).  Normally, you'd get a compiler error here; however, since the last argument to the method is actually a params array, the compiler is treating the whole array as an object which is being implicitly packaged up into a new object [] and passed to the method.

    EDIT - Or, even better, use your approach and pass each string as a separate argument.

    Keith

  •  02-16-2010, 20:56 13139 in reply to 13023

    Re: DataGridViewRowCollection.Insert() and arrays

    An update on this just in case anyone cares. This works. Manually boxing the array values also works. Thanks Keith

    self.GridView.Rows.Insert(irow, (newData |> Array.map box))
View as RSS news feed in XML
Powered by Community Server, by Telligent Systems