Wrox Programmer Forums
|
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
 
Old April 8th, 2004, 01:30 PM
Authorized User
 
Join Date: Jan 2004
Posts: 66
Thanks: 0
Thanked 0 Times in 0 Posts
Default datagrid header texts

How are you?

I was wondering, is it possible to dymanically assign the value of a datagrid column's header text?

For example, I have a dropdown list, ddlone and the user selects an option from the list. How do I get ddlone.selecteditem.text to appear as a headertext in a bound column of a datagrid.

Here is the code for my datagrid:

<asp:datagrid id="dgEmps" runat="server" Width="617px" Height="190px" AutoGenerateColumns="False" BackColor="#eeeeee" HorizontalAlign="Center" Font-Bold="True" Font-Name="Verdana" Font-Size="10pt" HeaderStyle-VerticalAlign="Middle" footerstyle-verticalalign="Middle" HeaderStyle-HorizontalAlign="Center" footerstyle-horizontalalign="Center" HeaderStyle-BackColor="#7b87b2" HeaderStyle-height="100px" FooterStyle-height="200px" AlternatingItemStyle-BackColor="white" style="Z-INDEX: 107; LEFT: 176px; POSITION: absolute; TOP: 328px">
    <Columns>
    <asp:BoundColumn DataField="CPT_COMPETENCY_NM" HeaderText="Competency Name" />
    <asp:BoundColumn DataField="EMPLOYEE ONE" HeaderText="XX" ItemStyle-HorizontalAlign="Center" />
    <asp:BoundColumn datafield="EMPLOYEE TWO" HeaderText="XY" ItemStyle-HorizontalAlign="Center" />
    </Columns>
</asp:datagrid>


I have been playing around with column controls and datagrid controls in vb.net today and I dont see anything that is going to let me this.

thanks in advance,

Morris

 
Old April 8th, 2004, 02:15 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,998
Thanks: 0
Thanked 3 Times in 3 Posts
Default

I believe you can dynamically assign a header this way:

dgEmps.Columns(1).HeaderText = ddlOne.selectedItem.Text
 
Old April 8th, 2004, 05:36 PM
Authorized User
 
Join Date: Jan 2004
Posts: 66
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi,

That works! Nice and easy, thanks!

Morris

 
Old April 9th, 2004, 06:34 AM
Authorized User
 
Join Date: Jan 2004
Posts: 66
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi, me again wth another problem,

I am trying to change the names of the columns in a datagrid where I am using autogenerated columns.

While the code posted earlier works with bound columns, is it possible to do the same thing with autogenerated columns? I am getting this error:

Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index

Anu ideas?

Thanks,

Morris

 
Old April 9th, 2004, 01:42 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,998
Thanks: 0
Thanked 3 Times in 3 Posts
Default

Are you trying to change the header before you bind to the data grid? This may cause this. Also, note that every time you rebind to the data grid, you will probably have to reassign the column header.
 
Old April 9th, 2004, 04:04 PM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

Morris,

You are probably trying to this before the grid columns are created. In order to modify the automatically created columns, you'll need to work in the ItemCreated handler for the grid. You can test the current e.Item.ItemType for the DataGrid Header item and then manipulate the column headers at that point.

Peter
-------------------------
Work smarter, not harder
 
Old April 10th, 2004, 12:09 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 996
Thanks: 2
Thanked 11 Times in 11 Posts
Send a message via Yahoo to melvik
Default

use [u]<HeaderStyle></u> in [u]<asp:DataGrid></u> in HTML of ASPX file!

HTH.

Always:),
Hovik Melkomian.
 
Old April 10th, 2004, 08:31 AM
Authorized User
 
Join Date: Jan 2004
Posts: 66
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hey folks,

Firstly, thanks for all the help, it is really appreciated.

Here is the code-behind for my datagrid:
    Function search() 'searches for info about the users and comp types
        Dim sql_compare_str As String
        sql_compare_str = build_sql()

        Dim Review_cmd As System.Data.OleDb.OleDbCommand

        'opening the database connection
        dbconn.Open()
        dgEmps.Visible = True

        Review_cmd = New System.Data.OleDb.OleDbCommand(sql_compare_str, dbconn)
        Dim Review_ds As System.Data.OleDb.OleDbDataReader
        Review_ds = Review_cmd.ExecuteReader()

        dgEmps.DataSource = Review_ds
        dgEmps.DataBind()
        dbconn.Close()

        dgEmps.Columns(1).HeaderText = ddemp1.SelectedItem.Text
        dgEmps.Columns(2).HeaderText = ddemp2.SelectedItem.Text

    End Function

As far as I can make out, I am not trying to add column headings until the data is bound to the datagrid. Should I be doing this in another function?

Morris

 
Old April 10th, 2004, 11:13 AM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

I don't see anything inherently wrong with the way you are doing this. As long as it works this way.
 
Old April 10th, 2004, 11:51 AM
Authorized User
 
Join Date: Jan 2004
Posts: 66
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi,

that is the code which is causing the problem... any ideas?

Morris






Similar Threads
Thread Thread Starter Forum Replies Last Post
Datagrid fixed header Swopna .NET Framework 1.x 1 March 22nd, 2008 11:06 AM
Getting column name/header of a datagrid elygp ASP.NET 1.0 and 1.1 Professional 2 June 6th, 2007 01:07 PM
header text in datagrid msrnivas .NET Web Services 1 March 4th, 2004 01:56 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.