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 Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Formatting data in a datagrid template column - how? 2

Status
Not open for further replies.

JulesBos

Programmer
Sep 6, 2006
68
US
Hi all

I've converted a datagrid column to a template column, but I'm not sure how to write the html (I assume it's an IIF statment but not sure) to do the following:

If the value is 1 then display Jan, if the value is 2 then display Feb, if the value is 3 then display Mar, etc up to 12 and Dec.

Can anyone help?

Thanks in advance.
 
Sorry if I'm being stupid, but controls do not have a text property, nor do datagrid columns.

I've tried the following:

Dim dgColumn As DataGridColumn = HedgingDataGrid.Columns(1)
and
Dim dgColumn As Control = HedgingDataGrid.FindControl(1)

In neither of these examples does dgColumn have a text property. Did I misunderstand you? Can you be more specific please?

Thanks
 
If you are using a TemplateColumn then presumably you are using a Control such as a Label. If you use FindControl to find this Label (by passing in it's ID) then ,if you cast it to a Label, you will be able to access it's Text property.


____________________________________________________________
Mark,
[URL unfurl="true"]http://aspnetlibrary.com[/url]

Need help finding an answer? Try the Search Facility or read FAQ222-2244.
 
Sorry, you've really thrown me now. What's the label got to do with it? It's a databound column and I want to change the display of the data not the label?

Again, can you be more specific?
 
OK here goes: firstly the HTML:

Code:
<asp:datagrid id=HedgingDataGrid tabIndex=127 runat="server" Font-Names="Verdana" Font-Size="X-Small" BorderColor="#D9D5D2" AutoGenerateColumns="False" DataSource="<%# HedgingDataSet1 %>" DataMember="Hedging" DataKeyField="HedgingID">
                        <HeaderStyle Font-Bold="True" ForeColor="#D7451A"></HeaderStyle>
                        <Columns>
                            <asp:BoundColumn DataField="Value" SortExpression="Value" HeaderText="Value"></asp:BoundColumn>
                            <asp:TemplateColumn HeaderText="Month">
                                <ItemTemplate>
                                    <asp:Label runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.Month") %>'>
                                    </asp:Label>
                                </ItemTemplate>
                                <EditItemTemplate>
                                    <asp:TextBox runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.Month") %>'>
                                    </asp:TextBox>
                                </EditItemTemplate>
                            </asp:TemplateColumn>
                            <asp:BoundColumn DataField="Year" SortExpression="Year" HeaderText="Year"></asp:BoundColumn>
                            <asp:BoundColumn DataField="Type" SortExpression="Type" HeaderText="Type"></asp:BoundColumn>
                            <asp:ButtonColumn Text="Edit" CommandName="Select"></asp:ButtonColumn>
                            <asp:ButtonColumn Text="Delete" CommandName="Delete"></asp:ButtonColumn>
                            <asp:BoundColumn Visible="False" DataField="HedgingID" SortExpression="HedgingID" HeaderText="HedgingID"></asp:BoundColumn>
                            <asp:BoundColumn Visible="False" DataField="Month" SortExpression="Month" HeaderText="Month"></asp:BoundColumn>
                        </Columns>
                    </asp:datagrid>

Then the rest:

Code:
Private Sub PopulateHedgingDataGrid()
        'firstly we need to declare that there are no hedging rows
        HedgingRows = False
          'this sub populates the csd item data grid on the bottom of the form
        Connection.Open()
        CurrentCSD.RecordID = Me.CSDRecordIDLabel.Text
        'set the data adapter if it's not already been done
        If CurrentHedgingItem.hedgingDASet = False Then
            CurrentHedgingItem.SetDataAdapter()
        End If
        HedgingDataSet1.Clear()
        Dim selectCommandString As String = "SELECT Hedging.HedgingID, Hedging.Value, Hedging.Month, " + _
            "Hedging.Year, Hedging.Type FROM Hedging " + _
            "WHERE (((Hedging.CSDID)=" + CurrentCSD.RecordID + "))"
        CurrentHedgingItem.HedgingSelectCommand.CommandText = selectCommandString
        CurrentHedgingItem.HedgingDataAdapter.Fill(HedgingDataSet1, "Hedging")
        HedgingDataGrid.DataBind()
        Connection.Close()
        'now if the dataset has some rows, then declare that there are hedging rows
        'this is used when the order value is calculated
        Dim rows As DataRow
        Dim noRows As Integer = 0
        For Each rows In HedgingDataSet1.Tables(0).Rows
            noRows = noRows + 1
        Next
        If noRows > 0 Then
            HedgingRows = True
        End If
    End Sub

The column I want to change is Month. Sorry if I misunderstood you!

Thanks in advance.
 
What's the label got to do with it?
The code below is the Label I was referring to.
Code:
<ItemTemplate>
    <asp:Label runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.Month") %>' ID="Label1">
    </asp:Label>
</ItemTemplate>



____________________________________________________________
Mark,
[URL unfurl="true"]http://aspnetlibrary.com[/url]

Need help finding an answer? Try the Search Facility or read FAQ222-2244.
 
OK, still not 100% sure what you mean. I've given it the ID "MonthLabel" and added the following code:

Code:
Private Sub HedgingDataGrid_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles HedgingDataGrid.ItemDataBound
        Dim MonthLabel As Label = HedgingDataGrid.FindControl("MonthLabel")
        MonthLabel.Text = "Jan"
    End Sub

[\Code]

I realise I will have to add the if else statements, but I just want to see if I can change the text to something.

If I run this I get an Object reference not set to an instance of an object error at the MonthLabel.Text = "Jan" line.

I apologise if I'm not getting this, but I'm new to .Net and more detailed answers are sometimes a bit more helpful to use newbies.

Thanks
 
Code:
Private Sub HedgingDataGrid_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles HedgingDataGrid.ItemDataBound
[b]If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem Then[/b]
        Dim MonthLabel As Label = HedgingDataGrid.FindControl("MonthLabel")
        MonthLabel.Text = "Jan"
    End Sub
End If
 
Try this:
Code:
    Protected Sub HedgingDataGrid_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles HedgingDataGrid.ItemDataBound
        Select Case e.Item.ItemType
            Case ListItemType.AlternatingItem, ListItemType.Item
                Dim MonthLabel As Label = e.Item.FindControl("MonthLabel")
                MonthLabel.Text = "Jan"
        End Select
    End Sub


____________________________________________________________
Mark,
[URL unfurl="true"]http://aspnetlibrary.com[/url]

Need help finding an answer? Try the Search Facility or read FAQ222-2244.
 
That's it! Sorted. Thanks very much for your help. I'm sorry I took so long to get it!
 
Yes you are correct .. just quickly cut and pasted.. good catch... :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top