PDA

Click to See Complete Forum and Search --> : problem with column width in datagrid


Borry
Oct 30th, 2002, 07:25 AM
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">
<asp:DataGrid 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>
</asp:DataGrid>
</form>
</body>
</HTML>



Thanks,


Bjorn

pvb
Oct 30th, 2002, 10:55 AM
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.

<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:


Protected Authors As DataGrid
Protected TotalRows As Label
Protected TotalCols As Label

Private Sub Page_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
If Not Page.IsPostBack Then
bindDataGrid()
TotalRows.Text = CType(Authors.DataSource, DataSet).Tables(0).Rows.Count.ToString()
TotalCols.Text = CType(Authors.DataSource, DataSet).Tables(0).Columns.Count.ToString()
End If
End Sub

Private Sub bindDataGrid()
Dim connString As String = AppSettings.Item("DBConnString")
Dim cn As New SqlConnection(connString)
Dim cmdText As String = "Select * From Authors"
Dim cmd As New SqlCommand(cmdText, cn)
Dim ds As New DataSet("Authors")
Dim da As SqlDataAdapter
cmd.CommandType = CommandType.Text

Try
da = New SqlDataAdapter(cmd)
da.Fill(ds)
Authors.DataSource = ds
Authors.DataBind()
Catch sqlEx As SqlException
Response.Write("SQL Exception -> " & sqlEx.Message)
Catch ex As Exception
Response.Write("Unhandled Exception -> " & ex.Message)
Finally
If Not cn Is Nothing Then
If cn.State <> ConnectionState.Closed Then cn.Close()
cn.Dispose()
End If
End Try
End Sub

Borry
Oct 31st, 2002, 02:06 AM
Thanks for your answer.

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

Thanks a lot,


Bjorn

pvb
Oct 31st, 2002, 09:04 AM
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.