Q: DataGrid [Attribute] ???

B

Bret Pehrson

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.

Thanks
 
B

Bret Pehrson

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...
 
J

John Saunders

Bret Pehrson said:
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

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

John Saunders
 
B

Bret Pehrson

Manohar said:
Why not use a DataGridTableStyle?

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.

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

Bret Pehrson said:
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.

Thanks
 
T

Tom Krueger [MSFT]

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.

Bret Pehrson said:
Manohar said:
Why not use a DataGridTableStyle?

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.

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

Bret Pehrson said:
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.

Thanks
 
B

Bret Pehrson

Tom said:
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.

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.
Definitely should be native to the control.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top