user2564223
user2564223

Reputation: 451

Populating and editing datagrid from a SQL Server table

I need to populate my datagrid from my a table in SQL Server called Pastel_Companies and then if any changes are made to the datagrid it has to update it to the database.

I am using a default view to populate my datagrid.

Is there another way where I link each column separately so I can resize my columns as they are fixed to what SQL has?

Here is my code:

Dim cn As New SqlClient.SqlConnection(SQL_Loader("", My.Settings.SQL_Win_Auth, My.Settings.SQL_Username, My.Settings.SQL_Password, My.Settings.SQL_Server_Name, My.Settings.SQL_DB_Name))
Dim Cmd As New SqlClient.SqlCommand
Dim ds As New DataSet
Dim dt As New DataTable
ds.Tables.Add(dt)
Dim da As New SqlClient.SqlDataAdapter

Cmd.Connection = cn

cn.Open()

da = New SqlClient.SqlDataAdapter("Select Company_ID, Prefix, DSN, File_Path From Pastel_Companies", cn)
da.Fill(dt)

'DataGridView1.Columns.Add("Company_ID", Prefix.ToString)
DataGridView1.DataSource = dt.DefaultView

cn.Close()

Upvotes: 2

Views: 127

Answers (1)

D_Bester
D_Bester

Reputation: 5911

From: http://social.msdn.microsoft.com/Forums/windows/en-US/e444ca84-3319-4dfa-aa31-46f310dd0c13/datagridview-autosize-rowcolumn

    'for the rows
    DataGridView1.AutoResizeRow(2, DataGridViewAutoSizeRowMode.AllCellsExceptHeader)
    'and for columns
    DataGridView1.Columns(0).AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells

or you can use for each loop

    DataGridView1.Columns(i).Width = x

Upvotes: 3

Related Questions