Results 1 to 4 of 4

Thread: problem with column width in datagrid

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Sep 2002
    Location
    Belgium
    Posts
    99

    problem with column width in datagrid

    Hello everybody,


    I'm binding a datagrid to a datareader. This works awesome. But now I'm trying to achieve the following :
    I want to make my columns smaller then the standard. My columns aren't bound to anything.
    This is what I try, but nothing seems to work, cause when I do a columncount from my datagrid, it always returns 0... but when I run the asp page, I see all of my columns.

    Private Sub LoadData()

    Dim dr As SqlDataReader
    Dim oCommand As New SqlCommand()
    Dim oConnection As New SqlConnection()

    Dim strSQL As String = "SELECT wp.Afdeling, wp.WerkPostTitel, pg.Naam, pg.Voornaam FROM t" & _
    "blWerkpostPerPersoon wpp INNER JOIN tblWerkposten wp ON wpp.WerkPostNr = wp.Werk" & _
    "PostNr INNER JOIN PersoneelsGegevens.dbo.tblPersGeg pg ON pg.StamNr = wpp.StamNr" & _
    " WHERE (wp.Archief = 0) AND (wpp.Archief = 0) ORDER BY wp.Afdeling, wpp.WerkPost" & _
    "Nr, pg.Naam"

    'Wijs de connectiestring toe aan het nieuw connectieobject
    oConnection.ConnectionString = strConnection
    'Wijs de query toe aan het nieuwe command object
    oCommand.CommandText = strSQL

    'Wijs de connectie property van het command object toe aan het connectie object
    oCommand.Connection = oConnection

    'Open de connectie
    oConnection.Open()

    'Vul de reader op met de gegevens van het command object
    dr = oCommand.ExecuteReader

    'Bind de datagrid aan de datareader
    dgResults.DataSource = dr
    dgResults.DataBind()

    'dgResults.Columns().ItemStyle.Width = Unit.Pixel(20)

    'Sluit de datareader en de dbase connectie af.
    dr.Close()
    oConnection.Close()

    End Sub

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Response.Write(dgResults.Columns.Count)
    LoadData()
    'Label1.Text = dgResults.Columns.Count

    End Sub
    I really hope someone could help me out !!!!

    On my html page :


    <%@ Page Language="vb" AutoEventWireup="false" Codebehind="default.aspx.vb" Inherits="Arbeiders.WebForm1"%>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
    <HTML>
    <HEAD>
    <title>WebForm1</title>
    <meta name="GENERATOR" content="Microsoft Visual Studio.NET 7.0">
    <meta name="CODE_LANGUAGE" content="Visual Basic 7.0">
    <meta name="vs_defaultClientScript" content="JavaScript">
    <meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
    </HEAD>
    <body MS_POSITIONING="GridLayout" bgColor="#ffffff">
    <form id="Form1" method="post" runat="server">
    <aspataGrid id="dgResults" style="Z-INDEX: 101; LEFT: 16px; POSITION: absolute; TOP: 108px" runat="server" Height="150px" Width="760px" CellPadding="4" BackColor="White" BorderWidth="1px" BorderStyle="None" BorderColor="#CC9966">
    <SelectedItemStyle Font-Bold="True" ForeColor="#663399" BackColor="#FFCC66"></SelectedItemStyle>
    <ItemStyle Wrap="False" ForeColor="#330099" BackColor="White"></ItemStyle>
    <HeaderStyle Font-Bold="True" ForeColor="#FFFFCC" BackColor="#990000"></HeaderStyle>
    <FooterStyle ForeColor="#330099" BackColor="#FFFFCC"></FooterStyle>
    <PagerStyle HorizontalAlign="Center" ForeColor="#330099" BackColor="#FFFFCC"></PagerStyle>
    </aspataGrid>
    </form>
    </body>
    </HTML>

    Thanks,


    Bjorn

  2. #2
    Hyperactive Member
    Join Date
    Aug 2002
    Location
    Fort Collins, CO
    Posts
    366
    Well there's a few different ways of doing this, one way is setting the "AutoGenerateColumns" property of the DataGrid control to "False", then specify each column's property, like width etc.
    Code:
    <form id="Form1" method="post" runat="server">
    	Total Rows: <asp:Label Runat="server" ID="TotalRows"/><br/>
    	Total Cols: <asp:Label Runat="server" ID="TotalCols"/><br/>
    	<asp:DataGrid
    		id="Authors"
    		runat="server"
    		style="Z-INDEX: 101; LEFT: 16px; POSITION: absolute; TOP: 108px"			
    		CellPadding="4" 
    		BackColor="White" 
    		BorderWidth="1px" 
    		BorderStyle="None" 
    		AutoGenerateColumns="False"
    		BorderColor="#CC9966">
    		<SelectedItemStyle Font-Bold="True" ForeColor="#663399" BackColor="#FFCC66"></SelectedItemStyle>
    		<ItemStyle Wrap="False" ForeColor="#330099" BackColor="White"></ItemStyle>
    		<HeaderStyle Font-Bold="True" ForeColor="#FFFFCC" BackColor="#990000"></HeaderStyle>
    		<FooterStyle ForeColor="#330099" BackColor="#FFFFCC"></FooterStyle>
    		<PagerStyle HorizontalAlign="Center" ForeColor="#330099" BackColor="#FFFFCC"></PagerStyle>
    		<Columns>
    			<asp:BoundColumn 
    				HeaderText="First Name"
    				DataField="au_fname"
    				ItemStyle-Width="100px"/>
    			<asp:BoundColumn
    				HeaderText="Last Name"
    				DataField="au_lname"
    				ItemStyle-Width="100px"/>
    		</Columns>
    	</asp:DataGrid>
    </form>
    and then if you use the dataset you can easily get the row and col counts:

    VB Code:
    1. Protected Authors As DataGrid
    2.     Protected TotalRows As Label
    3.     Protected TotalCols As Label
    4.  
    5.     Private Sub Page_Load(ByVal sender As System.Object, _
    6.                           ByVal e As System.EventArgs) Handles MyBase.Load
    7.         If Not Page.IsPostBack Then
    8.             bindDataGrid()
    9.             TotalRows.Text = CType(Authors.DataSource, DataSet).Tables(0).Rows.Count.ToString()
    10.             TotalCols.Text = CType(Authors.DataSource, DataSet).Tables(0).Columns.Count.ToString()
    11.         End If
    12.     End Sub
    13.  
    14.     Private Sub bindDataGrid()
    15.         Dim connString As String = AppSettings.Item("DBConnString")
    16.         Dim cn As New SqlConnection(connString)
    17.         Dim cmdText As String = "Select * From Authors"
    18.         Dim cmd As New SqlCommand(cmdText, cn)
    19.         Dim ds As New DataSet("Authors")
    20.         Dim da As SqlDataAdapter
    21.         cmd.CommandType = CommandType.Text
    22.  
    23.         Try
    24.             da = New SqlDataAdapter(cmd)
    25.             da.Fill(ds)
    26.             Authors.DataSource = ds
    27.             Authors.DataBind()
    28.         Catch sqlEx As SqlException
    29.             Response.Write("SQL Exception -> " & sqlEx.Message)
    30.         Catch ex As Exception
    31.             Response.Write("Unhandled Exception -> " & ex.Message)
    32.         Finally
    33.             If Not cn Is Nothing Then
    34.                 If cn.State <> ConnectionState.Closed Then cn.Close()
    35.                 cn.Dispose()
    36.             End If
    37.         End Try
    38.     End Sub

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Sep 2002
    Location
    Belgium
    Posts
    99
    Thanks for your answer.

    Could there be any reason why it doesn't work with a datareader ?

    Thanks a lot,


    Bjorn

  4. #4
    Hyperactive Member
    Join Date
    Aug 2002
    Location
    Fort Collins, CO
    Posts
    366
    I would venture a guess that it has to do with the nature of the datareader. It's readonly, forwardonly, so the datagrid won't know details about the total columns and rows until it's completely rendered(i.e. it's gone through the entire datareader). You can still use the datareader to load up the datagrid in your example. Unless it's an adhoc query, you already know how many columns you're going to have so you can determine the size of the columns at design time.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width