Need help... GridView defaults to all records

Hi everyone,

I setup my data grid and I'm filling it using a dataset from code no (see below). I defined all my columns and pointed them to the database fields and it shows just fine in design time. However when I run it the columns are remapped by the dataset showing all the fields instead of the 5 I picked. I can't find anything like Auto Generate fields as in asp .net. If someone can help I would appreciate it.

thanks
-rjp

Code:

Dim myDataSet As New DataSet

myConnection.Open()

strSQL = "SELECT * FROM LstData ORDER BY ID"

myDataAdapter = New SqlDataAdapter(strSQL, myConnection)
myDataAdapter.Fill(mydataset,
"Listings")
gridListings.DataSource = myDataSet.Tables(
"Listings")

myConnection.Close()



Answer this question

Need help... GridView defaults to all records

  • limelight

    if you don't want to display all the fields then set the column's visible property to false, or you can edit your select command selecting only those that you want to display.

  • Jaros?aw Kowalski

    I understand that however heres whats going on - I setup 5 columns out of a table of 7 fields. I'm selecting all the records because I want the other records to populate txtboxes on the form as you move from record to record in the grid so I can't cut down the sql statement. Ok so I have my 5 columns and in design it looks great. When I run the app it plugs in the other 2 fields. Hence this is my problem...

    -rjp


  • Ankit2008

    If you are using a DataGridView, set AutoGenerateColumns to false

  • David Hubbard

    Please see SqlDataAdapter properties TableMapping,MissingMappingAction ,MissingSchemaAction
  • Roger McKinney

    To hide a column, set its width to zero or use MappingType as per this code sample –

    Dim strCON As String = "<Connection String>"

    Dim strQuery As String = "Select Field1, Field2 from TBL"

    Dim DA As SqlDataAdapter = Nothing

    Dim DS As DataSet = Nothing

    Try

    Dim CON As New SqlConnection(strCON)

    DA = New SqlDataAdapter(strQuery, CON)

    DS = New DataSet

    DA.Fill(DS, "TBL")

    CON.Close()

    Catch ex As Exception

    MsgBox("Error")

    Me.Close()

    Return

    End Try

    'Here you can HIDE the Column

    DS.Tables("TBL").Columns("Field1").ColumnMapping = MappingType.Hidden

    '

    DataGrid1.DataSource = DS.Tables("TBL")


  • Need help... GridView defaults to all records