 |
| ASP.NET 1.1 As of 10/6/2005, this forum is locked as part of the reorganization described here: http://p2p.wrox.com/topic.asp?TOPIC_ID=35394. No posts have been deleted. Open ongoing discussions from the last week have been moved to either ASP.NET 1.0 and 1.1 Beginners http://p2p.wrox.com/asp-net-1-0-1-1-basics-60/ or ASP.NET 1.0 and 1.1 Professional. http://p2p.wrox.com/forum.asp?FORUM_ID=50. See my sticky post inside for more. |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the ASP.NET 1.1 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|
|

July 25th, 2005, 01:30 PM
|
|
Authorized User
|
|
Join Date: Nov 2003
Posts: 24
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Datagrid problems !
Hi,
I would like to change data field of a bound column and hyperlink column on the event onLoad of my form but i don't find the property.
thanks a lot
exarkuun
|
|

July 26th, 2005, 12:08 AM
|
|
Friend of Wrox
|
|
Join Date: Nov 2003
Posts: 1,348
Thanks: 0
Thanked 5 Times in 5 Posts
|
|
question: Why would you want to do this?
|
|

July 26th, 2005, 07:34 AM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 1,998
Thanks: 0
Thanked 3 Times in 3 Posts
|
|
Hello,
Try doing so in Page_Load. Though, I'd have to ask to what do you intend to do, maybe we can help more if we understood what you wanted to try to do...
Brian
|
|

July 28th, 2005, 07:28 AM
|
|
Authorized User
|
|
Join Date: Nov 2003
Posts: 24
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Hi,
I was not clear, my bad. When you ant bind a datagrid with a object who implement ICollection you need to define every column in the property builder of the datagrid but that work perfectly when you know in advance what fields you will bind on every columns, but i want to choose the field that i will bind on the page_load of the form, but i didn't find the properties of the datagrid in the behinde code who do what i want. Do you have any example that do what i want to do.
thx i hope that i'm a little bit more clear.
François
|
|

July 28th, 2005, 12:44 PM
|
|
Friend of Wrox
|
|
Join Date: Apr 2004
Posts: 204
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
I am by no means an expert on the datagrid control, but it occurs to me that even if you could change the DataField property of a bound column at runtime, there are probably a whole host of reasons why you shouldn't. Moot point, however, because it doesn't seem like you can.
My suggestion would be instead to create the datagrid with every column you think you may use, and make visible just the ones you are actively using.
An alternate (and messier) approach would be to change the dataSource property of the dataGrid to a different dataView or dataSet table based on your program's needs.
Very messy: declare a SQL query that returns different fields as the same name, e.g.
Code:
Dim cmdCommand as New SqlCommand
Dim fieldToGet as String = <<however you get your varying field name>>
cmdCommand.CommandText = "SELECT " & fieldToGet & " AS MyField FROM <<TableName>>
cmdCommand.Connection = <<Active Connection>>
Dim reader as SqlDataReader = cmdCommand.ExecuteReader()
Then re-bind your datagrid to the DataReader or DataSet you created. Your bound column's DataField propery will always be "MyField", but the contents of MyField will be a different field from your database.
But again, I don't think you should go this route, you could run into all sorts of snags. I'll probably get blasted for even suggesting it.
Aaron
|
|

July 28th, 2005, 01:22 PM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
With the latter solution, I think you're better off with an AS clause in your query. e.g.:
SELECT SomeColumn AS MyField
OR
SELECT SomeOtherColumn AS MyField
will both return a column called MyField but from two different source columns.
I was interested in seeing if it was possible to change them dynamically, so I ran a little test. Let's say I have a method that returns a DataSet with a Title and a LargeImageUrl column. I then use the following DataGrid:
Code:
<asp:DataGrid ID="Grid1" Runat="server" AutoGenerateColumns="False">
<Columns>
<asp:BoundColumn DataField="Title"
HeaderText="Title"></asp:BoundColumn>
<asp:BoundColumn DataField="LargeImageUrl"
HeaderText="LargeImageUrl"></asp:BoundColumn>
</Columns>
</asp:DataGrid>
When this Grid is bound, all it does is display the two columns, just as you'd expect.
Now, let's say I want to change the second column, so it displays the Title as well. The trick is here to get a reference to the right column, and then cast it to the appropriate type, a BoundColumn in this case. Once you have a strongly typed BoundColumn, you can change its DataField property to another column in the DataSet, before you call DataBind()
Code:
Grid1.DataSource = GetMyDataSet()
Dim myColumn As BoundColumn = CType(Grid1.Columns(1), BoundColumn)
myColumn.DataField = "Title"
Grid1.DataBind()
When you run this page, it will have two columns that have Title and LargeImageUrl as their headers. However, both columns now display the Title....
Does this help??
Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
|
|

July 28th, 2005, 01:27 PM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
I forgot one thing. Instead of reusing existing columns, you can also create your own. The following code generates a new BoundColumn and plugs it into the grid, right between the other two columns:
Code:
Dim myNewBoundColumn As BoundColumn = New BoundColumn
myNewBoundColumn.DataField = "LargeImageUrl"
myNewBoundColumn.HeaderText = "This is a test header"
Grid1.Columns.AddAt(1, myNewBoundColumn)
You can use Add and AddAt to determine where the column must be added. Don't forget to call this code before DataBind or your column won't show up.
Cheers,
Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
|
|

July 28th, 2005, 01:51 PM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
Aaron, on rereading this thread, I realized the AS option was exactly what you proposed. I somehow misunderstood what you were saying so I came up with an identical solution.
BTW, exarkuun, does AutoGenerateColumns="True" do the trick for you?
Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
While typing this post, I was listening to: You Think I Ain't Worth A Dollar, But I Feel Like A Millionaire by Queens of the Stone Age (Track 2 from the album: Songs For The Deaf) What's This?
|
|

July 29th, 2005, 11:05 AM
|
|
Authorized User
|
|
Join Date: Nov 2003
Posts: 24
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Imar thanks you very much... If we were living in the same city i would pay you a beer, you save my ass because i would never find this solution alone, i didn't know that i could manipulate boundcolumn like this.
François
|
|
 |