Click to See Complete Forum and Search --> : Datagrid problem
Cin0s3
May 12th, 2003, 02:14 PM
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:
<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'.
hellswraith
May 12th, 2003, 02:40 PM
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.
Cin0s3
May 19th, 2003, 01:45 PM
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.
<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>
hellswraith
May 19th, 2003, 04:01 PM
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:
// 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.Net
' Get the unique value of the row of the button the user clicked
Dim pkCell As TableCell = e.Item.Cells(0)
Dim theValueOfTheCell As String = pkCell.Text
' Do your processing based on that value here.
Cin0s3
May 19th, 2003, 05:35 PM
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.
<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.
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.