How to get DevExpress GridControl to show captions instead of field names? - vb.net

I have a DevExpress GridControl with a master and and detail. On the detail, I want to show one column. The dataset I'm using has two columns, the one I want to show and the foreign key back to the master. In the designer, I've included only the column I want and in design mode it show one column. However, when I run the application I see both columns with the names from the data set. How do I get the detail grid to just show the one column I want?
Here's an example of my code. I've tried it with and without the level tree add and tried different names for the relation. Nothing I've tried uses the formatting I set in the designer for the detail view.
Dim cmd As SqlCommand
Dim da As SqlDataAdapter
Dim da2 As SqlDataAdapter
cmd = MSSQL_CONN.CreateCommand
cmd.CommandType = CommandType.Text
cmd.CommandText = "select * from vw_backlog with (nolock) where order_id = " & m_order.id
da = New SqlDataAdapter(cmd)
cmd = MSSQL_CONN.CreateCommand
cmd.CommandType = CommandType.Text
cmd.CommandText = "SELECT order_id, backlog_id, special_handling_id, comment_id, message FROM vw_backlog_special_handling_comments with (nolock) WHERE order_id = " & m_order.id
da2 = New SqlDataAdapter(cmd)
Dim ds As New DataSet()
da.Fill(ds, "Backlogs")
da2.Fill(ds, "Messages")
Dim keyColumn As DataColumn = ds.Tables("Backlogs").Columns("backlog_id")
Dim foreignKeyColumn As DataColumn = ds.Tables("Messages").Columns("backlog_id")
ds.Relations.Add("gvSpecialHandlingComments", keyColumn, foreignKeyColumn)
gcBacklog.DataSource = ds.Tables("Backlogs")
gcBacklog.ForceInitialize()
gcBacklog.LevelTree.Nodes.Add("SpecialHandlingComments", gvSpecialHandlingComments)
gvSpecialHandlingComments.PopulateColumns(ds.Tables("Messages"))

Related

Data type mismatch in criteria expression vb.net

I am using VB.Net connecting to MS Access database. I think SQL syntax is wrong because before i add WHERE clause, it works. When i add WHERE clause, i got error ' data type mismatch in criteria expression'. Any help would be appreciated, thank you.
Here is my code
Dim da As OleDbDataAdapter = New OleDbDataAdapter("Select Sum([Shark Individual Weight]) From FishCaught Where [OperationID] ='" & TextBoxOpID4.Text & "'", myConnection)
Dim ds As DataTable = New DataTable()
da.Fill(ds)
DataGridView2.DataSource = ds
Additional info: textboxOpID4 is enabled false
make something like this
Dim da As OleDbDataAdapter = New OleDbDataAdapter("Select Sum([Shark Individual Weight]) From FishCaught Where [OperationID] =" & TextBoxOpID4.Text , myConnection)
Dim ds As DataTable = New DataTable()
da.Fill(ds)
DataGridView2.DataSource = ds
I quite use this kind of query when the where clause is a number because if you do put ' in a number column the output would be string which produces the error for example the ColumnID = 5 your code is like this ColumnID = '5' which is wrong especially if it is an AutoNumber field so do not insert a datatype number inside ' so it will not become a string

Visual Basic - No value given for one or more required parameters

I have a DataGridView which displays the contents of a table (with sql query). But whenever I run the code, it displays this error:
No value given for one or more required parameters.
Where did it went wrong? The program ran without displaying any errors.
Here's the code for displaying in the DataGridView:
Dim connString As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\BSPDatabase.accdb"
MyConn = New OleDbConnection
MyConn.ConnectionString = connString
ds = New DataSet
tables = ds.Tables
da = New OleDbDataAdapter("Select * from [Transactions] where transMonth=" & monthCombobox.Text & "", MyConn)
da.Fill(ds, "BSPDatabase") 'Change items to your database name
Dim view As New DataView(tables(0))
source1.DataSource = view
TransactionsDataGridView.DataSource = view
I think you are forming bad SQL. The value needs to be in quotes.
da = New OleDbDataAdapter(String.Format("Select * from [Transactions] where transMonth ='{0}'", monthCombobox.text), MyConn)

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.

Linking two DataGridViews

I have two Grid controls in VB.Net Winform application connected to Oracle Database. The first one shows a table 1 with fields (among others) ID and TaskName. The second one shows a linked table with fields (among others) Task. In the logic of the application the two tables are linked (Table 1, Field ID) <----> (Table 2, Field Task).
I would like, when selecting a row in the first DataGridView, have only the linked row in the second GridView but I can't make it work.
Here is my code attempt :
Dim SQLQuery As String = "SELECT * FROM SCHEME.ARCH_TASKS"
Conn = New OracleConnection(ConnectionString)
Command = New OracleCommand(SQLQuery, Conn)
DataAdapter = New OracleDataAdapter(Command)
DataSet = New DataSet()
DataTable1 = New DataTable
DataSet.Tables.Add(DataTable1)
DataAdapter.Fill(DataTable1)
DataGridView.DataSource = DataTable1.DefaultView
GridControl1.DataSource = DataTable1.DefaultView
SQLQuery = "SELECT * FROM SCHEME.ARCH_LINK_ROLE_TASK"
Command = New OracleCommand(SQLQuery, Conn)
DataAdapter = New OracleDataAdapter(Command)
DataTable2 = New DataTable
DataSet.Tables.Add(DataTable2)
DataAdapter.Fill(DataTable2)
GridControl2.DataSource = DataTable2
DataSet.Relations.Add(DataTable1.Columns("ID"), DataTable2.Columns("TASK"))
Here is the result
http://img11.hostingpics.net/pics/4713062013102413h1253.png
As you can see, I added manually a relationship between the two data tables but it doesn't really do what i'm looking for. It simply adds a subline in the first grid control.
http://img11.hostingpics.net/pics/7508382013102413h1309.png
I tried to apply what PeterG suggested but I still can't make it work.
Dim SQLQuery As String = "SELECT * FROM SCHEME.ARCH_TASKS"
Command = New OracleCommand(SQLQuery, Conn)
DataAdapter = New OracleDataAdapter(Command)
DataSet.Tables.Add(DataTable1)
DataAdapter.Fill(DataTable1)
SQLQuery = "SELECT * FROM SCHEME.ARCH_LINK_ROLE_TASK"
Command = New OracleCommand(SQLQuery, Conn)
DataAdapter = New OracleDataAdapter(Command)
DataSet.Tables.Add(DataTable2)
DataAdapter.Fill(DataTable2)
DataSet.Relations.Add(DataTable1.Columns("ID"), DataTable2.Columns("TASK"))
BindingSource1.DataSource = DataSet
GridControl1.DataSource = BindingSource1.DataSource
GridControl1.DataMember = "Table1"
BindingSource2.DataSource = BindingSource1.DataSource
GridControl2.DataSource = BindingSource2.DataSource
GridControl2.DataMember = "Table2"
Am I missing something ?
Thank you.

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