Results 1 to 5 of 5

Thread: Datagrid Columns

  1. #1

    Thread Starter
    Fanatic Member carlblanchard's Avatar
    Join Date
    Sep 2003
    Location
    Bournemouth (UK)
    Posts
    539

    Datagrid Columns

    Hi all, anyone who can do this please help its been driving me crazy for ages

    I want to be able to define the width of the column
    ie column(1).width = 100 and so on i know its not the right syntax thats what i cant do....

    this is my code i wana do it on

    Dim dtePicker As Date
    dteOrgDate = Me.MonthCalendar1.SelectionEnd
    Dim intDay As Integer = dteOrgDate.Day
    Dim intMth As Integer = dteOrgDate.Month
    Dim intYr As Integer = dteOrgDate.Year
    dteChaseDate = intMth & "/" & intDay & "/" & intYr

    Dim data_adapter1, data_adapter2 As SqlDataAdapter
    Dim strSql1 As String = "SELECT AppointmentTime as Time, CustomerName as Task FROM Appointments WHERE AppointmentDate = '" & dteChaseDate & "' AND AdvisorId = " & IdList(Me.cboEmployee.SelectedIndex) & " ORDER BY AppointmentTime"

    data_adapter1 = New SqlDataAdapter(strSql1, MyConnection) ' Create the SqlDataAdapter.
    data_adapter1.TableMappings.Add("Table", "MyCalendar")
    m_DataSet1 = New DataSet ' Fill the DataSet.
    data_adapter1.Fill(m_DataSet1)

    DataGrid1.DataSource = m_DataSet1.Tables("MyCalendar") ' Bind the DataGrid control to the Contacts DataTable.
    DataGrid1.ReadOnly = True


    thanks all
    I am curretly building a defect management system for software and web developers,
    If you wana try it out (beta test) and keep it for free just send me a message

  2. #2
    Fanatic Member VBCrazyCoder's Avatar
    Join Date
    Apr 2003
    Posts
    681
    You need to implement CustomTableStyles for the datagrid, and then you can set the length, etc of each of the columns.

  3. #3

    Thread Starter
    Fanatic Member carlblanchard's Avatar
    Join Date
    Sep 2003
    Location
    Bournemouth (UK)
    Posts
    539
    could u show me how with above code cos ive tired 100 times and cant seem to get it working
    I am curretly building a defect management system for software and web developers,
    If you wana try it out (beta test) and keep it for free just send me a message

  4. #4
    Fanatic Member VBCrazyCoder's Avatar
    Join Date
    Apr 2003
    Posts
    681
    Before you set your datasource for the grid, call this method (which is a sample for setting the width of a column named "SomeColumnName":
    VB Code:
    1. Private Sub AddCustomDataTableStyles(ByVal dstData As DataSet)
    2.  
    3.         'Clear the table styles for the datagrid
    4.             DataGrid1.TableStyles.Clear()
    5.  
    6.             'Declare local variables
    7.             Dim tsTableStyle As New DataGridTableStyle
    8.             Dim tcTextCol As DataGridTextBoxColumn
    9.             Dim intCounter As Integer
    10.  
    11.             'Map the table style to the table in the dataset
    12.             tsTableStyle.MappingName = dstData.Tables(0).TableName()
    13.  
    14.             'Add textbox column style for each column in the dataset
    15.             For intCounter = 0 To dstData.Tables(0).Columns.Count() - 1
    16.  
    17.                 'Reinstantiate the text box column and set mappings and attributes
    18.                 tcTextCol = New DataGridTextBoxColumn
    19.                 tcTextCol.MappingName = dstData.Tables(0).Columns.Item(intCounter).ColumnName
    20.                 tcTextCol.HeaderText = dstData.Tables(0).Columns.Item(intCounter).ColumnName
    21.  
    22.                 'Set column size based on name
    23.                 Select Case tcTextCol.MappingName()
    24.                     Case "SomeColumnName"
    25.                         tcTextCol.Width() = 100
    26.                     Case Else
    27.                         tcTextCol.Width() = 50
    28.                 End Select
    29.  
    30.                 'Add the column to the table style
    31.                 tsTableStyle.GridColumnStyles.Add(tcTextCol)
    32.             Next
    33.  
    34.             'Add the table styles to the datagrid
    35.             DataGrid1.TableStyles.Add(tsTableStyle)
    36. End Sub

  5. #5

    Thread Starter
    Fanatic Member carlblanchard's Avatar
    Join Date
    Sep 2003
    Location
    Bournemouth (UK)
    Posts
    539
    Thanks Buddy that works great
    I am curretly building a defect management system for software and web developers,
    If you wana try it out (beta test) and keep it for free just send me a message

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