Add a new Column to a DataGridView control - vb.net

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

Related

Add only new items from listbox to SQL

i have a listbox that users can add Countries Names into it anytime.
now i need to only add new data to check if the data is new then insert it to sql.
my code not work:
For Each i As String In listbox1.Items
Dim sql = "select * From Countries where CountryName=N'" & i & "'"
Dim adp As New SqlClient.SqlDataAdapter(sql, SQlconn)
Dim ds As New DataSet
adp.Fill(ds)
Dim dt = ds.Tables(0)
If dt.Rows.Count = 0 Then
Dim dr = dt.NewRow
dr!CountryName = i
dt.Rows.Add(dr)
Dim cmd As New SqlClient.SqlCommandBuilder(adp)
adp.Update(dt)
End If
Next
If you're loading the existing names from the database in the first place then there's no need for you to check anything. Simply query the database to populate a DataTable and bind that to the ListBox. When the user adds a new country, add it to the DataTable and it will automatically show up in the ListBox. When it's time to save, just use the same data adapter to save the DataTable and only the new records will be saved.

How can I specify which DataGridView columns are used when loading data from a DataTable?

I have the following code to populate a DataGridView component.
It works fine, but column names are defined in the DataGridView and I got a duplicated set of columns, the ones coming from the original definition and the ones coming from the dataTable. So it seems that load is not making any effort to match names between DataGridView and DataTable (I carefully set them equal).
Private Sub PopulateTable()
Dim dataConnection As New MySqlConnection
Dim cmd As New MySqlCommand
Dim reader As MySqlDataReader
Dim dataTable As New DataTable
dataConnection.ConnectionString = "server=127.0.0.1;uid=user;pwd=password;database=test"
dataConnection.Open()
' Open connection and execute query
cmd.Connection = dataConnection
cmd.CommandText = "SELECT name, type, street1, street2 FROM customer;"
' Load DataTable through reader
reader = cmd.ExecuteReader
dataTable.Load(reader)
reader.Close()
' Assign DataTable to DataGridView
dataGridView.DataSource = dataTable
' Close DB connection
dataConnection.Close()
End Sub
Is it possible to match columns from DataTable to DataGridView?

clear all records from data set in vb.net

i am working on vb.net windows form..in my button click i have given code like this:
Dim rpt As New DelivaryPerformance
Dim ds As New DataSet
Dim da As New SqlDataAdapter
Dim rpt As New DelParkingtype
Dim locid As Integer = RecordID("Locid", "Location_tbl", "LocName", CmbLocations.Text)
Dim cmdstatus As New SqlCommand("IBS_DelivaryStaus", con.connect)
cmdstatus.CommandType = CommandType.StoredProcedure
cmdstatus.Parameters.Add("#locid", SqlDbType.Int).Value = locid
da.SelectCommand = cmdstatus
da.Fill(ds)
If (ds.Tables(0).Rows.Count > 0) Then
rpt.SetDataSource(ds.Tables(0))
' CrystalReportViewer1.ReportSource = rpt
End If
while i am clicking each time in my button click,,i want to clear all records from my data set,,how i can clear my all records from my dataset
If you want to clear all rows from the tables contained in a dataset you could use
ds.Clear()
MSDN reference
DataSet.Clear
This however, removes the records, not the tables schema. If you want to get rid of the tables also then you need to use
ds.Tables.Clear()
However, in the context of your code, the dataset is created as new, and so it is already empty until you fill it with the appropriate command, so it is not clear in which context you need to empty it.

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

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.