locked
DataGrid column filtering...? RRS feed

  • Question

  • Hello,

     

    I have DataTable with 7 columns and I want to show 5 of these 7 columns in a datagrid. How can I do this?

     

    Thanks a lot.

    Friday, June 30, 2006 10:20 AM

Answers

  • I may have misunderstood what you are asking, so if this doesn't suit your needs just ignore it.  Otherwise,

    you just build the datagrid and only show those columns you want.  Here is some sample code, hope this helps.

    Dim strSQL, strErrorMessage As String

    Dim dgContactsTableStyle As New DataGridTableStyle

    Dim dgContactsLNameCol As New DataGridTextBoxColumn

    Dim dgContactsFNameCol As New DataGridTextBoxColumn

    Dim dgContactsPhoneCol As New DataGridTextBoxColumn

    Try

        strErrorMessage = "Contact data cannot be accessed. Contact your System Administrator."

        'build SQL Statement

        strSQL = "Select * From Contacts Where AccountNumber = '" & strAccountNumber & "' AND Status NOT LIKE 'Inactive' ORDER BY ContactLName"

        dsContacts = LocalData.SetupDataSet(strSQL, "Contacts", cstrSQLCeFileName)

        dtContacts = dsContacts.Tables("Contacts")

    If dtContacts.Rows.Count = 0 Then

        lblNoEntriesFound.Visible = True

        dgContacts.Visible = False

        'tlbTelephone.Visible = False

        mnuContact.Enabled = False

    Else

        lblNoEntriesFound.Visible = False

        dgContacts.Visible = True

        mnuContact.Enabled = True

        dgContacts.Font = New System.Drawing.Font("Microsoft Sans Serif", 10.0F, Drawing.FontStyle.Regular)

        dgContacts.RowHeadersVisible = False

        'fill datagrid

        dgContacts.DataSource = dtContacts

        dgContactsTableStyle.MappingName = "Contacts"

        dgContactsLNameCol.MappingName = "ContactLName"

        dgContactsLNameCol.HeaderText = "Last Name"

        dgContactsLNameCol.Width = 84

        dgContactsTableStyle.GridColumnStyles.Add(dgContactsLNameCol)

        dgContactsFNameCol.MappingName = "ContactFName"

        dgContactsFNameCol.HeaderText = "First"

        dgContactsFNameCol.Width = 56

        dgContactsTableStyle.GridColumnStyles.Add(dgContactsFNameCol)

        dgContactsPhoneCol.MappingName = "ContactPhone"

        dgContactsPhoneCol.HeaderText = "Phone"

       dgContactsPhoneCol.Width = 156

       dgContactsTableStyle.GridColumnStyles.Add(dgContactsPhoneCol)

       dgContacts.TableStyles.Clear()

        dgContacts.TableStyles.Add(dgContactsTableStyle)

        dgContacts.Focus()

    End If

        Me.Show()

        'Catch ex As SqlCeException

        ' MessageBox.Show(ex.Message)

    Catch ex As Exception

        MessageBox.Show(ex.Message)

        'MessageBox.Show(strErrorMessage)

    End Try

    Friday, June 30, 2006 1:12 PM