I'm having issues accessing a named cell in Excel from C# using Excel Interop when the cell is hidden and I access it like so:
bool SomeBoolean = WorkSheet.Range["MyRangeName"].Value;
The Range property implicitly calls get_range when I'm accessing it, which is what's throwing the exception according to the stack trace. The code used to call get_range directly, and that had similar results.
If I unhide the cells, it works fine. If I step through the code line by line, it works fine (this is always a frustrating situation).
Of course I can solve this by unhiding the cells, but I'd like to try to find a more elegant solution. I've pored over the MSDN documentation and several threads here on SO, but I can't seem to find anything. Any help would be much appreciated!
MSDN page for Worksheet.Range: https://msdn.microsoft.com/en-us/library/office/ff836512.aspx
This is by design. By definition, hidden cells are not accessible.
Try it yourself. Create a worksheet in Excel, hide some columns, and then search (By Values, not Formulas) for a value in a hidden cell. You will get a dialog box indicating that the value could not be found.
If you don't agree with this behavior, then take it up with Microsoft.
Thanks everyone for trying to help me solve this mystery. During my debugging, I noticed that sometimes it worked, mysteriously, and other times not, and I went over the code leading up to the line in question.
The cause of this issue was that the Range is being searched from a worksheet which is set as the currently-selected worksheet at the time the plugin is called. So if the correct tab in the workbook isn't selected at call time, it fails. If it is selected, it succeeds. As it turns out it's nothing to do with the cells being hidden. Rather frustrating, but at least now we know!
Related
I've a workbook object in which I'm using excel formulas in certain cells.
I'm programmtically setting the value for ProtectContents on IWorksheet object to turn the readonly mode on those cells on and off.
1.When the value of ProtectContents is set to true, I see that all the cells are in read-only mode as expected but the cell (which use excel formula) value show up as #NAME?.Is this how it works in Spreadsheetgear?
2.And when the ProtectContents is set back to false,I see all those cells which use formula still show value as as #NAME? but they are editable as expected.But wondering why are those cells values not getting evaluated??
Please advise.
Thanks for your help.
There should not be any correlation between #NAME? errors and the state of worksheet protection. #NAME? errors can occur for a number of reasons but without more details on your particular case it's impossible to say why you are seeing this.
One common reason you might see this in SpreadsheetGear (and I presume not in Excel itself?) is because the formulas in these problematic cells either directly or indirectly depend on a User Defined Function (UDF) in the VBA portion of your workbook, from an external XLL add-in, etc. SpreadsheetGear does not support executing UDFs via VBA or XLLs, so such functions would evaluate to #NAME?.
SpreadsheetGear does support implementing your own Custom Functions in your .NET application via the SpreadsheetGear.CustomFunctions.Function class and its Evaluate(...) method. If you discover the above being the cause, you could implement your own Function to make these cells correctly calculate. I'd provide sample code but I'm going out on a limb in the first place mentioning this as being the underlying cause. Update your question with more specifics and I can update mine accordingly if needed.
I have looked at the various levels(application, workbook, and worksheet) of the Excel.Interop libraries to find out how to detect a user sorting data. I have created a class that instantiates an application, workbook, and worksheet. The program has data piped in from another application and then adds it to a worksheet. I am using .NET 4.5, C# compiler version 4.0, and have been testing with Excel 2013 if that makes a difference.
I can detect cell changes and calculation events but neither of those events fire when a column is sorted. I believed that it should work after reading this article.
After having that fail for me, I looked into the table update event but I had no luck. There error I received was along the lines of, "the handler does not exist in the Microsoft.Office.Interop.Excel namespace, are you missing an assembly. I don't think this is the right direction I need to go but I was attempting to see if it would suit my needs.
Using any other method besides the Excel Interop method might set me back a few weeks.
Is there something I am missing in regards to the table update event handler or perhaps the sort event itself?
I keep having the columns from some of my DataGridViews disappearing randomly in the designer, and of course then nothing shows up when I run the application either. Currently I can't even set the datasource to None and then back to my binding source (which has always worked in the past), so I guess I need to create a new DataGridView and rebind the datasource to it. The problem is I add several listeners, set column widths, etc. and I can't keep track of everything I do to every table (and I shouldn't have to!). And I am tired of redoing the same thing over and over again.
I have done a little searching and found that others have this problem and it may be a bug in Visual Studio (I am using Visual Studio Express 2013). So my question: Am I doing something to cause this and can I prevent it from happening again?
Edit
I have determined the cause, but still not the solution. It happens when I change the DataTable behind the DataGridView by, for instance, adding or removing a column in the DataTable, and then add or remove that column to the DataGridView. Once I do that, all of the columns disappear from the Designer, including any listener code, etc. I added. I would really appreciate some help on this since it happens to me all the time since I am working on a project with changing needs and it is impossible to have the DataTables be static at this point. And it is really hard to remember the column widths, what listeners/tooltips/etc. I added to which columns, etc. This totally seems like it is a bug in the Designer.
I have recently faced the a similar problem with a Datagridview bound to a DataSource. When I was trying to add a new column to the Datagridview, all columns would disappear on pressing OK button in the column editor window.
Cause:
In my case it turned out that the new column I was trying to add was named 'override' in the database which is a reserved word in C#. Designer was trying to add the column with its existing name which of course was messing things up.
Solution:
Just change the Name property of the column in the column editor window before saving changes to your Datagridview. No need to change anything in your database or anywhere else.
I also faced similar issue. Make the changes in designer.cs file.
You will find all your columns in the designer.cs file. You can still add new columns using add column.
I just had this problem with my project.
Problem Description: I was adding a new databound column to my DataGrid. After pressing OK in the Edit Columns window, all columns disappeared.
Solution: Sadly you will loss your DataGrid columns configuration. Save the project and Reload. Columns will appear again (if not, set the DataSource again).
Side Effects: The worst part of this bug is that all components that were previously associated to your DataGrid will still exist in the project and I was not able to delete them using the designer or the Solution Explorer.
Project Cleanup: I had to clean up my project following this procedure:
Go to the DataGridColumn in Solution Explorer. It will show up in your project in the .Designer.cs file.
Delete or comment the line of the file.
Errors will appear in your error list.
Delete or comment the lines reported in the errors (located on InitializeComponent() method).
Caveat: I did the recommended changes and had no problems with my project. However always make a backup of your project before doing manual corrections like the ones I described.
Good luck and happy coding.
May be its a bug. But it worked for me to add column from the Add Column option as shown in pic and not from Edit Columns option.
I'm using VS 2013 and facing the same issue. I open the design form, edit the datagridview's column and then press OK but after that all column in my datagridview is gone.
I fix this by renaming all the column started with _ (underscore) and then press OK and everything just worked fine. Maybe it's a VS bug or my column name contains a reserved word.
take a look at my screenshot
I had the same problem with a column whose name ended with ?. Using an alias without the ? solved the problem. incidentally if, after the columns disappear, you close and open the form again in the IDE, you find that the columns are still there but all your laboriously applied formatting has disappeared. Thank heavens for source code control.
I had the same problem in VS 2015 with my columns disappearing from dataGridView. The cause, in my case, turned out to be that I had a column with the same name as a string variable from Form1. The solution for me was to rename the string variable and all of its references. After that, I was able to edit the "Columns" property without any columns disappearing. Hope this is helpful anyone still facing this issue.
Use Add Column instead of Edit Columns.
enter image description here
I have a crystal reports rpt file being called from C# code. Report works as expected from Designer within Studio 2010. Same report ignores Selection Criteria when called from C#, but ONLY when including a certain table's field in the criteria.
Current Criteria Example :
date({transaction.transaction_date}) in {?FromTo} and
{user_branch.branch_id} = 1
{?FromTo} is a date range parameter which works perfectly
When I remove the branch_id section, the {?FromTo} date parameter is checked correctly and the data returns as expected. BUT, as soon as I include the branch_id field, ALL the data is returned, which does not make sense at all.
Running this exact same report via the designer, works correctly... The problem occurs when running via C# SDK code.
Any help with this will be GREATLY appreciated
Already Tried
Recreate Entire Report from Scratch bit by bit, table by table. This works up to a point where I start adding more fields to the report itself, no one in particular, then the problem starts up again... So obviously, I start back tracing my steps until the entire report is empty again, but the problem persists, like the file is now "broken"
No similar issues in google... No where in my code is a line emptying the selection criteria during code... My other reports not accessing that table works fine.
Currently using version 13.0.1 of the CR Runtime. Tried to upgrade to 13.0.2, still didn't work
Ran the exact same SQL that the report generated directly against the database. That works as expected.
Try changing your criteria to:
(date({transaction.transaction_date}) in {?FromTo}) and
{user_branch.branch_id} = 1
Adding the date({transaction.transaction_date}) in {?FromTo} line inside a (). I'm not just sure.
After many head-aches this week, I finally found the problem... This looks like a bug in the SDK itself though, but to solve the problem, just make sure that the field giving the problem mentioned above is contained in the detail section of the report.
i.e. I added my branch_id field to the detail section of the report and suppressed it. It works by suppressing the section as well, just as long as the field is contained there.
I tested this on all my other reports with the same problem and it worked.
I am currently opening a word 2003 .doc file and using the Office Interop to get information out of it. For some reason the program will fail reading information from the header if it is not visible.
Has anybody checked to see if the header was visible through this before? How would I go about doing it?
From other posts I've found some documentation for Word 2007 (not the version I'm using) but I can't find 2003 documentation. I couldn't find any helpful information in the 2007 documentation but from what I found it may have something to do with the header/footers and a style (no idea how to apply a "style" to them or even check the style.
You can read info from a header if it's not visible, but often, if a header or footer isn't visible, it's because it doesn't exist. Word has 3 headers/footers, Normal, First page, and even (or is it odd) pages. But they don't actually have to exist. And Note that each SECTION can have its own version of all three of those objects, OR any of those objects in the section can "Link to previous", in which case, the section itself doesn't have the object, but rather a pointer to the same object in the previous section. Confusing? Oh yeah!
Check that with the EXISTS property on the headerfooter object. If it's false, don't bother with the header/footer cause it's not there.
Beware, though, that in some cases, simply checking the header footer will actually +create+ it, if it wasn't there before. and creating a header footer tends to drop a paragraph into it, which, depending on how the document is formatting, might affect the layout of the document slightly (adding a little extra space). If the doc was tightly formatted to the page, you could end up creating a second page inadvertantly.
Just something to be aware of.