Results 1 to 5 of 5

Thread: Datagrid problem

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jan 2001
    Location
    Worcester, MA
    Posts
    77

    Datagrid problem

    I'm trying to allow my users to decide what rows from the database they want to see in the datagrid so I have a page where they can set that, but when I try to read those values from the database and try to set the columns to those values I get an error. This is how I am doing it:
    Code:
    <asp:BoundColumn DataField="<%=Session("sys_header1")%>" SortExpression="<%=Session("sys_header1")%> asc" HeaderText="<%=Session("header1")%>">
    									<HeaderStyle Font-Size="XX-Small" Font-Names="Microsoft Sans Serif" Font-Bold="True" Wrap="False"></HeaderStyle>
    									<ItemStyle Font-Size="XX-Small" Font-Names="MS Sans Serif" Wrap="False" HorizontalAlign="Center"></ItemStyle>
    								</asp:BoundColumn>

    But I get this error: Parser Error Message: Literal content ('<asp:BoundColumn DataField="') is not allowed within a 'System.Web.UI.WebControls.DataGridColumnCollection'.
    "Find all you need in your mind if you take the time" -DT

  2. #2
    PowerPoster hellswraith's Avatar
    Join Date
    Jul 2002
    Location
    Washington St.
    Posts
    2,464
    I think you will have to set it to be a template column. Then put a label control inside it, and use the databinder to databind the column to the labels text property.

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Jan 2001
    Location
    Worcester, MA
    Posts
    77
    That worked pretty good thx!!! Any idea how to set the command argument on the link button to a variable though? I cant use the code behind because it doesnt register a withevents event for it. I tried a session variable and that didnt go.

    Code:
    <asp:TemplateColumn>					<HeaderTemplate>
    	<asp:linkbutton id="Linkbutton1" runat="server" CommandName="sort">	<%#Session("header1")%></asp:linkbutton>
    	</HeaderTemplate>
    	<ItemTemplate>
    	<%#Container.dataItem(Session("sys_header1"))%>
    	</ItemTemplate>
    </asp:TemplateColumn>
    "Find all you need in your mind if you take the time" -DT

  4. #4
    PowerPoster hellswraith's Avatar
    Join Date
    Jul 2002
    Location
    Washington St.
    Posts
    2,464
    Not sure what you mean...

    If you have a link button column, you can use the ItemCommand event of the datagrid itself.

    In that event, you can use the argument that is passed in as 'e' to get what was clicked. Example:

    If you have a few columns, and one is the primary key column (or a unique column) you can use the e.Item.Cells property to get that value. It will assist you in identifying which rows button was clicked:
    PHP Code:
    // C#
    // Get the unique value of the row of the button the user clicked
    TableCell pkCell e.Item.Cells[0];
    string theValueOfTheCell pkCell.Text;

    // Do your processing based on that value here. 
    VB Code:
    1. 'VB.Net
    2. ' Get the unique value of the row of the button the user clicked
    3. Dim pkCell As TableCell = e.Item.Cells(0)
    4. Dim theValueOfTheCell As String = pkCell.Text
    5.  
    6. ' Do your processing based on that value here.

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Jan 2001
    Location
    Worcester, MA
    Posts
    77
    I guess that would work when dealing with the column contents but this is with the header during the sort command. Basically what I am trying to do is set these properties of the link button in the header of a template row so I can sort. THe reason for the variable is the user can change the default column headers for their search results because our database has about 250 fields in it and I only display 10 of them in the grid. So I have a variable for the literal name of the field and then one for the actual sql field name and I need to pass the name of the field for the sort.

    Code:
    <asp:linkbutton
     id="linkbutton1"
     runat="server"
     commandname="sort"
     commandargument=<%#Session("sys_header1")%>
    />
    But it wont let me use that Session("sys_header1") as the commandargument.
    "Find all you need in your mind if you take the time" -DT

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width