SqlDataAdapter.Fill(DataGridView.DataSource) duplicates all rows

后端 未结 2 774
日久生厌
日久生厌 2021-01-28 13:10

Simple question:

When I call SqlDataAdapter.Fill(DataGridView.DataSource) the second time after initially creating first Data it does not update the contain

2条回答
  •  青春惊慌失措
    2021-01-28 14:00

    From MSDN:

    You can use the Fill method multiple times on the same DataTable. If a primary key exists, incoming rows are merged with matching rows that already exist. If no primary key exists, incoming rows are appended to the DataTable.

    So either define a primary key or clear the table first.

    Dim table = CType(DataGridView.DataSource, DataTable)
    table.Clear()
    ' fill  ...
    

    To define primary key(s) manually read this. To let it create automatically if they are defined in the database you need to set the MissingSchemaAction to AddWithKey:

    ' ...
    dataAdapter.MissingSchemaAction =  MissingSchemaAction.AddWithKey
    ' fill ...
    

提交回复
热议问题