Display data in a datagrid - c#

I am working with C# in Visual Studio Community 2013. I have been trying to display data in a DataGrid that I have set up in a Window in my application. I've been finding various web pages including 1 or 2 from stackoverflow that are supposed to show ways of inserting and displaying the data, but every time I try one of these approaches, it doesn’t work for me.
Most of the time it seems like the code in these pages is from a previous or different version of Visual Studio and it just won’t work with Visual Studio Community 2013. I say that because I keep getting error messages in VS 2013 telling me the code isn’t correct in one way or another. This is when I’ve copied what’s in the corresponding web page exactly. Also, I’ve usually spent time looking into the errors without finding a way of resolving them.
I just want to be able to insert data into the DataGrid so that I can see it when I look at the app.
Please can someone show me how this needs to be done, or point me in the direction of a website/page that will show me how to get this working? This is obviously in Visual Studio Community 2013. I realise that I need to use data binding, but so far I haven’t found a way of doing that that actually works in my code.
I could paste the code I have so far up here, but there’s a lot of it and it would take up a lot of space. I will therefore hold off from doing that until someone actually asks for it.
At this stage I have the database data set up in class instances, which store the data that I want to display. The data is all just bits of text or numeric values. The instances are stored in a class that’s like a CollectionBase List, so that I can get individual instances from the list as I need them. My aim is to be able to display all of the instances, or just some of the instances from the list, dependent on how the user interacts with the application. However, at this stage I just want to be able to display something, and so far that hasn’t been possible.
I also have the DataGrid set up with all the required headers for the various columns, plus various settings that I found were needed from looking on line. These can obviously be changed as and when I get some guidance on how to sort this out.

Related

How can i make an application with wpf that makes an new application in runtime?

I want to make an application WPF C# where i can make questions and drag drop picture and then save it. And then when i hit the save button it Makes an new application. Is this impossible? Does someone have an good url i can visit about this? I can't find anything when google it.
There already exists such an application - it's called Visual Studio. :-)
Joking aside, to answer your question, yes, it is definitely possible to build such an application. There would be numerous ways of doing it, depending on specifics.
One aspect of such an application, let's call it AppBuilder, would be to generate and compile code for the target application. It could be done, for example, using Roslyn, the C# compiler accessible from C#.
Another aspect would be to allow the user to specify the questions, as you describe, and to allow the user to place images. Overall this would require something like a design surface with tools to manipulate the placement of the images and words, not unlike the WPF designer in Visual Studio.
While the user builds the application, there has to be a way of them to save their work, to be able to come back to it later. Does every Save generate and compile the target application? Probably not. So there is a need for a persistence mechanism, and a database, or structured file of some sort, to save the user's work in a form that can later be read back and the project reconstituted in memory.
Those are just the three most obvious features that such AppBuilder would need to support. Now for a reality check: it would not be simple, and depending on your specific requirements, it could get quite complicated. What about debugging the generated application? What about styling? Etc., etc.
I don't mean to discourage you, but maybe you can provide more information about what exactly you want to accomplish, and then we could discuss more.

Change DataGridView connection and query on another form based on selection c#

I've been trying to find an answer for this and I'm completely lost.
I have an application that starts with a logon screen, after the client is logged on, it opens an MDI Form in which I have some items on a menu strip. One of them are assets (which is what the app will be used for), and the assets menu has a drop down with different assets to select, PC's, software etc.
When you click on PC's, another window opens within the MDI Form that has a DataGridView on it. And it works great but, I don't want to create a window for each and every connection or table in my databases, I feel this is bad practice.
So what I want is, to be able to change the DGV based on the asset selection, but for the life of me I can't figure out how to do this. I've been trying to pass parameters from one form to the other but the I just get different errors etc.
Can you guys please point me in the right direction? I am new to visual studio etc and want to learn these things, so I'm not asking for freebies so to speak, just what the logic should look like more or less.
Thanx!!
I don't have code for what I'm trying to do because I have no idea what the logic should look like.
I'm still trying wrap my head around how two forms communicate in a way I need.
I'm used having variables in a loop and when they change values the result changes. But in visual studio everything needs to be called which makes it a bit difficult for a rookie like me.
I did try sending sql commands via variables, but read that, that is a very bad idea.
I just want the DGV to change the data its showing based on a button selection.

Having done them in Win8, how do I do Split Pages in Windows 8.1?

My team and I have recently migrated our app from Windows 8 to Windows 8.1. It worked fine until I went to update the existing pages and add a new one. Instead of any errors, just nothing happened.
The way I understand it is, in Windows 8, split pages had their information stored in a DataSource class, and I could even put all the stuff for all the split pages into the one class if I wanted to.
It's changed in Windows 8.1, though, so all the content for the pages go into the code behind pages for that actual page.
I can't actually seem to find any examples, articles or anything online on how to do this. Would anyone be able to link me to such an article or explain how the groups and items are meant to go into the class?
Thanks!
Reply to htuomola:
When I tried adding a new split page, at the time I didn't realise that how you put the content into the page is handled differently, so I just went and did the same as before. I admit, it might not be best practice to have done it like this, but at the time I felt it made the most sense. I had all the groups and items for the various split pages in the one data source class, and the individual pages just pointed to which items/groups I wanted. So, not knowing any better, I made an identical split page to the others, and it didn't pick up the new groups/items, but it did find the old ones. The old split pages were working fine at this point too. It was only when I tried to make a new 8.1 style split page, that they all stopped working and threw errors like "The type 'IME.Miscellaneous.Additional.Help' already contains a definition for 'itemDetailGrid'" and ""IMEDataSource" does not exist in the namespace "using:IME.Data".", even though I didn't change the pages that are having these errors - at all.
The Split Page item template still exists with Windows 8.1 applications even if the NavigationHelperand visual state handling code is a bit different. I think it also works pretty nicely out-of-the-box. Did you try adding a new Split Page page to you project, what problems are you facing?
Some documentation that you can find is in MSDN project templates and item templates pages. You should probably find a few samples in MSDN that use the Split Page/App template.
About the information storage, do you mean that in Windows 8.0 there was a separate view model class and 8.1 only has an ObservableDictionary property on code behind? Nothing prevents you from replacing that with a better solution. I'd just delete it and follow MVVM guidelines, setting up a ViewModelLocator and view models.

Add custom editor windows to Visual Studio window panes

My Problem
I'm trying to build an extension to Visual Studio that allows code to be edited on a per-function basis, rather than a per-file basis. I'm basically attempting to display code in a similar fashion to Microsoft Debugger Canvas.
I'm wondering how to host multiple Visual Studio editors within a single window (I believe the windows are implementing IVsWindowFrame). The functionality I'm after can be seen below:
Each editor window retains typical functionality and interacts with third-party extensions as expected. (For example, VsVim functions correctly within these windows).
What I've Tried
I've spent almost two weeks researching and trying this stuff and I'm having a lot of trouble figuring out which services, interfaces and classes I'm going to be using.
Reading through MSDN
First off, most of the documentation discusses how to edit a single editor window and add adornments, tags, margins etc. It doesn't discuss the possibility of spawning multiple editors within a window pane.
I've looked through the documentation on a vast number of interfaces of interest to me including IVsTextBuffer, IVsTextView and IVsInvisibleEditor. Unfortunately I can't get some of these interfaces to play nicely together.
On top this, the usually excellent MSDN is extremely lacking in this area. Many of the interfaces contain only a list of members without even a basic remark on intended use and functional. (IComponentModel, for example).
Many of the interfaces make reference to a set of Editor Samples but the code cannot be read or downloaded on MSDN. Apparently it shipped with Visual Studio 2005, but I don't have this version of Visual Studio, nor can I find it.
Interacting with IVsUIShell
I can get access to all WindowFrames open using IVsUIShell.GetDocumentWindowEnum();
I see there is an IVsUiShell.CreateDocumentWindow() method, but I'm completely unfamiliar with the parameters it accepts, or if this is the correct path to go down.
What I need to do
Programatically create a dockable window pane
Programatically add editors to this window pane. (And ensure they're correctly registered within Visual Studio, the running document table, etc.)
Edit:
I'm sorry, I should have expanded on my steps. When I said I needed to register with the running document table and Visual Studio, it's because I want to actually edit the original document in my custom editor. Below is a short example of the functionality available in Debugger Canvas that I'm trying to recreate:
http://i.imgur.com/aYm8A5E.gif (I can't embed a .gif)
Alternatively:
If anyone knows where I can find the editor samples included with Visual Studio 2005 such as the Basic Editor Sample I'm sure I could figure this stuff out. The MSDN documentation has no code samples regarding these interfaces, which has made my job extremely difficult.
The Git Source Control Provider is an open source extension includes a tool pane that embeds a standard editor as a control within a custom WPF tool window. I would use this code as a reference for any Visual Studio 2010+ extension where I wanted to host an editor window in some custom location.
PendingChangesView.xaml includes a ContentControl named DiffEditor, the content of which will be the editor.
PendingChangesView.xaml.cs includes a method ShowFile, which calls a method to create the editor control and assigns the result as the content of DiffEditor.
ToolWindowWithEditor.cs includes a method SetDisplayedFile which returns a Tuple<Control, IVsTextView> interface, which provides access to a Control that can be added to a ContentControl as well as the IVsTextView for the text view. The heavy lifting is in this method.
Note that the SetDisplayedFile method includes several lines with the following form:
textViewHost.TextView.Options.SetOptionValue({name}, {value});
These lines perform key functionality for the Git Source Control Provider such as removing margins and making the window read only. There are many options available, so you'll want to review the documentation for DefaultTextViewOptions and DefaultTextViewHostOptions to apply the ones appropriate for your particular extension.
I haven't actually looked at the files that #280Z28 (why this username?) posted. I used to work on the Visual Studio editor and what you are trying to do has multiple facets to it that you should tackle independently:
Hosting multiple command targets inside a single IVsWindowFrame (this means that you'll have different elements inside the same pane from the point of view of Visual Studio's shell, and each of them need to have their own command handling. Consider the case where you put your caret in one of the mini-editors and want to undo using Ctrl+Z, moments later, you then place your caret into another mini-editor and do the same. Even though the WPF and Win32 focus have remained inside the same window frame (from the point of view of the Visual Studio Shell), the commands need to be routed to different components.
Using editor's that are displaying parts of another document. The mechanisms here that will be your friend are in the projection namespace. Projection essentially allows you to project a piece of buffer (or buffers) into a view. Ellision buffers are special case projection buffers that project from one source buffer into a target view while hiding areas of the buffer (this is most likely what you want). An example of a projection buffer is what happens inside a cshtml file. In that case, there is one buffer containing all the C# code, one buffer containing all the javascript, and one buffer containing the html and each compiler works off of that buffer, but the end user sees a projection of all these buffers into the editor's view with only relevant parts displayed (for example C# import statements are elided even though they exist in the real C# buffer.)
Managing the running document so that when an edit is made in a mini-editor, the real document is dirtied. You need to handle cases where the parent file is already open in the RDT in which case you want to dirty the same document when changes are made, and also cases where the document is not open, in which case you need to create a new entry in the RDT.
Also, please post to the Visual Studio forums, there are people who regularly check the forums and route the questions to corresponding devs.
Generally speaking, when it comes to the editor, avoid any traditional interfaces (anything that does not use MEF), so samples from Visual Studio 2005 should not be used as a reference point.
If you care enough and are in Seattle, you can try to go to campus as an MVP. There are days where you come to campus, and members of varying team will grab a laptop and come to your conference room and you can debug code together or hack away (while having access to debugging symbols and what not).
Last but not least, contact the code canvas guys, I'm sure they've solved many of the problems you are facing.
You need to register a tool window with your package extension; this can be done via the ProvideToolWindow attribute. The following article contains all the required information on how an editor can be hosted in a tool window: http://bit.ly/9VWxPR
Take a look at the WpfTextViewHost class; the article explains that this type is actually an UIElement, so I imagine that it´s possible to host multiple instances of it...

Replacement for Index in .NET

I've been out of doing proper programming for sometime, so as an exercise in trying to get some practice, I'm trying to make a program to solve Sudoku in C# (VS 2010)
My problem occurs when I'm trying to create some form of initial grid for the data out of text boxes. Back when I used to use VB6, I could call all text boxes as a single name and then give them all an index number which would allow me to refer to a specific text box when I was in a loop.
As far as I can see, there's no easily visible equivalent in C# and my searching has been to no avail although I can't imagine it'd be a feature that would be removed.
Thanks in advance
You can create a control array.
http://www.devasp.net/net/articles/display/674.html
It would probably be easier, and look better, if you use a genuine grid control like the DataGridView.
One bit of bad news. In the days of VB4,5,6 Microsoft used to release a new grid control with every version of Visual Basic, which was annoying (unless you rewrote your code every year). Well, they are still at it.

Categories