Populating and editing datagrid from a SQL Server table - sql

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()

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

Related

Add a new Column to a DataGridView control

I have a DataGridView with DataSource from a text file
If ouvrir.ShowDialog = DialogResult.OK Then
Dim sr As New StreamReader(ouvrir.FileName)
While Not sr.EndOfStream
datagridview1.Rows.Add(sr.ReadLine.Split(CChar(vbTab)))
End While
End If
Now I need to add another column from a database and I have a lot of problem with it.
While adding it show under the first column automatcally and what I want to add it as new column in the middle of the DataGridView.
My code its just:
Dim cmd As New SqlCommand("select numero from database", cn)
Dim da As New SqlDataAdapter(cmd)
Dim dt2 As New DataTable
da.Fill(dt2)
datagridview1.DataSource = dt2

Saving record with dataset

I want to save a record in the database using a dataset, but my data is not committing into my database.
My code can be viewed below:
Dim mydataset1 As New MyDataSet
Dim row As DataRow = mydataset1.Tables("testtable").NewRow()
With row
.Item("name") = "Segun Omotayo"
.Item("address") = "Abuja"
End With
mydataset1.Tables("testtable").Rows.Add(row)
Any help will be appreciated
A DataSet/DataTable is a offline/in-memory representation of your database. If you want to update the database, you need to use a DataAdapter.
For example (assuming you're using MS-Sql-Server):
Public Function UpdateDataSet(dataSet As DataSet) As Int32
Using con = New SqlConnection(My.Settings.SqlConnection)
Dim sql = "INSERT INTO TUser(Name,Address)VALUES(#Name,#Address)"
Using cmd = New SqlCommand(sql, con)
cmd.Parameters.Add(New SqlParameter("#Name", SqlDbType.VarChar))
cmd.Parameters.Add(New SqlParameter("#Address", SqlDbType.VarChar))
Using da = New SqlDataAdapter()
da.InsertCommand = cmd
con.Open()
Dim rowCount = da.Update(dataSet)
Return rowCount
End Using
End Using
End Using
End Function
I could be rusty here since its a long time since I wrote any VB.NET or used data adapters/datasets/datatables but I think if you decide to take that route you would need code like this:
Dim connection As New SqlConnection("#####YourConnectionString#####")
connection.Open()
Dim adapter As New SqlDataAdapter("SELECT * FROM testtable", connection)
' For the line below to work, you must have a primary key field in "testtable"
Dim builder As New SqlCommandBuilder(adapter)
Dim testtable As New DataTable("testtable")
adapter.Fill(testtable)
Dim row As DataRow = testtable.NewRow()
With row
.Item("name") = "Segun Omotayo"
.Item("address") = "Abuja"
End With
testtable.Rows.Add(row)
adapter.Update(testtable)
connection.Close()

Setting data to DataGridView in VB.net

I have a DataGridView on windows Form, just draged and droped from tools.
Now I want to set data to this gridview using DataSet or DataTable. How can set it by coding.
I am using SQL Server 2008 and VS 2010
Please Help me....
here's a quick example:
Dim DS As New DataSet()
Dim DA As New SqlDataAdapter()
Dim SelectQ as String = Query
Dim CN As New SqlConnection(ConnectionString)
DA.SelectCommand = New SqlClient.SqlCommand(SelectQ)
DACustomers.SelectCommand.Connection = CN
CN.Open
DA.fill(DS, "diseredTable")
CN.Close
Dim BS As New BindingSource
BS.DataSource = DS
BS.DataMember = ”desiredTable”
DataGridView1.DataSource = BS

Moving a column in a datatable from first to last

I have a sql stored procedure that returns a certain group of columns in my datatable.
How can I move (let's say) the column in the first position, to the last position before I export the datatable to excel ?
Dim myConn As New SqlConnection(strConnection)
Dim myCmd As New SqlCommand(strSQL, myConn)
Dim dt As DataTable = New DataTable()
Dim da As SqlDataAdapter = New SqlDataAdapter(strSQL, myConn)
da.Fill(dt)
Dim excelPackage = New OfficeOpenXml.ExcelPackage
Dim excelWorksheet = excelPackage.Workbook.Worksheets.Add("ProjectStatusReports")
excelWorksheet.Cells("A3").LoadFromDataTable(dt, True)
This changes the position of the first column to the last in a DataTable:
dt.Columns(0).SetOrdinal(dt.Columns.Count - 1)
http://msdn.microsoft.com/en-us/library/system.data.datacolumn.setordinal.aspx
http://msdn.microsoft.com/en-us/magazine/cc163495.aspx (search SetOrdinal)
The columns before the addressed ordinal position are all decremented one slot to make room for the change of positioning.

How to determine the id of a data in vb.net?

I'm currently working on an ms access database in vb.net.
And I need a code that can determine which data I am updating in the database. Because when I try to update the data, the previous data is being cloned and it will generate two data(the updated and the previous data), and the program will also generate a random id number for the updated data, which is not good.
Here is my code for the update button:
'update
Dim cb As New OleDb.OleDbCommandBuilder(da)
ds.Tables("GH").Rows(INC).Item(1) = TextBox13.Text
ds.Tables("GH").Rows(INC).Item(2) = TextBox14.Text
ds.Tables("GH").Rows(INC).Item(3) = TextBox15.Text
da.Update(ds, "GH")
MsgBox("Data updated")
My code for form load:
con.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=F:\ACCESS DATABASE\search.mdb"
con.Open()
sql = "SELECT * FROM GH"
da = New OleDb.OleDbDataAdapter(sql, con)
da.Fill(ds, "GH")
con.Close()
I'm using system.data.oledb namespace
Here are my declarations:
Dim cmd As OleDbCommand
Dim cn As OleDbConnection
Dim dr As OleDbDataReader
Dim ds As New DataSet
Dim con As New OleDb.OleDbConnection
Dim da As OleDb.OleDbDataAdapter
Your question is not clear to me at all, but if you want to find the last ID of an insert, you would usually check the results of "SELECT ##IDENTITY" immediately after the record is inserted.