|
To edit the file from code, you can use NPOI[^], or EPPlus[^] for Excel 2007/2010/2013 files. Both libraries are free and open-source.
However, neither library seems to support printing the file. You might need to look for a commercial library to do that.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
|
|
Hello Friends,
Thanks in Advance... I am using VS2012 ON WIN7 as my development tool.
I want to get data from C++ into my C# 2D arrays, Arrays are member of strucutre.
I am getting data fine in case of Single dimension arrays and basic data types.
Below is the sample code:-
I have a C# strcuture having 3-4, multidimensional array example.
[StructLayout(LayoutKind.Sequential)]
public struct CompMultiArray
{
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 2 * 200)]
public int [,] tempVacData;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 2 * 200)]
public int [,] tempFuncData;
};
Public void GetDataFromUnmanagedCode()
{
CompMultiArray cmpGetData = new CompMultiArray();
cmpGetData .tempVacData = new int[2, 200]; // Allocating or not allocating has no effect
cmpGetData .tempFuncData= new int[2, 200]; // Allocating or not allocating has no effect
CPPClass.FillData(ref cmpGetData);
}
C++ side
struct CompMultiArray
{
int tempVacData[2][200];
int tempFuncData[2][200];
};
void FillData(CompMultiArray& cmData)
{
for(int i =0; i< 2; ++i)
{
for(int j = 0; j < 200; ++j)
{
cmData.tempVacData[i][j] = 100;
cmData.tempFuncData[i][j] = 200;
}
}
}
If I see data in C# side after getting from unmanaged code in Windows->Memory (VS2012), I can see correct data in memory. GC Code to get memory address
GCHandle gch = GCHandle.Alloc(cmpGetData.tempVacData, GCHandleType.Pinned);
// Get a pointer to the array data...
IntPtr pArrayData = gch.AddrOfPinnedObject();
// Via the debugger's memory viewer, confirm
// that the data is laid in memory in sequence.
gch.Free();
But using indexing, i cannot get any data.
for(int i = 0; i < 2; ++i)
{
for(int j = 0; j < 200; ++j)
{
cmpGetData.tempVacData[i,j];
cmpGetData.tempFuncData[i,j]
}
}
please ignore, Dllimport etc. all things are working fine.
please guide me.
Thanks,
Subhash
|
|
|
|
|
This may not be possible in your case, but since you have provided the C++ code, have you tried compiling the C++ part as C++/CLI and using managed arrays in the C++ part? You could then make the C++ part a DLL that is referenced in your C# project.
I do this all the time in order to get over the difficult unmanaged/managed boundary.
|
|
|
|
|
Class a
{
a obj=new a();
}
in this case we are calling the default constructor
but
class a
{
public a(int i)
{
console.writeline(i);
}
a obj=new a(); //Gives error during compilation
}
In this case we can't call default constructor after creating a custom constructor.
But why it happns???
Why we cant use default constructor in this case
|
|
|
|
|
Because it doesn't have one.
|
|
|
|
|
means?, can u describe it
|
|
|
|
|
In your first version of class a, you haven't specified a constructor. This means that a default constructor is created for you. As soon as you add a constructor, you lose the default constructor. This is exactly what should happen because you can't instantiate a class that doesn't have a constructor, and you don't want the compiler to give someone the ability to use a default constructor if your class relies on values that are passed in via the constructor.
|
|
|
|
|
thnks for this answer i appreciate it
|
|
|
|
|
Classes only have the constructors you create - if you don't create any at all then a parameterless constructor is created for you.
Structs, being value types, are different and always have a default parameterless constructor with all the fields initialized to their default values (normally zero, or null if the field is a reference type).
|
|
|
|
|
Because the language specification says so.
17.10.4 Default constructors
If a class contains no instance constructor declarations, a default instance constructor is automatically
provided. That default constructor simply invokes the parameterless constructor of the direct base class.
Really it should be have said "if and only if", because that's what they mean. If you don't define a ctor, the default one exists. If you do define a ctor, the default one goes away. You can get it back if you want, because it's trivial:
public TypeName() { } (it would be protected for abstract classes)
|
|
|
|
|
Would like to ask what are the possible causes for columns of cells in a datagridview not to show values being pushed into it?
I have a datagridview with about 40+ columns and for some strange reason a number of columns are able to accept values I am pushing into them using:
datagridview.rows[rowindexhere].cells[columnindexhere].value = valueToPushHere;
whereas a number of others wont accept it? This is really weird and not sure if there a re some undocumented things or limitations about Datagridview control that new users like me need to be aware of to ensure successful push of info into the control 100% of the time?
Thanks so much for all the help you could give ... I really appreciate it.
Matt
|
|
|
|
|
While banging my head trying to find an answer :^) ... below is what I did to test it further and gain more insight .. I changed the values as described below with result also indicated:
Change 1: I changed all the values pushed to the 48 columns to be the string "Writing here"
Result : About six columns responded and reflected the change
Change 2: I then proceeded to change the value from "Writing here" to the number 68
Result : 30+ columns have responded and reflected the new value 68
This is good and indicates that the type of data pushed have an influence on whether the value actually gets to land in the grid or not.
Unfortunately, the particular column whose cells I am interested in altering remains unaffected so there seems something odd about it ...
All the columns get populated on the fly via a stored procedure that is mapped to a dataadapter that is created inside a .xsd file.
The one odd thing about the particular column I want to affect via code is that it is not actually a column existing in a table instead it is created via with a little t-sql
manipulation in the select statement of the stored procedure alongside the other normal columns similar to the one shown
Select NormalColumnActuallyExisting1,
NormalColumnActuallyExisting2,
NormalColumnActuallyExisting3,
ColumnCreatedOnFly = Case When NormalColumnActuallyExisting1 = 1 then ...
When NormalColumnActuallyExisting2 = 1 then ...
When NormalColumnActuallyExisting3 = 1 then ...
Else ...
End
NormalColumnActuallyExisting3,
...
The odd (ColumnCreatedOnFly) gets populated with a hardcoded string based on the bit values of NormalColumnActuallyExisting1, NormalColumnActuallyExisting2 and NormalColumnActuallyExisting3 columns so it really puzzles me why it wont accept the string in the Change 1 I have described above...
So I did the changes below:
Change 3: I then proceeded to change the value from 68 to the boolean true
Result : The odd column whose value I want to change still remains unaffected
Change 4: I then proceeded to change the value from true to the integer 0 (for bit)
Result : The odd column whose value I want to change still remains unaffected
All I want is to be able to push a string in that said odd column but finding the grid not cooperating with me right now ... so is there a way for me to make it happen ... thanks again for all the help I really appreciate it.
|
|
|
|
|
It looks like somekind of limitation in the DataGridView control which prohibits use of on the fly created virtual (non-existing) column ...
As I learn more of DataGridView I will discover more what can and can't be done with it. ... thanks again
-- case solved --
|
|
|
|
|
It appears to be somekind of limitation in the DataGridView control which prohibits use of on the fly created virtual (non-existing) column ...
For now this works for me and maybe as I learn more of DataGridView I will discover more what can and can't be done with it. ... thanks again
-- case solved -
|
|
|
|
|
I'm working on an app that has custom exceptions defined. When the DAL wants to communicate that something is wrong, not necessarily an error, then it throws a custom exception. The theory here is that the UI can catch them and know what's wrong.
Examples might be a purchase amount exceeding a credit limit. The custom exception AmountExceedsCreditLimitException is thrown, with the message and amount attached to to it.
Then the UI now knows about this and can deal with it.
Now I know some who say that Exceptions should only be used for true errors.
I'd like to get your opinion.
If it's not broken, fix it until it is
|
|
|
|
|
I guess there's always people who are going to be polarised one way or another - yet I think the major crime is throwing exceptions, catching them, and chucking them away.
Define 'true error' anyway - surely that's also in the context of the software
At least in your case, the exceptions mean something, and are being caught and handled
|
|
|
|
|
Throwing Exception("<blah>") isn't too useful because you can't code for those exceptions (i.e. e is CustomExceptionType). Custom exceptions are better, but you'll find that you'll end up writing THOUSANDS of these AmountExceedsCreditLimitException type classes.
IMHO, your example of AmountExceedsCreditLimitException should not be an exception, but rather a bool return from a method.
Goes back to my first point, once you start doing custom exceptions, its easy to get overzealous and make everything exceptions.
|
|
|
|
|
SledgeHammer01 wrote: Goes back to my first point, once you start doing custom exceptions, its easy to get overzealous and make everything exception
That seems like what's going on here.
The problem is that if you want to return some kind of message to the UI, then the message has to be defined in the DAL or BL, rather than in the UI where it belongs. Using the custom exception tells the UI "Hey, this condition occurred. You decide how to tell the user", rather than "Hey, this condition occurred and here's what to tell the user".
If it's not broken, fix it until it is
|
|
|
|
|
Is the user really going to be able to do anything about the error? Most of the time probably not if they are not technical. The .Net framework throws custom exceptions so you might want to be consistent with that. Personally I try to show the user as few errors as possible because they'll just think the software is buggy. Better to try to handle common errors yourself silently and log the ones you can't.
|
|
|
|
|
I think this is a great question to throw ( ) out in the community and I am interested in seeing the responses. I am in favor of throwing exceptions on errors and when the code encounters an unexpected situation. Vague, I know. I would not want to make a more detailed statement because it depends on what is considered unexpected in any given function, library or application.
In an application handling charges to credit cards, I do not think it is unexpected that an amount can exceed the available credit limit, so I would be against categorizing that as an exception. It seems to me that if you go down that road, you end up defining the program behavior through the use of exceptions.
Here is a link to what I will normally follow.
http://msdn.microsoft.com/en-us/library/ms173163.aspx[^]
Soren Madsen
"When you don't know what you're doing it's best to do it quickly" - Jase #DuckDynasty
|
|
|
|
|
That sounds OK to me, but that still doesn't seem enough like an Exception, and a boolean wouldn't give enough information, a good old enumerated value might be good.
public enum TransactionStatus
{
Pending
,
Applied
,
Rejected
,
NoSuchAccount
,
InsufficientFunds
...
}
|
|
|
|
|
{ true, false, EFileNoteFound } ?
Needless to say, I'd recommend using the exceptions; yes, there's a performance-penalty, but it's hardly noticeable. Unless it's in a tight loop, you won't notice it.
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
The problem with this is that, one, Exceptions are a little expensive to throw, so if you end up throwing a lot of them, well, it adds up.
Two, as the name suggests, is what you're finding "exceptional"? No, it's not. What you're doing is making a determination of an account status as part of a normal business process. You're chosing two different paths to return the two possible states of an account. THe account can either have sufficient credit, or not.
What you should be doing is not relying on exceptions to determine the state of a business object. The business object should be telling you its state. If you have to wrap or include a state object in your transaction object, fine.
Exceptions should be used to transmit information outside of your normal business process, such as if a transaction fails to commit because of a database failure.
|
|
|
|