vb.net Filling data-set value as combo-box value-member issue - sql

myORAConnection.Open()
Dim SQLString As String = "Select child_id as id, child_name as name from xxi_org_structure_v Where parent_id=" & LegalEntity & ""
Dim cmd As New OracleCommand(SQLString, myORAConnection, OraTrans)
Dim daTableORA As New OracleDataAdapter(cmd)
daTableORA.Fill(dtOrg)
daTableORA.Dispose()
cmd.Dispose()
myORAConnection.Close()
With IT_ORG_CODEComboBox
.DataSource = dtOrg.Tables(0)
.ValueMember = "id"
.DisplayMember = "name"
End With
Catch ex As Exception
MsgBox(ex.Message)
End Try
'TODO: This line of code loads data into the 'Main_POSDataSet.pos_promotion_item_group_details' table. You can move, or remove it, as needed.
Me.Pos_promotion_item_group_detailsTableAdapter.Fill(Me.Main_POSDataSet.pos_promotion_item_group_details)
'TODO: This line of code loads data into the 'Main_POSDataSet.pos_promotion_item_group_headers' table. You can move, or remove it, as needed.
Me.Pos_promotion_item_group_headersTableAdapter.Fill(Me.Main_POSDataSet.pos_promotion_item_group_headers)
I am filling the combobox with value from oracle, but getting the selected value from sql and storing it to sql too.
I am getting the value to fill in the form from "Main_POSDataSet.pos_promotion_item_group_headers", however I want to tell the form or the dataset or the combobox that the value retrieved from the "IT_ORG_CODE" column is to be used as combobox value-member and not display member.
is there anyway to solve this please ?
237 is the organisation id to use as combobox valuemember, but here it is used as display member !!

Just replace
Me.IT_ORG_CODEComboBox.DataBindings.Add(New System.Windows.Forms.Binding("Text", Me.Pos_promotion_item_group_headersBindingSource, "IT_ORG_CODE", True))
With
Me.IT_ORG_CODEComboBox.DataBindings.Add(New System.Windows.Forms.Binding("SelectedValue", Me.Pos_promotion_item_group_headersBindingSource, "IT_ORG_CODE", True))
in the form designer code

Related

combobox default value, vb.net

i have a combobox that populates from a database column but when the form loads, the combobox appears with the first item on the database. the value of this particular combobox determines the value of some other controls on the form so i want the combobox to load with an empty entry. thanks as i'll appreciate any help i can get.
here is my code:
'all variables declared...
connection.Open()
command = New MySqlCommand("SELECT PUMP FROM test.pump", connection)
dataadapter.SelectCommand = command
dataadapter.Fill(dataset)
With Me.PumpComboBox
.DisplayMember = "pump"
.DataSource = dataset
End With
Use
combobox1.SelectedIndex = -1
This sets the value of the combo box to a blank entry.
Maybe adding empty value and then selected that empty value using selectedindex after the combbobox populated will work
In your Form.Load include the following:
ComboBox1.SelectedIndex = 1
Here "ComboBox1" is your ComboBox and "1" equals index you want to be set by default.

How to display DataGridView outside the parent control like GroupBox

How can I display DataGridView outside the parent control (eg. GroupBox) like DropDownList.
I want to display DataGridView with full of size and its content whenever I double click the Textbox but DataGridView is partially showing and hides outside the GroupBox (its parent control).
Please refer to the attached snapshot.
ClearText = New DefaultClass
ClearText.ClearTextBox(Me)
Try
con = New SqlConnection(ConnStrSql)
con.Open()
da = New SqlDataAdapter("SELECT [CODE] as [Member CODE],[NAME] as [Name],[FNAME]as [Father's Name] FROM [MsumCOOP].[dbo].[MEMBER] ORDER BY [CODE] asc,[NAME] asc,[FNAME] asc", con)
dt = New DataTable
da.Fill(dt)
dgvMemberCode.DataSource = dt
If dt.Rows.Count > 0 Then
dgvMemberCode.Location = New Point(txtMemberCode.Left, txtMemberCode.Bottom)
dgvMemberCode.Visible = True
Else : dgvMemberCode.Visible = False
End If
Catch ex As Exception
MsgBox("No DATA Fetched" + ex.Message)
Finally
con.Close()
End Try
There are two ways, both mentioned in commens.
1) Simple, but limited
If your grid is allways small enought to fit on form boundaries, put it into your Form.Controls instead of GroupBox.Controls. As suggested by #Farhan Ahmed Saifi
2) Full, but complicated
Create second form with the grid, tune BorderStyle and so on and show it on required coordinates. This will require more handling with Show/Hide/LostFocus/PositionChanged logic. As suggested by #jmcilhinney

How to make sortable a DataGrid bounded with a table

I load data on a DataGrid from a SQLite database with code like this:
conn.Open()
Dim sql = "SELECT * FROM TableDataGrid"
Dim cmdConnection As SQLiteCommand = New SQLiteCommand(sql, conn)
Dim da As New SQLiteDataAdapter
da.SelectCommand = cmdConnection
Dim dt As New DataTable
da.Fill(dt)
MyDataGrid.DataSource = dt
When the user click on a Row I display the data on a few textbox, combobox, no problem at all.
Now I need to let the user sort the DataGrid by clicking the column he want and of course is not that simple.
After research on the "e.RowIndex >= 0" and "System.NullReferenceException" error I understand that "The problem is that out of the box the BindingList does not support sorting! I know - sounds dumb but that is how it is." posted here: WinForms: DataGridView - programmatic sorting
So If I'm right I need to implement my own SortableBindingList but I'm confused because the samples of code are about a LIST and I load the database records on a TABLE (dt)
Example:
http://timvw.be/2007/02/22/presenting-the-sortablebindinglistt/
Another post say I can fix the trouble with a line like:
SortableBindingList<YourListType> sortableBindingList = new SortableBindingList<YourListType>(list)
What is the generic solution to make the DataGrid sortable on this case?
Thanks to the tips of the users I found the solution
Private Sub MyDataGrid_SelectionChanged(sender As Object, e As EventArgs) Handles MyDataGrid.SelectionChanged
'Check if the user clicked on a header or a row
Try
If MyDataGrid.CurrentRow.Index = Nothing Then
'Header clicked!!
Else
'Row clicked!!!
'Here I load the data of the clicked row on my form
FormCleanDataEntry()
FormShowRowData()
End If
Catch ex As Exception
'MsgBox(ex.ToString())
End Try
End Sub
The curiosity is that the exception is thrown but anyway I don't show any message to the user and the DataGrid is sorted and the program works fine

datagridview columns disappears

Guys i have a datagridview and a button on my form.
Under form load event following code runs for displaying data into datagridview columns.. I have manually added some columns in datagridview to populate data from dataset.
The button is placed on the form to clear all data except column..
I have tried several times but with data the columns also disappears..
is there any solution for this ? thanks in advance
I want to use this datagridview column again for displaying data entered in textbox... without dataset..
con.Open()
cmd.CommandText = "select Code,Name,Operations,Rate,Qty,Tax,Tax_amt,Discount,Total_Amount from products where Id = ('" & CStr(i) & "')"
Dim sdr As SqlDataReader = cmd.ExecuteReader
Dim dt As DataTable = New DataTable()
dt.Load(sdr)
dt.Columns("Code").ColumnName = "ii_code"
dt.Columns("Name").ColumnName = "ii_name"
dt.Columns("Operations").ColumnName = "ii_opera"
dt.Columns("Rate").ColumnName = "ii_rate"
dt.Columns("Qty").ColumnName = "ii_qty"
dt.Columns("Tax").ColumnName = "ii_Tax"
dt.Columns("Tax_Amt").ColumnName = "ii_taxamt"
dt.Columns("Discount").ColumnName = "ii_dis"
dt.Columns("Total_Amount").ColumnName = "ii_total"
DataGridView1.DataSource = dt
con.Close()
If the DataGridView is not bound to any data source, this code will do the trick:
DataGridView.Rows.Clear()
Else you can clear you data source in your example it is data table
Datatable.rows.clear()
I mean I have one datagridview on my form to view sales order...
On form load datagridview is filled with data...
But i want to place a button on my form which clears all the data from datagridview to accept new data without deleting data from database..
There are some text box on my form from that new data will be entered temporary on datagridview..

Need to change Datagridview row color if checkbox is checked on Form Load

i have problem with dataset gridview on Form load procedure. I hava dataset as datagridview source and when I'm trying to change datagrid row color based on if statement if checkbox is checked nothing happens. If I used same code on some button-click procedure it all works fine.
Can anyone help me with this problem.
da.Fill(ds, "customer")
cnn.Close()
DataGridView1.DataSource = ds.Tables("customer")
For i = 0 To DataGridView1.Rows.Count - 1
If DataGridView1.Rows(i).Cells(3).Value = True Then
DataGridView1.Rows(i).DefaultCellStyle.BackColor = Color.MediumAquamarine
Else
DataGridView1.Rows(i).DefaultCellStyle.BackColor = Color.MistyRose
End If
Next
You cant have this code in Form_Load. This only occur when the form starts the first time. Try instead to put the code in the DataGridView1.Paint event handler. That should do the trick.