|
I'm really struggling trying to bind a Datagrid to a collection of objects. I understand that for each public property of the objects in the collection, a column will be displayed. But I don't want to display a column for all of the public properties, just some of them. And I also want to add columns to the datagrid for things like an image that is associated with one of the properties.
How do you hide columns when binding to objects?
How do you add columns to a datagrid when it is bound to a collection of objects?
Any help would be greatly appreciated!
Thanks
Warwick
"I hear they've even got the internet on Computers now." - Homer Simpson
|
|
|
|
|
If you databind a datagrid to a list of objects, it will create a column for each property as you said.
However, if you first create a DataGridTableStyle and add to it the DataGridColumnStyle objects that you want then only those columns will be shown.
This can be done directly in the designer.
|
|
|
|
|
Thanks! I've just had a look at TableStyles/ColumnStyles and will try it today.
Cheers
Warwick
"I hear they've even got the internet on Computers now." - Homer Simpson
|
|
|
|
|
Take a look at ITypedList. Adding it to your collection allows you to create property descriptors. The Datagrid will respect this list. This is more "class friendly" to implementers and they won't have to use table styles to get rid of the unfriendly stuff.
|
|
|
|
|
Hi,
Can someone please tell me how can I zoom in and out of an image that is displayed in a picture box control???
Please Help...
Thanks,
E.A.
|
|
|
|
|
As far as I know, the picturebox does not directly support such a thing. You would have to write custom code to scale the image from an offscreen source image, then reset the picture box .Image property. You could also write your own simple custom control that intrinsically supports image scaling, which would be more elegant and probably smoother.
|
|
|
|
|
Here's an example of what I'm dealing with:
int[] myints = new int { 1, 2, 3 };
foreach(int i in myints)
{
Console.Write(i.ToString()); // WORKS PERFECT.
}
Console.Write(myints.Length); // Displays 3.
int[] newints = new int { 1, 2, 3, 4 };
myints = newints;
foreach(int i in myints)
{
Console.Write(i.ToString()); // WORKS PERFECT.
}
Console.Write(myints.Length); // Displays 4
int[] reallynewints = new int { 1, 2 };
myints = reallynewints;
foreach(int i in myints)
{
Console.Write(i.ToString()); // THROWS NULLEXCEPTIONERROR
}
Console.Write(myints.Length); // Displays 2
So, here's the problem I'm having. When you set an array equal to an array with less elements, the Length property is updated, but there is still a null reference inside the array to an unused array element. You can fix the problem by overloading the Int.ToString() method with a try-catch block to avoid the error, but that's just a patch. How can I reset the array completely, so there is not a null reference to unused elements?
|
|
|
|
|
budidharma wrote: foreach(int i in myints)
{
Console.Write(i.ToString()); // THROWS NULLEXCEPTIONERROR
}
Console.Write(myints.Length); // Displays 2
How it display 2 if you got a NullReference exception?
xacc-ide 0.0.99-preview7 now with C#, C, C++, IL, XML, Nemerle, IronPython, Perl, Caml, SML, Ruby, Flex, Yacc, Java, Javascript, Lua, Prolog and Boo highlighting support!
|
|
|
|
|
The way I just wrote that, it wouldn't cause the error would occur first. If you put the myints.Length line before the foreach statement, it will display the correct length.
|
|
|
|
|
budidharma wrote: When you set an array equal to an array with less elements, the Length property is updated, but there is still a null reference inside the array to an unused array element.
That can't be true. Arrays being reference types, everytime you assign something to myints, the earlier reference is overwritten with the new one. So myints = reallynewints maeks myints point to the reallynewints array now.
The source of your problem is somewhere else. Are you doing something to the myints array after the assignment but before the foreach statement?
Regards
Senthil
_____________________________
My Blog | My Articles | WinMacro
|
|
|
|
|
Before I answer, how many times does the last foreach loop? Does it loop twice, then give the nullexception? Or does it immediately give the nullexception on thefirst iteration?
|
|
|
|
|
hi i am c# programmer and thank you for your site . it is best programming site .
how to get serial number of cd in c# ?
please help me ...
thanks
hdv212@yahoo.com
hdv212@yahoo.com
|
|
|
|
|
hi all,
am using .NET 2.0 and have a service that has a notifyicon, the service runs as LocalSystem and is allowed to interact with desktop.
when the service starts the tray icon appears, (and the service spawns a form thread, successfully) but i dont have a contextmenu when i click the icon, i have put a click event on the icon as well to restore a form that the service also starts and the event doesnt appear to be firing.
is this a problem, with it running as localsystem ? or have i completely gone pear shaped with something.
<br />
private void notifyIcon_MouseClick(object sender, MouseEventArgs e)<br />
{<br />
MessageBox.Show("hey");<br />
}<br />
<br />
private void notifyIcon_Click(object sender, EventArgs e)<br />
{<br />
MessageBox.Show("hey");<br />
if (this.proxyServer.Visible == false)<br />
{<br />
this.proxyServer.Visible = true;<br />
this.proxyServer.WindowState = FormWindowState.Normal;<br />
}<br />
}<br />
neither of these events are showing a messagebox
kind regards,
g00fy
|
|
|
|
|
Is it possible in C#?
I have a class, Hand. It contains an Array of cards. I have defined a property in the array which returns the array of cards that it contains, but it makes for convoluted syntax:
Player.Cards.Card[1];
Where Player.Cards refers to the "hand" a player holds.
If my Hand class looks something like:
public class Hand
private Card[] m_Hand;
public Hand(params Card[] cards)
{
m_Hand = cards;
}
public Card[] Cards
{
get
{
return m_Hand;
}
}
}
Can I simply overload the index operator for my Hand class and get rid of the Cards property?
So I could do something like:
Player.Cards[0];
Would refer to: Player.Cards.m_Hand[0];
That make sense?
As always, thanks!
|
|
|
|
|
Yes you can, using this as a property name and optionally using the IndexerNameAttribute to get it a name (for some languages like VB.NET have to refer to the indexer while C# doesn't) other than "Item":
public class Hand
{
[System.Runtime.CompilerServices.IndexerName("Card")]
public Card this[int index]
{
get { return m_Hand[index]; }
}
}
This posting is provided "AS IS" with no warranties, and confers no rights.
Software Design Engineer
Developer Division Customer Product-lifecycle Experience
Microsoft
[My Articles] [My Blog]
|
|
|
|
|
Hi,
I can't change the background color of a specific cells in a datagrid on RUN TIME. I find a article but this article works on DESIGN TIME.
My problem is: For example user select a color on the colorDialog and i want to change current cell color. Not all row, just one cell change it.
For example: select green, ROW#1 CELL#5 is green
select red, ROW#2 CELL#5 is red
select blue, ROW#3 CELL#5 is blue ....
Please help me! Pleseeeeeeeeeeeeeeeeeeee...
I try from 3 day.
BORAN
|
|
|
|
|
|
This article on Design Time. I want to change on Run Time. Yes, i can solve it but some new problem. I can't understand how to add component class. I add ColumnStyle.cs but error on ColumnStyle() function.
Thanks alot
|
|
|
|
|
In your class that derives from DataGridColumnStyle and in your overload to DataGridColumnStyle.Paint you'll get a CurrencyManager and a position to use with that CurrencyManager to get the data for the cell being drawn and then paint it how you want.
I'm really not sure how you want to set it at run-time. Please be specific. Do you want some property to arbitrarily or based on data? The article is about how to set the color of a cell based on the value of the data in that cell and that happens at run time.
This posting is provided "AS IS" with no warranties, and confers no rights.
Software Design Engineer
Developer Division Customer Product-lifecycle Experience
Microsoft
[My Articles] [My Blog]
|
|
|
|
|
How to add this code on my form:
protected override void Paint(Graphics g,Rectangle Bounds,CurrencyManager Source,int RowNum, Brush BackBrush ,Brush ForeBrush ,bool AlignToRight)
{
bool bdel = (bool) GetColumnValueAtRow(Source, RowNum);
if(bdel == true)
BackBrush = Brushes.Coral;
else
BackBrush = Brushes.White;
g.FillRectangle(BackBrush, Bounds.X, Bounds.Y, Bounds.Width,Bounds.Height);
System.Drawing.Font font = new Font(System.Drawing.FontFamily.GenericSansSerif , (float)8.25 );
g.DrawString( bdel.ToString() ,font ,Brushes.Black ,Bounds.X ,Bounds.Y );
}
|
|
|
|
|
You need to write your own class that extends DataGridColumnStyle , then you instantiate that class and add it to a DataGridTableStyle that you add to DataGrid.TableStyles . This isn't a component you can just drag and drop, but you have to write your class and use it (like writing your own control and adding that to a form.
The article details how to do this and the sample source gives a thorough example of using the sample class. The link I gave you earlier shows a good example of how to use table styles to customize how the data is displayed; otherwise, only the base DataGridTextBoxColumn and DataGridBoolColumn are used.
This posting is provided "AS IS" with no warranties, and confers no rights.
Software Design Engineer
Developer Division Customer Product-lifecycle Experience
Microsoft
[My Articles] [My Blog]
|
|
|
|
|
I am looking into extending an existing solution to perform a minor user management. It's for the following scenario:
I want to block a user's account entirely so that it can't login. As soon as the user checks in with his pass, his account (linked to that pass) has to be enabled again. But he should be able only to login to a specific computer. After he logs out of the computer again and he checks out of the room using his card his account should be blocked again.
From my view on this scenario I need two things:
- First I need to be able to enable/disable accounts using LDAP (Becaus its novell they use).
- Second I need to be able to manage the account so it has access to a computer. (I know how to do it by hand using the management tools, but not from the LDAP)
Anyone have any ideas on how I might be possible to do this? I think I found the solution to the first part of the problem. But not to the second part of the problem.
WM.
What about weapons of mass-construction?
|
|
|
|
|
Hello! I just wanted to ask a question: If I write a simple C# applicationm, for examle a "Hello World!" program, do I already use the .NET Framework?
Thanks in advance!
Have a nice day!
|
|
|
|
|
Yevgeny Efter wrote: If I write a simple C# applicationm, for examle a "Hello World!" program, do I already use the .NET Framework?
Yes
'A programmer is just a tool which converts caffeine into code'
|
|
|
|
|
To be more specific, C# is one of many language that compile to Intermediate Language, or IL. VB.NET is another example. All those compile to IL that then gets compiled on the fly by the Common Language Runtime (CLR).
The .NET Framework is a set of base class libraries (like Windows Forms, ASP.NET, etc.) and the CLR.
This (and a few more acronyms) allow languages to work together, like a Windows Forms application written in VB.NET can use a library written in C# because when both are compiled they are compiled to IL.
This posting is provided "AS IS" with no warranties, and confers no rights.
Software Design Engineer
Developer Division Customer Product-lifecycle Experience
Microsoft
[My Articles] [My Blog]
|
|
|
|