all groups > dotnet windows forms databinding > december 2004 >
You're in the

dotnet windows forms databinding

group:

Q: DataGrid [Attribute] ???


Q: DataGrid [Attribute] ??? Bret Pehrson
12/21/2004 12:40:49 PM
dotnet windows forms databinding:
Question/issue:

I'm using a custom data source for my (WinForms) datagrid:

public class MyDataSource : IList
{
public void AddRow(string firstName, string lastName);
// ...
}

each row is a:

public class MyDataRow
{
public string FirstName
{
get { return firstName; }
}
private string firstName;
//...
}

Then:

MyDataSource data = new MyDataSource();
data.AddRow("Bret", "Pehrson");
//...

grid.DataSource = data;


Now, everything displays fine in the DataGrid. I'm not using any designer or
other parameters to set up the columns, just letting the data source and grid
figure that out. The only problem that I have is that I want the DataGrid to
display "First name" as the column header and not "FirstName" as is currently
happening.

What I'd hope would be possible is some attribute on the FirstName property
that would specify the column name, something like

[HeaderText("First name")]
public string FirstName
{
get { return firstName; }
}

Is there such a beast? I've looked and looked, and can't find anything. I
want all of this to be done in the code for the MyDataRow/MyDataSource class.

Re: Q: DataGrid [Attribute] ??? Bret Pehrson
12/22/2004 10:15:42 AM
Does anyone have a comment on my question/issue? No responses because:

- don't understand what I need

- never done what I need

- isn't possible

- don't care about this topic

- ???

_ANY_ info would be greatly appreciated...



[quoted text, click to view]
Re: Q: DataGrid [Attribute] ??? John Saunders
12/22/2004 1:18:28 PM
[quoted text, click to view]

Isn't possible, as far as I know, unless you use a DataTable.

John Saunders

Re: DataGrid [Attribute] ??? Manohar Kamath
12/24/2004 2:30:25 AM
Why not use a DataGridTableStyle?

--
Manohar Kamath
Editor, .netWire
www.dotnetwire.com


[quoted text, click to view]

Re: DataGrid [Attribute] ??? Bret Pehrson
12/27/2004 1:18:02 PM
[quoted text, click to view]

Because I want to affect the change through the class/data (domain-level
object) rather than the grid (presentation layer).

In my mind, it seems that it would be appropriate if the DataGrid control to
use attributes on the data itself for some of the display characteristics. In
my case, column name is a good example, as is numeric formatting, etc.

Implementation of this feature would be trivial within the DataGrid, but
obviously didn't happen as I haven't been able to locate anything on it, and no
one has indicated otherwise.


[quoted text, click to view]
Re: DataGrid [Attribute] ??? Tom Krueger [MSFT]
2/15/2005 3:21:21 PM
This article may give you some thoughts on how this can be accomplished,
however, I'm not sure if it goes all the way.
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnadvnet/html/vbnet08262002.asp

I assume you could just use reflection to get the attributes of each
property and then build out DataGridTableStyles.

--
Tom Krueger

My Blog - http://weblogs.asp.net/tom_krueger
Smart Client DevCenter - http://msdn.microsoft.com/smartclient/
Mobile DevCenter - http://msdn.microsoft.com/mobility

This posting is provided "as is" with no warranties and confers no rights.

[quoted text, click to view]

Re: DataGrid [Attribute] ??? Bret Pehrson
2/17/2005 4:34:23 PM
[quoted text, click to view]

I ended up implementing this myself using custom attributes applied to the
underlying data classes.

I overrode the DataSource proper setter on the grid, and scan the incoming data
source for my attributes. When found, I simply modify the column characteristics.

It turned out to be fairly simple to implement, and hugely powerful in that I
don't have to write any grid interface code at all for my custom data sources.
AddThis Social Bookmark Button