Link to home
Create AccountLog in
Avatar of Wildone63
Wildone63

asked on

Format GridView

I am using Visual Studio VB.net 2008.

I have an aspx page with a gridview that I am populating at run time like this

        Dim dbadp As New OdbcDataAdapter(myquery, mycs)
        Dim dTable As New DataTable
        dbadp.Fill(dTable)
        dbadp.Dispose()
        GridView1.DataSource = dTable
        GridView1.DataBind()

Is there a way to format the columns at run time as well?

Thanks
Avatar of s_chilkury
s_chilkury
Flag of United States of America image

Avatar of slightwv (䄆 Netminder)
slightwv (䄆 Netminder)

Just to clarify:  you want a different format for the cells as you bind the data?
You can change everything on Page Load

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        GridView1.Columns(0).HeaderText = "New Column Name"
        GridView1.Columns(0).HeaderStyle = ...
    End Sub

Open in new window

Avatar of Wildone63

ASKER

I tried to set this on page load

        GridView1.DataBind()
        GridView1.Columns(1).HeaderText = "Item"
        GridView1.Columns(2).HeaderText = "Descrip"

But I get this error
Index was out of range. Must be non-negative and less than the size of the collection.
Why try to set heading on page load?  Set it in the bound column template for the grid.
Wildone.  Make sure the columns already exist.    The first column would be GridView1.Columns(0).HeaderText = "Item"

What you can do to keep the format on each column is after a Databind or in an event that you need do this
If me.Gridview1.Created=True then
  if Me.DataGridView1.ColumnCount>0 then
           Me.DataGridView1.Columns(0).HeaderText="Id"
      Me.DataGridView1.Columns(0).DefaultCellStyle.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleRight
        Me.DataGridView1.Columns(0).DefaultCellStyle.Format = "C"
        'Rest of the properties that you want to set by columns
   End If
End If


k-designers:

This is right on track for what I am trying to do but....

I get the error:

Created is not a member of 'System.Web.UI.Webcontrols.GridView'
And ColumnCount is not a member of System.Web.UI.WebControls.GridView...

Here is my Code...

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Label1.Text = System.DateTime.Now.ToLongDateString
        Dim sitem As String = Session("srcItem").ToString.ToUpper
        Dim myquery As String = "Select Administrator.ITEMS.FULL_NAME as 'ITEM', Administrator.ITEMS.SALESDESCRIPTION as 'Description',  Administrator.BIN_NUMBER.BIN_NUMBER as 'Bin', administrator.BIN_NUMBER.ON_HAND_COUNT as 'On Hand' FROM Administrator.ITEMS LEFT OUTER JOIN Administrator.BIN_NUMBER ON Administrator.BIN_NUMBER.ITEM_ID = Administrator.ITEMS.ITEM_ID where Administrator.BIN_NUMBER.ON_HAND_COUNT > 0 and Administrator.ITEMS.FULL_NAME  LIKE " + "'" + sitem + "%" + "'"
        Dim mycs As String = "Dsn=NS.com;uid=userid;pwd=password"
        Dim dbadp As New OdbcDataAdapter(myquery, mycs)
        Dim dTable As New DataTable
        dbadp.Fill(dTable)
        dbadp.Dispose()
        GridView1.DataSource = dTable
        GridView1.DataBind()

        If Me.GridView1.Created = True Then
            If Me.GridView1.ColumnCount > 0 Then
                Me.GridView1.Columns(0).HeaderText = "Id"
                Me.GridView1.Columns(0).DefaultCellStyle.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleRight
                Me.GridView1.Columns(0).DefaultCellStyle.Format = "C"
                'Rest of the properties that you want to set by columns
            End If
        End If

        myquery = ""
        sitem = ""


    End Sub
Sorry, I'll post the the web version if it. I thought that was a windows app
After the GridView1.DataBind() put this

If Me.GridView1.Columns.Count > 0 Then
                Me.GridView1.Columns(6).ItemStyle.Width = 120
                Me.GridView1.Columns(6).ItemStyle.HorizontalAlign = HorizontalAlign.Right
 End If
k-designers: I added what you said. I am not getting any errors now but... Nothing is changing. I changed the Columns(6) to 0 and then to 1 (trying to change the first column...

Here is my code.

        If Me.GridView1.Columns.Count > 0 Then
            Me.GridView1.Columns(1).ItemStyle.Width = 120
            Me.GridView1.Columns(1).ItemStyle.HorizontalAlign = HorizontalAlign.Right
            Me.GridView1.Columns(1).HeaderText = "Items"

        End If

Thank You!
You got it my friend
k-designers

I guess I am confused... I made the changes... And I am not getting any errors, but the changes I am trying to make with the code for example is simply to change the header text... I added

            Me.GridView1.Columns(1).HeaderText = "Items"

but the header text did not change.
Let me review it
Hi!

Did you find the property EnableHeadersVisualStyles in the datagridview ???
Set it to false.
There are a lot of comments about the ASP.NET GridView, but I would like to find out where you are, and what you still need help with.
Hello,


I am trying to make with the code for example is simply to change the header text... I added

            Me.GridView1.Columns(1).HeaderText = "Items"

but the header text did not change.

Here is the code I am trying to use. None of the changes I am trying to make are coming across when I run the page.

        If Me.GridView1.Columns.Count > 0 Then
            Me.GridView1.Columns(1).ItemStyle.Width = 120
            Me.GridView1.Columns(1).ItemStyle.HorizontalAlign = HorizontalAlign.Right
            Me.GridView1.Columns(1).HeaderText = "Items"

        End If

ASKER CERTIFIED SOLUTION
Avatar of slightwv (䄆 Netminder)
slightwv (䄆 Netminder)

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
What might be happening is that the changes that you make in the code-behind, are getting lost...
Thank You.