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

Datagrid View display

Status
Not open for further replies.

mHos

Programmer
Mar 3, 2003
19
US
I am working with the DataGridView in VB.Net2005. I am having problems with the way that it loads and displays the data. I have added it to my form and set several properties at design time and runtime, however the column and row headers are not displaying properly when I set my datasource and the data loads twice before it displays, causing a huge load time (with large numbers of rows). Here are my questions:

1. Why does the datagridview load the data twice when I set dgv.Datasource = datatable? Is there a better way to load all of my data? Is there a setting I can set to keep this from happening? This is causing a horrible load time when I have several thousand records, and this is a small set for me.

2. Why won't the column headers use the defaultColumnHeaderStyle? I have tried setting values for this both at design time and runtime and I still cannot get it to work. I would like my column headers alignment = centered, but it will only display left justified. I have tried catching several events and putting code in, but still nothing will change it.

3. Why won't the row headers display their value? I need to display the row number in the row header and so I have tried setting the row header value and the changing the forecolor, but still the value will not be displayed.

Thanks,
mHos

 
I may not answer all your questions but its a start..

I find it easier to create the columns in the new datagridview control, I had a lot of trouble with it when I migrated from vb2002. I will paste a method which I use to create the columns that I am sure you will find useful. If you worry about the cost implications of a large dataset then it might be worth loading the top 1 or 2 thousand records and then if need be lookup the next batch when required. Implement a filter on your datatable.

Method.. (I've pasted exactly as-is from my project so you may need to study it well!!!)

Code:
Private Sub BindDataAndInitializeColumns()
 
        With DataGridView1
            .Columns.Clear()
 
            .AutoGenerateColumns = False
            .DataSource = ds.Tables("QUOTE_HEADER")
            .AllowUserToAddRows = False
            .AllowUserToDeleteRows = False
            .AllowUserToOrderColumns = True
            .AllowUserToResizeColumns = True
            .AllowUserToResizeRows = True
            .SelectionMode = DataGridViewSelectionMode.FullRowSelect
 
            Dim dc As DataGridViewTextBoxColumn
            Try
                dc = New DataGridViewTextBoxColumn
                dc.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
                dc.Frozen = True
                dc.HeaderText = "Quote ID"
                dc.Width = 75
                dc.Name = "QUOTE_ID"
                dc.DataPropertyName = "QUOTE_ID"
                dc.ReadOnly = True
                .Columns.Add(dc)
 

                dc = New DataGridViewTextBoxColumn
                dc.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
                dc.HeaderText = "QUOTE_UPDATED"
                dc.Width = 20
                dc.Visible = False
                dc.Name = "QUOTE_UPDATED"
                dc.DataPropertyName = "QUOTE_UPDATED"
                dc.ReadOnly = True
                .Columns.Add(dc)
 
                dc = New DataGridViewTextBoxColumn
                dc.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
                dc.HeaderText = "Date"
                dc.Width = 95
                dc.Name = "QUOTE_DATE"
                dc.DataPropertyName = "QUOTE_DATE"
                dc.DefaultCellStyle.Format = "dd/MM/yyyy"
                dc.ReadOnly = True
                dc.DataPropertyName = "QUOTE_DATE"
                .Columns.Add(dc)
 
                dc = New DataGridViewTextBoxColumn
                dc.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
                dc.HeaderText = "Chased"
                dc.Width = 100
 
                dc.Name = "Chased"
                'dc.DefaultCellStyle.ForeColor = Color.Red
                ' dc.DefaultCellStyle.Font = New Font(DataGridView1.Font, FontStyle.Bold)
                dc.ReadOnly = True
                dc.DataPropertyName = "BeenChased"
                .Columns.Add(dc)
 
                dc = New DataGridViewTextBoxColumn
                dc.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
                dc.HeaderText = "Hold"
                dc.Name = "Hold"
                dc.Width = 70
                dc.ReadOnly = True
                dc.DataPropertyName = "OnHold"
                .Columns.Add(dc)
 

                dc = New DataGridViewTextBoxColumn
                dc.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleLeft
                dc.HeaderText = "Hidden Notes"
                dc.Name = "DEL_NAME"
                dc.AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill
                'If Me.chkShowHiddenInfo.Checked = False Then
                dc.Visible = Me.chkShowHiddenInfo.Checked
                'End If
                dc.FillWeight = 10
                dc.ReadOnly = True
                dc.DataPropertyName = "DEL_NAME"
                .Columns.Add(dc)
 
 
 
                dc = New DataGridViewTextBoxColumn
                dc.HeaderText = "Inc Print"
                dc.Name = "PrintItems"
                dc.Width = 50
                'If Me.chkShowContainsPrintItems.Checked = False Then
                dc.Visible = Me.chkShowContainsPrintItems.Checked
                'End If
                dc.ReadOnly = True
                dc.DataPropertyName = "PrintItems"
                .Columns.Add(dc)
 

                Dim bc As DataGridViewButtonColumn
                bc = New DataGridViewButtonColumn
                bc.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
                bc.HeaderText = "Open"
                bc.Width = 75
                bc.Name = "Open"
                bc.Text = "Open/Edit"
                bc.UseColumnTextForButtonValue = True
                .Columns.Add(bc)
 
                bc = New DataGridViewButtonColumn
                bc.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
                bc.HeaderText = "Print"
                bc.Name = "Print"
                bc.Width = 75
                bc.UseColumnTextForButtonValue = True
                bc.Text = "Print"
                .Columns.Add(bc)
 
                dc = New DataGridViewTextBoxColumn
                dc.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleLeft
                dc.HeaderText = "Company Name"
                dc.Name = "COMPANY_NAME"
                dc.AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill
                dc.FillWeight = 20
                dc.ReadOnly = True
                dc.DataPropertyName = "COMPANY_NAME"
                .Columns.Add(dc)
 
                dc = New DataGridViewTextBoxColumn
                dc.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleLeft
                dc.HeaderText = "USERNAME"
                dc.Width = 0
                dc.Visible = False
                dc.Name = "USERNAME"
                dc.ReadOnly = True
                dc.DataPropertyName = "USERNAME"
                .Columns.Add(dc)
 
            Catch ex As Exception
                MessageBox.Show(ex.ToString)
            End Try
            '.Columns("CustomerID").Visible = False
        End With
 
    End Sub

Kind Regards.


 
Keyth--

Thanks for the Code. I decided to do my columns this way, and it does load faster. I have found that for some Unknown reason when I set
dgv.DataSource = myDataTable​
it binds the data twice. Weird.

Also, I figured out that my columns were being centered before, but it just didn't look like it because the column header left room for a sorting glyph on the end, so it appeared to be left justified when it wasn't.

I still would like some help to easily and quickly get the row number to appear in the row header ... I still haven't got that figure out.

Thanks,
mHos
 
I still would like some help to easily and quickly get the row number to appear in the row header ... I still haven't got that figure out.
Give this a try:
Code:
        Dim dc As DataGridViewTextBoxColumn

        dc = New DataGridViewTextBoxColumn
        dc.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
        dc.Frozen = True
        dc.HeaderText = "My Row Number"
        dc.Width = 75
        dc.Name = "Row_Num"
        dc.DataPropertyName = "Row_Num"
        dc.ReadOnly = True
        DataGridView1.Columns.Insert(0, dc)

        Dim i As Integer
        For i = 0 To DataGridView1.RowCount - 1
            DataGridView1.Rows(i).Cells("Row_Num").Value = i + 1
        Next

    End Sub

Jim
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top