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

Formatting Unbound Datagrid Columns

Status
Not open for further replies.

sila

Technical User
Aug 8, 2003
76
GB
Hi

Can anyone tell me if I can do centering on a datagrid column when the datagrid does not use bound columns?

Thanks

 
You can do it when an item is created using something like:
Code:
e.Item.Cells(0).Style.Item("TEXT-ALIGN") = "CENTER"

--------------------------------------------------------------------------------------------------------------------------------------------

Need help finding an answer?

Try the search facility ( or read FAQ222-2244 on how to get better results.
 
Code:
Private Sub dgBudget_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles dgBudget.ItemDataBound
    Dim lit As ListItemType
    lit = e.Item.ItemType

    For c As Integer = 1 To e.Item.Cells.Count - 1
      If lit = ListItemType.AlternatingItem Or lit = ListItemType.Item Then
        e.Item.Cells(0).HorizontalAlign = HorizontalAlign.Left
        e.Item.Cells(0).Width = System.Web.UI.WebControls.Unit.Pixel(230)
        e.Item.Cells(c).HorizontalAlign = HorizontalAlign.Right
        e.Item.Cells(c).Width = System.Web.UI.WebControls.Unit.Pixel(95)
        e.Item.Cells(c).Text = String.Format("{0:N}", CDec(e.Item.DataItem(c)))
      ElseIf lit = ListItemType.Header Then
        e.Item.Cells(0).HorizontalAlign = HorizontalAlign.Left
        e.Item.Cells(0).Width = System.Web.UI.WebControls.Unit.Pixel(230)
        e.Item.Cells(c).HorizontalAlign = HorizontalAlign.Right
        e.Item.Cells(c).Width = System.Web.UI.WebControls.Unit.Pixel(95)
      ElseIf lit = ListItemType.Footer Then
        e.Item.Cells(0).Text = "** Budget Totals **"
         e.Item.Cells(c).HorizontalAlign = HorizontalAlign.Right
        e.Item.Cells(0).Width = System.Web.UI.WebControls.Unit.Pixel(230)
        e.Item.Cells(c).Width = System.Web.UI.WebControls.Unit.Pixel(95)
      End If
    Next
  End Sub

This is something that I use, because I don't know how many columns can be returned...the first column is always the same, that's why I specifically give that one different formatting...

"...we both know I'm training to become a cagefighter...see what happens if you try 'n hit me..."
 
Sorry, I'm not too experienced at the vb side.
I'm not quite sure how to use this. I only need to center one column (the forth one along, counting first as 0). The column will be called Winner - created via the stored procedure, and will have an asterisk in it.

Thanks (and apologies for ignorance!)
 
Add my code to the ItemDataBound event and change the zero to a 3

--------------------------------------------------------------------------------------------------------------------------------------------

Need help finding an answer?

Try the search facility ( or read FAQ222-2244 on how to get better results.
 
in that case you should be able to simply go into the HTML of your datagrid...

Code:
<asp:BoundColumn DataField="winner" HeaderText="Win">
<HeaderStyle Font-Size="11px" Font-Names="Verdana" Font-Bold="True" HorizontalAlign="Center"></HeaderStyle>
<ItemStyle Font-Size="11px" Font-Names="Verdana" HorizontalAlign="Center" VerticalAlign="Middle"></ItemStyle>
</asp:BoundColumn>

"...we both know I'm training to become a cagefighter...see what happens if you try 'n hit me..."
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top