Sorry, .NET is new to me: I had assumed that when automating Excel you could
use syntax similar to VB[A] but it appears that's not the case.
Tim
"JonOfAllTrades" <(E-Mail Removed)> wrote in message
news:5B475693-1945-4266-9E6A-(E-Mail Removed)...
> "Tim Williams" wrote:
>
>> I'm not familiar with c# or .NET in general, but there must be a way to
>> access the values used to populate your datagrid.
>> Google groups had some promising stuff.
>>
>> In VBA you can populate an entrire range from an array like this:
>>
>> '************
>> Sub Tester()
>> Dim x, y
>> Dim arr(1 To 10, 1 To 10) as Integer
>>
>>
>> For x = 1 To 10
>> For y = 1 To 10
>> arr(x, y) = ((x - 1) * 10) + y
>> Next y
>> Next x
>>
>> ActiveSheet.Range("A1").Resize(10, 10).Value = arr
>>
>> End Sub
>> '************
>>
>> ActiveSheet.Range("A1").Resize(10, 10).Value = arr
>> is equivalent to
>> ActiveSheet.Cells(1,1).Resize(10, 10).Value = arr
>>
>> or
>>
>> With ActiveSheet
>> .Range(.Cells(1,1), .Cells(10,10)).Value = arr
>> End With
>>
>> a single row is referred to as
>>
>> ActiveSheet.Rows(1)
>>
>> Tim
>
> Now THAT would be convenient! Doing this in C# is not nearly that
> straightforward. There's no Resize() function in C#, and the Rows()
> function
> doesn't work that way. There's a get_Range(Cell1, Cell2) function, but it
> doesn't take Cell objects as parameters! I guess COM doesn't allow for
> method overloading, though since everything's passed as objects Excel
> should
> be able to interrogate the parameters and interpret them correctly as
> strings
> or Cells. I got this much to work:
>
> string topLeft = "A1";
> string bottomRight = char.ConvertFromUtf32(65 + numColumns - 1) +
> numRows.ToString(); // 65 is ASCII for 'A', didncha know?
> Range range = worksheet.get_Range(topLeft, bottomRight);
>
> Later I found the get_Resize() function; looks like this is a known
> workaround, KB 824004.
> Building the array is much simpler, but my code is still not right to
> actually push the data into Excel. KB 302096 uses
> range.set_Value(Missing.Value, array), where array is an array of longs or
> strings. The MS example uses [row][column], but I think that's wrong,
> other
> sources indicate it's [column][row]. Trying to use set_Value, I keep
> getting
> error "Specified array was not of the expected type."
> I've tried object arrays and string arrays. I've tried specifying
> RangeValueDataType, and I've tried using Type.Missing or
> System.Reflection.Missing.Value. I've tried flipping the axis of my
> array.
> I think I've tried every combination of these settings.
> Clearly I'm missing something. Any ideas? If it can be done in
> VB.NET, it can be done in C#, I'm sure.
|