Sign in to follow this  
shrek2

[.net] c# how to make my datagridview work like Excel?

Recommended Posts

Is there a way to get my datagridview to work like Excel? For example, then the user types =1+1 into any of the cells, then then cell will return 2. Thanks.

Share this post


Link to post
Share on other sites
I'm sure there is a "CellTextChanged" or similar event that you can catch. Check if the value begins with a "=", then you parse the expression and calculate the result.

There is no "WorkLikeExcel" property though :).

Share this post


Link to post
Share on other sites
Quote:
Original post by shrek2
hmm can you please explain how that works in more detail? can it do cell referencing and if statements?

[google]

Share this post


Link to post
Share on other sites
How much like excel does it need to be, the syntax is a bit different but Python is a complete language that is more than enough for basic in app scripting.

Share this post


Link to post
Share on other sites
Quote:
Original post by Kaze
How much like excel does it need to be, the syntax is a bit different but Python is a complete language that is more than enough for basic in app scripting.


basic arithmetic, if statements, and cell referencing.

For example,

=1+1
=if(c2=c3,4,0)

can u give me an idea on how to implement this? i guess i don't understand what needs to be done her if i use python. thanks.

Share this post


Link to post
Share on other sites
Unless you take ChaosEngine's solution, then there is nothing (that I am aware of) that lets you do it without having to parse and evaulate the cells yourself.

Why do you think Microsoft charge so much for Excel/Office if it could all be built using off-the-shelf components for free anyway?

Share this post


Link to post
Share on other sites
Actually there are a number of spreadsheet components available for use with .NET, a number of which were originally released for the pre-.NET VisualBasic and got updated over the years. Entering "spreadsheet component" into your favorite search engine should turn up a few examples, though I'm not aware of any free ones. However, last I checked, you can also embed OpenOffice into your application, which is free.

Share this post


Link to post
Share on other sites
Also, writing a simple recursive-descent parser (google it) isn't too difficult if you have source or a tutorial available, and is probably something useful to learn. I know codeproject.com has many examples of these. I actually learned how they work from some source out of an old book on the C language, and since then I've had use of that knowledge from time to time on various projects.

I don't know of any simple 'plug in and go' type of solutions, however, if that's what you're looking for. There is SourceGrid, which might help with some of the UI problems of implementing your own spreadsheet-style app, but AFAIK it doesn't have any expression-parsing functionality.

Share this post


Link to post
Share on other sites
suppose i do have a way of getting a cell to understand the formula. in that case, how do i get the cell to show the result of the formula instead of the formula?

for example,

A1 = 1
A2 = 1
A3 = A1+A2

in this case, cell A3 should show 2. but it should always be a formula in there instead of a hard coded 2.

Share this post


Link to post
Share on other sites
There is no built in way to do this period. If you want to use a gridview , then you're using a gridview. A gridview has no such built in logic it's simply there to let you display tabular data. If you want to reimplement it all yourself (and it's a lot of work) you would have to do it all. Not just fill a few gaps but basically do everything the guys who did excell did. That is for example just to make A3 be A1+A2 you'd need:
1) a way to define expressions
2) to evaluate all expressions everytime a column containing members of that expression changes
3) to update all fields with the results.
4) everything else i must have missed

The reason people suggested python & eval is because as "1+1" makes 2 in C# it does so in pretty much every language (like in python) but python supporting the eval functions means it support evaluating statements in it's own language. So basically you can tell python "evaluate this string , treat it as python code and gimme the result". If you did this on your expression and your expression looked like something valid in python , if you were to run eval on it it would give you the result , freeing you from all the expression processing but still leaving you to handle cell changes/result updates.

However , don't do it , either embed excell if you can deal with it only working on machines with excell or better express why you need this so it's possible to guide you to a better solution.

Share this post


Link to post
Share on other sites
Well like excel you could embed a user defined equation symbol then extend the gridview. Wouldn't be hard.

But what you probably want is to use the Infragistics Ultra Win Grid. It has a print function that allows you to export to excel.

You can also use SSRS reports to export to excel. ( SQL Server Reporting Services )

I mwork for an negineering company so I understand the odd request. Stay in c#.

Nick

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this