vb.net GridView store value but display text from datatable - vb.net

Please I want to store the value of address in the gridview or datatable but display it's name, so when saving data to database I will insert the value.
dtCust_dtls.DefaultView.RowFilter = String.Format("SR_ID = '{0}'", SrLookup.GetSelectedItem("SR_ID"))
GCCustList.DataSource = dtCust_dtls.DefaultView
GVCustList.Columns("CUSTOMER_CODE").VisibleIndex = 0
GVCustList.Columns("ADDRESS_ID").VisibleIndex = 1
GVCustList.Columns("CUSTOMER_CODE").Caption = "Customer Code"
GVCustList.Columns("ADDRESS_ID").Caption = "Address"
on button add
dtCust_dtls.DefaultView.RowFilter = String.Format("SR_ID = '{0}' and CUSTOMER_CODE = '{1}'", SrLookup.GetSelectedItem("SR_ID"), txtCust_Code.Text.Trim)
If (dtCust_dtls.DefaultView.Count = 0) Then
Dim dr As DataRow = dtCust_dtls.NewRow()
dr("SR_ID") = SrLookup.GetSelectedItem("SR_ID")
dr("CUSTOMER_ID") = DataRowSelectedCust("CUSTOMER_ID")
dr("ADDRESS_ID") = cmbCustAdd.SelectedValue
dr("CUSTOMER_CODE") = txtCust_Code.Text.Trim
dr("CUSTOMER_BARCODE") = DataRowSelectedCust("CUSTOMER_ID") & "." & cmbCustAdd.SelectedValue 'custId + . + addID
dr("STAMP_DATE") = Now
dtCust_dtls.Rows.Add(dr)
Else
MsgBox("The Customer code '" & txtCust_Code.Text & "' already exist for " & txtSrName.Text)
End If
on save btn I am saving the datatable dtCust_dtls as it is.
I had an idea to add one column : Address Name and leave the column Address ID but I don't have any idea how to do it !

USING THE RepositoryItemLookUpEdit
Dim xCust_Code As New DevExpress.XtraEditors.Repository.RepositoryItemLookUpEdit
With xCust_Code
.ForceInitialize()
.DataSource = dtCustList.DefaultView
.DisplayMember = "Customer_Description"
.ValueMember = "Customer_Code"
.PopulateColumns()
.NullText = String.Empty
End With
GVCustDUE.Columns("CUSTOMER_CODE").ColumnEdit = xCust_Code

Related

Search data in DataGridView using FilterDataRow VB.Net

DV = New DataView(ds.Tables("CustomerList"))
FlxCustomerList.DataSource = ds.Tables("CustomerList").DefaultView
myDataTable = CType(FlxCustomerList.DataSource, DataView).ToTable
drToAdd = myDataTable.NewRow()
drToAdd("Customer Account") = ""
drToAdd("Customer Name") = ""
drToAdd("Customer Group") = ""
drToAdd("City") = ""
drToAdd("Mobile No") = ""
drToAdd("Status") = ""
myDataTable.Rows.InsertAt(drToAdd, 0)
myDataTable.AcceptChanges()
FlxCustomerList.DataSource = myDataTable.DefaultView
For Each dtrow As DataGridViewRow In FlxCustomerList.Rows
If dtrow.Index = 0 Then
dtrow.Frozen = True
Else
dtrow.ReadOnly = True
End If
Next
To achieve the search in Datagridview functionality, I'm getting the first row by this, on search enter, key-down function call this
CustomerAccountFilterStr = CStr(Me.DataGridView1.Rows(0).Cells("CustomerAccount").Value)
sStr = sStr & " [Customer Account] like '%" & CustomerAccountFilterStr & "%'"
ds.Tables("CustAcc").DefaultView.RowFilter = sStr
This is how I binding Data to DataGridView
myDataView.RowFilter = sStr
FlxCustomerList.DataSource = myDataView
And Then The DataGridView which is bound to the DefaultView of DataSet. I froze the first row but now, couldn't understand how to put back filtered values from the second-row onwards to the grid.
The example output should resemble like below.
I'm stuck to implement this functionality. please guide How to implement this functionality?

The error says "String is not a valid Boolean" in the code "CheckBoxActive.Checked = Convert.ToBoolean(ds1.Tables(0).Rows(0)(3).ToString())"

If (TextBoxStudentID.Text.ToString().Trim() <> "") Then
Dim adp1 As SqlDataAdapter = New SqlDataAdapter("select StudentName, StudentStandard, StudentBalance, EmailAddress, ContactNumber Active from StudentDetails where StudentID = '" + TextBoxStudentID.Text.ToString() + "'", con)
Dim ds1 As DataSet = New DataSet()
adp1.Fill(ds1)
If ds1.Tables.Count > 0 Then
If ds1.Tables(0).Rows.Count > 0 Then
TextBoxStudentName.Text = ds1.Tables(0).Rows(0)(0).ToString()
TextBoxStudentStd.Text = ds1.Tables(0).Rows(0)(1).ToString()
TextBoxCurrentBalance.Text = ds1.Tables(0).Rows(0)(2).ToString()
CheckBoxActive.Checked = Convert.ToBoolean(ds1.Tables(0).Rows(0)(3).ToString())
Else
MessageBox.Show("No student found for Student ID = " + TextBoxStudentID.Text.ToString(), "Search failed!")
TextBoxStudentName.Text = ""
TextBoxStudentStd.Text = ""
TextBoxCurrentBalance.Text = ""
TextBoxContactNumber.Text = ""
CheckBoxActive.Checked = False
End If
End If
End If
I wrote the above code to search for a particular roll number in the database I called. The error says "the string is not a valid boolean". kindly help.
Try:
Convert.ToBoolean(ds1.Tables(0).Rows(0)(5).ToString())
Column 3 is the email address. Also add a comma between ContactNumber and Active

DataGridView Yes No ComboBox Column

I have what on the face of it seems a pretty simple requirement, I need an unbound combobox column to hold an option list of 'Yes' and 'No'. In combobox parlance Yes and No are the DisplayMembers corresponding to ValueMembers 1 and 0 respectively which in turn are the values held in the field of the database table. I use bound combobox columns in the application extensively and am very familiar with building them, but can't for the hell of me work out how to incorporate this simple list functionality in the datagridview application. Could somebody please put me out of my misery with some sample code.
Thanks for your feedback Plutonix. The DGV is bound to an underlying table. Hopefully, the following code should give you some idea of what I am attempting to acheive. The code specifically relevant to this post is in the call to PopulateUnboundComboBox(comboBoxCol, {"Yes", "No"}).
Private Sub GetData()
Const procName As String = "GetData()"
Dim myValue As String = ""
Dim myDataSource As Integer = 0
Try
'First clear the existing grid
With Me.uxGrid
If .ColumnCount > 0 Then
'clear the grid
ClearGrid(Me.uxGrid)
End If
End With
_Logger.SendLog(Me.Name & "." & procName & " - Fetching device data.", NLog.LogLevel.Trace)
'Get the data from the database
If Not _dataSourceID = 0 Then
_sqlStatement = "SELECT ID, TEXT, CATEGORY, MOUNTING, DATASOURCEID, TANALYSIS" & _
" FROM P_TBL_DEVICE WHERE DATASOURCEID = " & _dataSourceID
_criteria = "WHERE DATASOURCEID = " & _dataSourceID
Else
_sqlStatement = ""
_criteria = ""
End If
_myDeviceMngr = New DeviceManager(_currentDB, _userName, _myPwd)
'Now get the latest data
_myDataSet = _myDeviceMngr.GetData(_sqlStatement)
_Logger.SendLog(Me.Name & "." & procName & " - Device data fetch completed.", NLog.LogLevel.Trace)
'Update the display
Call BuildGrid(_myDataSet, uxGrid)
Catch ex As Exception
Beep()
MsgBox(ex.Message, MsgBoxStyle.Exclamation, System.Windows.Forms.Application.ProductName)
_Logger.SendLog(ex.Message & ". Thrown in module " & Me.Name.ToString & "." & procName, NLog.LogLevel.Error, ex)
Finally
Call System.GC.Collect()
End Try
End Sub
Private Sub BuildGrid(ByVal myDataSet As DataSet, ByVal myGrid As DataGridView)
Dim myTable As New System.Data.DataTable
Dim myColCount As Integer
Dim comboBoxCol As DataGridViewComboBoxColumn
Dim readOnlyCellStyle As New DataGridViewCellStyle
Dim textBoxCol As DataGridViewTextBoxColumn
Dim gridObject As Object = Nothing
Const procName As String = "BuildGrid"
readOnlyCellStyle.ForeColor = Color.Gray
Try
_Logger.SendLog(Me.Name & "." & procName & " - Building device data grid.", NLog.LogLevel.Trace, Nothing)
myTable = myDataSet.Tables(0)
With myGrid
'Now add the columns to the grid
Dim column As System.Data.DataColumn
For Each column In myDataSet.Tables(0).Columns
'We dont want to include the changedon and changedby fields in the grid build
If column.ColumnName.IndexOf("CHANGED") = -1 Then
Select Case column.ColumnName
Case "DATASOURCEID"
gridObject = New Object
comboBoxCol = New DataGridViewComboBoxColumn
'First create the comboboxcolumn for the column
'Populate combobox
PopulateScadaSourceComboBoxCol(comboBoxCol)
comboBoxCol.DataPropertyName = "DATASOURCEID"
comboBoxCol.HeaderText = "SCADA SOURCE"
comboBoxCol.AutoSizeMode = DataGridViewAutoSizeColumnsMode.AllCells
comboBoxCol.DisplayStyle = DataGridViewComboBoxDisplayStyle.Nothing
gridObject = comboBoxCol
Case "TEXT"
textBoxCol = New DataGridViewTextBoxColumn
gridObject = textBoxCol
'Now add the columns to the grid
gridObject.DataPropertyName = column.ColumnName.ToString
gridObject.HeaderText = column.ColumnName.Replace("TEXT", "DEVICE")
gridObject.name = column.ColumnName.Replace("TEXT", "DEVICE")
gridObject.AutoSizeMode = DataGridViewAutoSizeColumnsMode.AllCells
Case "CATEGORY"
textBoxCol = New DataGridViewTextBoxColumn
gridObject = textBoxCol
'Now add the columns to the grid
gridObject.DataPropertyName = column.ColumnName.ToString
gridObject.HeaderText = "CATEGORY"
gridObject.name = "CATEGORY"
gridObject.AutoSizeMode = DataGridViewAutoSizeColumnsMode.AllCells
Case "MOUNTING"
textBoxCol = New DataGridViewTextBoxColumn
gridObject = textBoxCol
'Now add the columns to the grid
gridObject.DataPropertyName = column.ColumnName.ToString
gridObject.HeaderText = "MOUNTING"
gridObject.name = "MOUNTING"
gridObject.AutoSizeMode = DataGridViewAutoSizeColumnsMode.AllCells
Case "TANALYSIS"
comboBoxCol = New DataGridViewComboBoxColumn
'First create the comboboxcolumn for the column
'Populate combobox
PopulateUnboundComboBox(comboBoxCol, {"Yes", "No"})
comboBoxCol.DataPropertyName = "TANALYSIS"
comboBoxCol.HeaderText = "TELECONTROL ANALYSIS"
comboBoxCol.AutoSizeMode = DataGridViewAutoSizeColumnsMode.AllCells
comboBoxCol.DisplayStyle = DataGridViewComboBoxDisplayStyle.Nothing
gridObject = comboBoxCol
Case Else
textBoxCol = New DataGridViewTextBoxColumn
gridObject = textBoxCol
'Now add the columns to the grid
gridObject.DataPropertyName = column.ColumnName.ToString
gridObject.HeaderText = "ID"
gridObject.name = "ID"
gridObject.AutoSizeMode = DataGridViewAutoSizeColumnsMode.AllCells
End Select
'Now add the textbox columns to the grid
'gridObject.DataPropertyName = column.ColumnName.ToString
'gridObject.AutoSizeMode = DataGridViewAutoSizeColumnsMode.AllCells
.Columns.Add(gridObject)
End If
Next
'Set grid default styles/values
.Font = New System.Drawing.Font("Arial", 10, FontStyle.Regular)
.AllowUserToResizeColumns = True
.AllowUserToResizeRows = True
.AllowUserToAddRows = True
.ReadOnly = True
.AutoResizeRows()
.ClipboardCopyMode = DataGridViewClipboardCopyMode.EnableAlwaysIncludeHeaderText
'Now bind the datatable to the DGV datasource property
.DataSource = myTable
End With
_Logger.SendLog(Me.Name & "." & procName & " - Building of device data grid has been completed.", NLog.LogLevel.Trace, Nothing)
Catch ex As Exception
Throw
Finally
textBoxCol = Nothing
comboBoxCol = Nothing
readOnlyCellStyle = Nothing
GC.Collect()
End Try
End Sub
Private Sub PopulateUnboundComboBox(ByVal comboBoxCol As DataGridViewComboBoxColumn, byval valList() as string)
comboBoxCol.Name = "TANALYSIS"
comboBoxCol.DataSource = valList
comboBoxCol.ValueMember = ???
comboBoxCol.DisplayMember = ???
End Sub
EDITED//
Ok, I've made some progress and seem to have almost solved my issue. The one remaining bug is that the column in the DGV is displaying the 'ValueMember' (1 or 0) rather than the 'DisplayMember' (Yes or No). I've checked the valuemember and displaymember properties in the comboboxcolumn definition and and they appear to be set correctly. Here is the associated code:
Excerpt from original code posting listed above
...
Case "TANALYSIS"
gridObject = New Object
comboBoxCol = New DataGridViewComboBoxColumn
'First create the comboboxcolumn for the column
'Populate combobox
Dim item As New CBOItem
Dim itemList As New CBOItemList
item.ValueMember = 0
item.DisplayMember = "No"
itemList.Add(item)
item = New CBOItem
item.ValueMember = 1
item.DisplayMember = "Yes"
itemList.Add(item)
PopulateComboBox(comboBoxCol, itemList)
comboBoxCol.DataPropertyName = "TANALYSIS"
comboBoxCol.HeaderText = "TELECONTROL ANALYSIS"
comboBoxCol.AutoSizeMode = DataGridViewAutoSizeColumnsMode.AllCells
comboBoxCol.DisplayStyle = DataGridViewComboBoxDisplayStyle.Nothing
gridObject = comboBoxCol
...
Private Sub PopulateComboBox(ByRef comboBoxCol As DataGridViewComboBoxColumn, ByVal itemList As CBOItemList)
Dim tbl As DataTable = New DataTable
Dim row As DataRow
tbl.Columns.Add("ValueMember")
tbl.Columns.Add("DisplayMember")
row = tbl.NewRow
row.Item("ValueMember") = itemList(0).ValueMember
row.Item("DisplayMember") = itemList(0).DisplayMember
tbl.Rows.Add(row)
row = tbl.NewRow
row.Item("ValueMember") = itemList(1).ValueMember
row.Item("DisplayMember") = itemList(1).DisplayMember
tbl.Rows.Add(row)
comboBoxCol.ValueMember = tbl.Columns("ValueMember").ToString
comboBoxCol.DisplayMember = tbl.Columns("DisplayMember").ToString
comboBoxCol.DataSource = tbl
End Sub
Kind Regards
Paul J.

Adding data to a data table from textbox user input

When I run the project I add values into my textboxes that I want to go into the datatable but it doesn't have the newly inputted data from the textboxes or the new row when I check it out. Anyone know what I'm missing that puts in the data into my datatable? this is my sample code:
Dim lineTable As Data.DataTable = New Data.DataTable("NAFTALineItems")
lineTable.Columns.Add("NAFTAID")
lineTable.Columns.Add("NAFTALineID")
lineTable.Columns.Add("BoxQuantity")
lineTable.Columns.Add("Weight")
lineTable.Columns.Add("PartNumber")
lineTable.Columns.Add("PartDescription")
lineTable.Columns.Add("QuantityShipped")
lineTable.Columns.Add("UnitPrice")
lineTable.Columns.Add("TotalPrice")
Dim lineRow As DataRow = lineTable.NewRow()
lineRow("NAFTAID") = txtNaftaID.Text
lineRow("NAFTALineID") = txtLineID.Text
lineRow("BoxQuantity") = txtQuantity.Text
lineRow("Weight") = txtWeight.Text
lineRow("PartNumber") = txtPartNumber.Text
lineRow("PartDescription") = txtPartDescription.Text
lineRow("QuantityShipped") = txtQtyShipped.Text
lineRow("UnitPrice") = txtPrice.Text
lineRow("TotalPrice") = txtQtyShipped.Text * txtPrice.Text

Displaying a checkbox in a databound DataGridView

I am unable to correctly populate a DataGridView checkbox column from a boolean column from a database.
First form_load code:
Me.DataGridView1.DataSource = Me.bindingSource1
GetData("SELECT myInt, myBool, myString " & _
"FROM " & myFavTable & " " & _
"WHERE (myInt > 100) ORDER BY myString")
formatGrid()
In GetData I fill myTable with data:
Me.dataAdapter.Fill(myTable)
Me.bindingSource1.DataSource = myTable
And finally I format grid the before showing.
I format it manually because loading is much faster than with automatic formatting.
With DataGridView1
.AllowUserToAddRows = False
.AllowDrop = False
.AllowUserToOrderColumns = False
.AllowUserToResizeRows = False
.SelectionMode = DataGridViewSelectionMode.FullRowSelect
.MultiSelect = False
.Dock = DockStyle.Fill
.EditMode = DataGridViewEditMode.EditProgrammatically
With .Columns(0)
.Name = "postN"
.HeaderText = "Postal"
.Width = 55
End With
With .Columns(1) 'here should be a checkbox
.Width = 20
End With
With .Columns(2)
.Name = "colCity"
.HeaderText = "City"
.Width = 180
End With
End With
But with this code, in my column that should show checkboxes the string value 0 is displayed when in database is FALSE.
How in this situation can I get checkboxes in the middle column instead of text?
I try with .Columns.Add... before and after binding but with no wanted results.
That way I can get checkboxes, but in the new column.
In design-time add the columns to the DataGridView and set the middle column as a CheckBoxColumn.
Then set:
With DataGridView1
.AutoGenerateColumns = False
Edit:
I see the problem now. You need to set the DataPropertyName to be the same as the column.
When you add columns to the DataGridView, in that dialog set the DataPropertyName to match the DataTable (myTable) column Names. That's the magic behind the mapping.
Here is the code:
DataTable dt = new DataTable();
dt.Columns.Add("TextBoxCol");
dt.Columns.Add("CheckBoxCol");
DataRow dr = dt.NewRow();
dr[0] = "Hello";
dr[1] = false;
dt.Rows.Add(dr);
dr = dt.NewRow();
dr[0] = "World";
dr[1] = true;
dt.Rows.Add(dr);
dataGridView1.DataSource = dt;
I had the same problem.
I had a DataSet that it was filling with this SQL:
"SELECT nombre, CASE WHEN fecha IS NULL THEN 0 ELSE 1 END AS baja"
Assignment
dtgEmpleado.DataSource = ds.Tables(0)
With dtgEmpleado
.Columns(0).HeaderText = "Nombre"
.Columns(0).DataPropertyName = "nombre"
.Columns(0).Name = "nombre"
.Columns(0).Width = 100
.Columns(1).HeaderText = "Baja"
.Columns(1).DataPropertyName = "baja"
.Columns(1).Name = "baja"
.Columns(1).Width = 70
End With
I wanted that the column "Baja" it was displaying as a "Checkbox".
I could do it with:
AutoGenerateColumns = False
But an easier way, changing the SQL sentence:
"SELECT nombre, CAST(CASE WHEN fecha IS NULL THEN 0 ELSE 1 END AS BIT) AS baja"
Dim cell As DataGridViewCell = New DataGridViewCheckBoxCell()
With DataGridView1
With .Columns(1)
.CellTemplate = cell
End With
End With
EDIT:
This a suggestion, don't try to add columns at design-time in your DataGridView because you query itself it will generate a DataGridViewCheckBoxCell
GetData("SELECT myInt AS Id, myBool AS Bool, myString AS String " & _
"FROM " & myFavTable & " " & _
"WHERE (myInt > 100) ORDER BY myString")
Me.dataAdapter.Fill(myTable)
Me.bindingSource1.DataSource = myTable
DataGridView1.DataSource = bindingSource1;