Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

TemplateColumn and ItemTemplate

Status
Not open for further replies.

GavW

Programmer
Jan 13, 2006
58
GB
Hey all!

I was wondering if somebody could help me get my head around the syntax that i need to use for the templatecolumn within a datagrid.

All of the code that i have come across while researching has been the code required for the "HTML" view rather than the codebehind where i would like to implement this.

The problem that i have is with displaying more than one database value within a single cell (ive also come across examples of HTML tables being inserted into a single cell which would be handy). The syntax i have for a boundcolumn is as follows:

BoundColumn views = new BoundColumn();
views.DataField = "Views";
views.DataFormatString = "{0}";
views.HeaderText = "Views";

The problem with this is that i can only display this single value within the cell. I would be able to insert something similiar to the following:

"Number of views: <b>" + views + "</b><br>
Total Number: <b>" + total + "</b>";

Thanks in advance.

GavW
 
I am not sure what you want to do. Can you give some example output of how the grid should look? You can add multiple objects to a datagrid cell.

Jim
 
something along the lines of this:

column1 column2 column3

data1 data2 data3
data4

with data3 and data4 being separate fields in a database and also allowing the user to select them to naviagate to another location, passing strings in the process.

all that i am stuck with is the syntax to use in the codebehind in order to perform this operation. A HyperLinkColumn performs this using the DataNavigateUrlField and DataNavigateUrlFormatString methods but does not allow for multiple objects within the same cell.

GavW
 
This is best left to an ItemDataBound event handler. Typically this happens at the grid level because it's a more cohesive place to put it.

Once in the ItemDataBound event handler, you have access to the e.Item.DataItem and can utilize DataBinder.Eval. Through the DataItem, you have access to whatever column(s) you want and can easily add multiple values to a single column as you're trying to do above (by putting the values into, say, a Literal control you stored in the ItemTemplate via e.Item.Cells).

You would not do it through the properties of the Column, however, because they are geared more toward single values.
 
Alright thanks for the advice. I am not familiar with this method however could you maybe give me an example of what i would need within the ItemDataBound event handler.

GavW
 
The event handler would look something like this:

Code:
    void grid_ItemDataBound(object sender, DataGridItemEventArgs e)
    {
        int columnIndex = 2; 

        //set up myLiteralControl in the markup.
        LiteralControl myLiteral = e.Item.Cells[columnIndex].FindControl("myLiteralControl");
        
        string firstVal = DataBinder.Eval(e.Item.DataItem, "Whatever");
        string secondVal = DataBinder.Eval(e.Item.DataItem, "WhateverElse");

        myLiteral.Text = String.Format(
            @"Number of views: <b>{0}</b><br>
              Total Number: <b>{1}</b>",
             firstVal,
             secondVal );
    }
 
im assuming that i have to establish a literal by the name of myLiteralControl to get this to function properly? Do i then databind this literal to the table that i want my datagrid to display?

GavW

 
The LiteralControl is something you'd put in the DataGrid's ItemTemplate for the appropriate column.

If you don't have an instance of a Control inside the grid, then you won't be able to maintain ViewState and the column will blank out on PostBacks.
 
how do i go about creating this literal control then? I have never come across these before i didn't even realise that they existed before you brought them up :)

GavW
 
Just drag-and-drop from the designer into the ItemTemplate of the appropriate column.

Manually via markup it would look something like this:

<asp:LiteralControl id="literal" runat="server"/>
 
Right ok i think ive kinda got the hang of it now.

Im getting the error:

Cannot implicitly convert type 'System.Web.UI.Control' to 'System.Web.UI.LiteralControl'

The cause of the error is this line:

LiteralControl myLiteral = e.Item.Cells[columnIndex].FindControl("myLiteralControl");

I dragged a literal object into the itemtemplate within my column and named it myLiteralControl. Came up as this within the markup:

<asp:Literal id="myLiteralControl" runat="server"></asp:Literal>

Tried adding "Control" to "Literal" but same error applies. Any suggestions?

GavW
 
LiteralControl myLiteral = (LiteralControl)e.Item.Cells[columnIndex].FindControl("myLiteralControl");
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top