|
I'm making the switch from MFC to C#, or at least attempting, so here's an opportunity for a couple of you to get points for posting an answer to what is probably an easy question (keep looking for my name, more will be coming.) At any rate, I want to be able to use a DataGrid object on my form. I have read through countless articles, but I'm just not getting it. All I want is 8 columns, each of a primitive type (int, string, double). I just want the grid to be blank, with the first row showing, then when the user puts information into the first row, the next row shows up, etc. Is this really that hard to do? I'm using Visual Studios .NET, fyi.
Please only point me to an article if it answers my question...so far all of the articles I've found just bind the grid to a pre-existing data source...that's not what I have here. Thanks a ton...and for those using c# for a while...don't bash me too hard!
|
|
|
|
|
Just create a new DataTable, add a few columns of the appropriate type, and set the grid data source to the new table You have to make sure the grid allows new:
DataTable t = new DataTable();
t.TableName = "test";
t.Columns.AddRange(
new DataColumn[] {
new DataColumn("one", typeof(string)),
new DataColumn("two", typeof(int)),
new DataColumn("three", typeof(DateTime)),
new DataColumn("four", typeof(string)),
new DataColumn("five", typeof(bool)),
new DataColumn("six", typeof(double))
});
dataGrid1.DataSource = t;
The above code in my small test app was in the constructor of a form after the InitializeComponent() call. I created a C# Windows Forms project, added a DataGrid, then added the above code.
It should be a simple starting point.
|
|
|
|
|
The code that uses pre-existing data sources is what you need. The DataGrid - without overriding practically everything about it - is a data-bound control. If you use it for data entry, you must get the data from it somehow. You can do this by retrieving the DataSource value, which can be anything that implements IList or IListSource (like a DataSet , ArrayList , etc.).
Nothing says that data has to contain data initially, though. The easiest example is to use a DataSet , which you can - through code or by creating a typed DataSet - define a table and columns. In your DataGrid , set the DataSource to the empty DataSet instance, the DataMember to the name of the DataTable within the DataSet and you're basically done. You'll find that you'll have more control over the layout if you use the DataGrid.TableStyles property to add a corresponding DataGridTableStyle with various (8 in your case) DataGridColumnStyle s. The documentation in the .NET Framework SDK for any one of those 3 aforementioned properties and classes will have an example (same example, IIRC).
You could also use an ArrayList or any other type of list with a list of classes that define properties and bind your DataGrid in the same way. The problem is this can be difficult if you have no initial data since the DataGrid wouldn't know how to construct your data source.
When the DataGrid is bound - even to an empty data source - rows can be added like you want. With a data source, the DataGrid can't add rows to anything because it has nothing to add them to. Give it a data source (again, I recommend a DataSet ) and now it has something to modify since it's just a view on the data source.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
hi,
I want to ask, how can I get mouse position in realtime except getting it inside MouseEvent Handler?
The Idea is, I have one context menu, after I clicked the menu button, I want to show one dialog at the mouse location immediately.
Thanks for help
|
|
|
|
|
You can get the mouse position at any time - in screen coordinates - using the static Control.MousePosition method which returns a Point . To convert these coordinates to form coordinates, call PointToClient with the Point on the control that you want to the relative coordinates for (relative to the upper-left corner).
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Hello,
In my app a user chooses an icon. I want to save that icon to a file. However, no matter what I try, the image is saved incorrectly. I did some searching and found that there may be some troubles with saving the alpha channel info. I am no expert on images so I have no idea.
All I want to do is save the icon to a file. I tried
<br />
using (StreamWriter swI = new StreamWriter(Application.StartupPath + "/TestI.ico")) <br />
{<br />
ico.Save(swI.BaseStream);<br />
}<br />
However, the icon is saved in poor quality and when I load it, it looks pretty bad.
Can someone please help me? I just want to save an icon and be able to load it when I need it.
P.S. Is it possible for me to save multiple icons in one file and extract the icons one at a time?
Thanx for the help,
-Flack
|
|
|
|
|
A few things - don't use a StreamWriter , you may run into encoding problems. An icon is a binary file, so just use something simple like a FileStream .
Also, instead of Application.StartupPath + "/TestI.ico" , use Path.Combine(Application.StartupPath, "TestI.ico") , which properly combines the directories and/or filename with the correct directory separator. It may have worked this time, but it usually won't depending on the circumstances.
Finally to the real problem. The icon support in the .NET stinks. Icon files contain a header section that describes the resolution and addresses of indexed bitmaps (has palettes) within the file. Because of an apparent but in the System.Drawing classes, the 32-bit icon (which contains the per-pixel alpha channel) is not well preserved (and sometimes not at all).
The best way is to P/Invoke the various icon functions from the Windows Shell APIs, like SHCreateFileExtractIcon , which uses the COM interface IExtractIcon , which is pretty simple to declare as a managed interface (be sure to use the GuidAttribute with the right interface ID (IID)).
As far as extracting various resolutions from an icon file with multiple bitmaps, this is exactly what Windows does with methods like that from above and all the lower-level icon functions like GetIcon . Most of the time, the small and large icon handles (HICON ) are returned at once with the appropriate resolution. In order to get 32-bit icons with the native APIs, you must use an application manifest to bind to Common Controls 6, which is described in my article Windows XP Visual Styles for Windows Forms[^]. If you're using .NET 1.1 or higher, you can also call Application.EnableVisualStyles before Application.Run , but be sure to read the documentation for a few quirks (like calling Application.DoEvents in between the two to avoid common problems).
If you want complete control over the icons, how they save, and which icons are extract, read about Icons in Win32[^]. All those structs can be create in .NET easily enough, so long as you use the appropriate StructLayoutAttribute since some structs have a pack size, IIRC, of 2.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Does anyone have an idea on how to capture scrollbar events from a ListView control?
|
|
|
|
|
Extend it and override WndProc . Handle the WM_HSCROLL (0x0114) and WM_VSCROLL (0x0115) notification messages. See the Platform SDK for the possible WPARAM and LPARAM values.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
I running a service with no dialogs. I like to put an icon in the tray to show that the process is running. I don't know where to begin.
Thanks.
|
|
|
|
|
If this is running as a true Windows Service (Services Control Panel), then you would normally write a second app that communicates with the service you wrote and controls the icon in the Tray.
RageInTheMachine9532
|
|
|
|
|
other than that u could just insert a notifyicon component from the toolbox :P
i jsut havent figured out how to add a context menu to it!
|
|
|
|
|
I got a file which is an assembly file (managed code).
Now how do I find out programatically from unmanaged code?
TIA
Promise only what you can do. And then deliver more than what you promised. This signature was created by "Code Project Quoter".
|
|
|
|
|
Kant wrote:
I got a file which is an assembly file (managed code).
Now how do I find out programatically from unmanaged code?
Find out what?
RageInTheMachine9532
|
|
|
|
|
Dave Kreskowiak wrote:
Find out what?
My bad.
Find out programatically(from unmanaged), Is that file is an assembly file or not.
Promise only what you can do. And then deliver more than what you promised. This signature was created by "Code Project Quoter".
|
|
|
|
|
See my article, Shell Extensions for .NET Assemblies[^], a handy overlay icon handler that distinguishes between the two. The source is included.
There's a single bit that is different in the PE/COFF header of the executable. You load and map the DLL or EXE as a data file and traverse several virtual tables to find the address of this bit, then you check it.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Heath Stewart wrote:
There's a single bit that is different in the PE/COFF header of the executable
Now how the HELL did you figure that one out??
Damn geniuses...year younger than me, already got an MVP...I don't have an MVP title...work my fingers to the bone, day in, day out, for bent pennies...
|
|
|
|
|
I strive to know everything.
I read an article a long time back in MSDN Magazine from executable master Matt Pietrek, the guy who wrote dumpbin.exe. In this article he briefly mentioned about this bit but nothing more. Using concepts from his article i dug into the PE/COFF header format and wrote the code to find that bit and check it. Learned a heck of a lot about the PE/COFF in the process, which is really what I was after in the first place.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Heath Stewart wrote:
I strive to know everything.
*sigh*
Don't take this the wrong way Heath, but sometimes I really wished you lived around here....you sound like you share so many of my viewpoints, you'd be cool to hang out with
Want some (unaskedfor) advice from someone who's only a little bit older? Don't limit your strive to coding related stuff alone...right now, I've got about 20 different things I'm trying to teach myself now, and only a handful are computer related. Makes life a blast
Jeremy Kimball
|
|
|
|
|
Don't take this the wrong way, but I really hate agism. I - and a handful of others blessed with the skills of good development - were programming circles around some of our University profs. I was one of 3 go-to guys when I was still in high school in a town of over 20,000 back then. In this field, actually development skills have little to do with age. Again, don't take it the wrong way, but it's just a pet pieve of mine. I've experienced a lot of unnecessary hardships because of it from upper management taking credit or passing credit to others for what I did soley (and I don't mean just giving credit to the team, which I could handle - I mean actually saying that they personally did it all).
I also don't limit myself to development (not coding, which I use to refer to those that blindly write code without really knowing what they're doing). I love photography and people - including my father, a professional photographyer - say I have an eye for it. I also learned WAY MORE about feminine hygeine in high school since most of my friends were girls (as you can probably guess, I wasn't a big fan of athletes).
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Don't take *this* the wrong way, but you need to lighten up
I was being extremely sarcastic in saying I'm older than you. (born on 5/10/77, which makes me a whopping year older than you). I was and still am in the same boat you're in now - problems dealing with "old-school" developers who don't trust your judgement, even when it's obviously right.
I had the same situation with the whole female thing in high school...it's an interesting experience trying to explain the female ovulation cycle to your girlfriend
|
|
|
|
|
public sealed class Singleton<br />
{<br />
static readonly Singleton instance=new Singleton();<br />
<br />
static Singleton()<br />
{<br />
}<br />
<br />
Singleton()<br />
{<br />
}<br />
<br />
public static Singleton GetInstance()<br />
{<br />
return instance;<br />
}<br />
}
That was from this[^] article.
Maybe you can answer this question:
Is there an explicit need there for the private constructor to avoid initialization? If we remove it completely, will .NET supply a default public parameterless constructor?
Sammy
"A good friend, is like a good book: the inside is better than the cover..."
|
|
|
|
|
The point of making a constructor private/protected is so that someone using the object via the API can't create an instance on their own. This is very handy if you want to make an object with singleton behavior.
The only way you get the instance (the only instance) is using GetInstance . Doing it this way, there is no other way create the object. If you leave a construtor publicly exposed then it cirumvents the code you put in place to enforce the singleton behavior.
|
|
|
|
|
Thank you that was very clarifying.
Sammy
"A good friend, is like a good book: the inside is better than the cover..."
|
|
|
|
|
If you remove the private default constructor, yes, the compiler will provide one.
Also, this isn't the best singleton pattern to use. The one I posted earlier was. It uses a double-check locking approach which eliminates a couple race conditions that may occur.
This has been discussed in the past in this forum so if you want to know more, try searching for "singleton" or something for the C# forum.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|