Mokmeuh Mokmeuh - 1 year ago 58 Question

DataSet, DataAdapter, no dataTable Primary key

Ok so I'm having this:

Dim sql As String = "SELECT * FROM Articles"
dAdapter = New OleDbDataAdapter(sql, connection)
dSet = New DataSet("tempDatatable")

With connection
dAdapter.Fill(dSet, "Articles_table")
End With

With DataGridView1
.DataSource = dSet
.DataMember = "Articles_table"
End With

And I wonder if there is any possible way to define the first column as the primary key. I've looked around but everyone is using a manual datatable to fill up the datagrid. Since I'm using a dataBase I don't know how to set a primary key from there. I need some help.

Answer Source

You can add a primary key to your data table using something like this:

var table = dSet.Tables["Articles_table"];
table.PrimaryKey = new DataColumn[] { table.Columns[0] };

Sorry, just realised the question was tagged with, not c#. would be:

Dim table = dSet.Tables("Articles_table")
table.PrimaryKey = New DataColumn() {table.Columns(0)}