Changing from a datagridview to a simple textbox - vb.net

I have a program that puts a cell of data from an excel file and writes it to a datagridview cell. However since I'm only trying to write one cell of data I'm thinking it might be a better Idea to write the data from the excel file to a simple textbox.
I'm having trouble finding out how to interface a simple textbox with an excel file. If anyone has some suggestions on how I might accomplish this I would greatly appreciate it.
Below is my current code for working with my datagridview.
'This code sample here uses a for next loop to match the excel column and rows up with the datagridview's rows and columns.
Dim rowindex As Integer
Dim columnindex As Integer
For rowindex = 1 To DataGridView3.RowCount
For columnindex = 1 To DataGridView3.ColumnCount
objworksheet3.Cells(rowindex + 3, columnindex + 1) = DataGridView3(columnindex - 1, rowindex - 1).Value
Next
Next
'This code uses the oledatadapter to pull only the cells that I want from the excel file to populate the datagridview cell.
MyCommand3 = New OleDbDataAdapter("select * from [myWorksheet$B3:B4]", MyConnection)
ds3 = New System.Data.DataSet()
MyCommand3.Fill(ds3)

For rowindex = 1 To DataGridView3.RowCount
I suggest, For rowindex = 0 To DataGridView3.RowCount -1

You can get data directly from your dataset; for example to retrieve data from the first row of the first column:
Textbox1.text = ds.Tables(0).Rows(0)(0).ToString
if you want to fetch more cells you can cycle through rows and columns

Related

How to add a new row into DataGridView and fill it with data

This is my code
Public Sub InvoicItmCall()
'If IteDataTb.Rows.Count = 0 Then Exit Sub
SellingDGView.Rows.Add()
SellingDGView.Rows.Item(0).Cells(0).Value = SellingDGView.Rows.Item(0).Cells(0).Value + 1
SellingDGView.Rows.Item(0).Cells(1).Value = IteDataTb.Rows(0).Item("IteFName")
SellingDGView.Rows.Item(0).Cells(2).Value = IteDataTb.Rows(0).Item("IteFSizeUnit")
SellingDGView.Rows.Item(0).Cells(4).Value = IteDataTb.Rows(0).Item("IteFSellpris")
SellingDGView.Rows.Item(0).Cells(6).Value = IteDataTb.Rows(0).Item("IteVat")
'Dim unused = SellingDGView.Rows.Add(rows)
End Sub
Right now, the code works fine and fills the first row on the grid, this is for a barcode scanner. When I scan one barcode, it fills the row with the appropriate data. However, when I scan another code, it overwrites the older row.
I want it to add a new row and add the other data to it.
So when I scan a different item after the first one, it should fill up a new row.
How do I do that?
Thanks in advance :)
EDIT:
I have found a workaround
If IteDataTb.Rows.Count > 0 Then
For Each row As DataRow In IteDataTb.Rows
SellingDGView.Rows.Add(row.Item("IteFName"), row.Item("IteFSizeUnit"), row.Item("IteFSellpris"), row.Item("IteVat"))
Next row
End If
This creates a new row every time and fills it. But now I want to fill specific cells.
How do I do that?
Right now it fills everything inappropriately.
If you wanted to do it the way you originally were, I'd do it like this:
Dim index = SellingDGView.Rows.Add()
Dim newRow = SellingDGView.Rows(index)
'Dim newRow = SellingDGView.Rows(SellingDGView.Rows.Add())
Dim dataRow = IteDataTb.Rows(0)
newRow.Cells(0).Value = SellingDGView.Rows(0).Cells(0).Value + 1
newRow.Cells(1).Value = dataRow("IteFName")
newRow.Cells(2).Value = dataRow("IteFSizeUnit")
newRow.Cells(4).Value = dataRow("IteFSellpris")
newRow.Cells(6).Value = dataRow("IteVat")
If you wanted to use the more appropriate overload of Add that I mentioned and you used in your second example:
For Each row As DataRow In IteDataTb.Rows
SellingDGView.Rows.Add(SellingDGView.Rows(0).Cells(0).Value + 1,
row("IteFName"),
row("IteFSizeUnit"),
Nothing,
row("IteFSellpris"),
Nothing,
row("IteVat"))
Next
The If statement is pointless.
Also, I doubt that you're getting the next ID in the best way with that expression that adds 1 but that's a different question.
Every time that you need a new row, you can do as follows:
Dim i As Integer = SellingDGView.Rows.Add()
SellingDGView.Rows.Item(i).Cells(0).Value = i
SellingDGView.Rows.Item(i).Cells(1).Value = IteDataTb.Rows(j).Item("IteFName")
SellingDGView.Rows.Item(i).Cells(2).Value = IteDataTb.Rows(j).Item("IteFSizeUnit")
SellingDGView.Rows.Item(i).Cells(4).Value = IteDataTb.Rows(j).Item("IteFSellpris")
SellingDGView.Rows.Item(i).Cells(6).Value = IteDataTb.Rows(j).Item("IteVat")
Herein, I've assumed that the variable "j" indicates to your desired row in IteDataTb datagridview. I mean that you should control the "j" variable yourself. I can help you if know more about the IteDataTb.

How to fill a combobox in a datagridview

I have a datagrid view with 10 columns. It includes 2 checkbox columns followed by a combobox and then a number of text boxes for data entry. I do not have a database to load the combobox drop down but I do have a variable with 19 rows. I have tried a number of methods from SO but haven't been able to get this to work correctly so I can load the combobox for the user to select a value.
The code I've been using is like this. I have tried several different ways that are commented out as well...
' Build datagridview row
'
Dim t1 As New DataTable
For Each col As DataGridViewColumn In dgvMultiSelect.Columns
t1.Columns.Add(col.HeaderText)
Next
Dim dgvcb As New DataTable
dgvcb.Columns.Add("RunID", GetType(String))
For el = 0 To sRunID.Length - 1
dgvcb.Columns.Add(sRunID(el))
RunID.Items.Add(sRunID(el))
Next
' RunID.DataSource = dgvcb
' RunID.DataPropertyName = "dgvcb"
' RunID.DataSource = sRunID
' RunID.DataPropertyName = "sRunID"
'Dim chk As New DataGridViewCheckBoxColumn()
'DataGridView1.Columns.Add(chk)
'chk.HeaderText = "Check Data"
'chk.Name = "chk"
dgvMultiSelect.Rows(0).Cells(0).Value = True
The checkbox works fine (it shows as checked) and I was able to set the combobox value to show but clicking on the dropdown does nothing. I believe the data is in RunID (the column in the dgv.
Well, the answer that worked for me is: I was referencing the datagridviewCOLUMN and not a datagridviewCOMBOBOXcolumn. Thanks to Sai Kalyan Kumar Akshinthala!
' Build datagridview row
Dim dgvcc As DataGridViewComboBoxColumn
dgvcc = dgvMultiSelect.Columns("RunID")
For el = 0 To sRunID.Length - 1
dgvcc.Items.Add(sRunID(el))
dgvMultiSelect.Rows(0).Cells(2).Value = sRunID(el)
Next
Maybe next time you'll try to understand before voting down. Such a silly thing this voting down anyway. It should not reduce reputation.

Looking to export datagridview

I have a scrolling datagridview that dyanmically changes it's number of columns and rows. I'm looking for a way to export the datagridview as it is when the user clicks a button. I want it to export as something that cannot be edited (so not excel file). I've tried using iTextSharp to export as pdf and can't seem to come up with a dynamically changing loop that would suit it. Also, I haven't been able to find any sample code using iTextSharp that included also exporting row headers along with column headers. I've also tried one solution (that I can't seem to find at the moment) on the microsoft forums that takes advantage of a the add-on functionality of Excel to write to PDF after the datagridview contents are exported to it. The problem I have with that is it creates way to many pages as well as still shows rows that have been hidden or deleted from the datagridveiw. Any idea of how I can accomplish this feat?
I'll include two pictures to show the way in which the datagridview is dynamically populated and changes
It can have up to 66 rows and 12 columns (not including row or column headers)
You can use EPPlus to create an password-protected (locked) excel file that can be read, but not edited. I can show you how to start from a situation like this:
and get a protected file like this:
First you must download and include the EPPlus Library in your project. Don't forget to check the great EPPlusSamples Project
Then you need a method to extract your DataGridView VISIBLE data (you don't want the invisible column to be exported). I did it using a 2d array. This function accept a DataGridView parameter and returns a 2d array:
Function GetDataGridView2DArray(ByVal dataGridView As DataGridView) As String(,)
'Save list of visible columns
Dim nrVisibleColumns = (From c As DataGridViewColumn In DataGridView1.Columns
Where c.Visible
Select c).ToList()
'create 2d-array to store values, dimensions given by number of rows and visible columns
Dim dgvArray(nrVisibleColumns.Count, DataGridView1.Rows.Count) As String
'create the first row with Column Headers text
For Each col As DataGridViewColumn In nrVisibleColumns
dgvArray(col.Index + 1, 0) = col.HeaderText
Next
'create Rows, including Row Header text
For Each row As DataGridViewRow In DataGridView1.Rows
Dim rowNumber = row.Index + 1
dgvArray(0, rowNumber) = DataGridView1.Rows(row.Index).HeaderCell.Value 'save rowheader cell value
For Each col As DataGridViewColumn In nrVisibleColumns
dgvArray(col.Index + 1, rowNumber) = DataGridView1(col.Index, row.Index).Value
Next
Next
Return dgvArray
End Function
Now that you have your array you can create an Excel File and fill it with data. Plus, before saving, we'll lock it with a password to prevent user editing.
Private Sub CreateXls()
Dim fi = New FileInfo("C:\temp\output.xlsx")
Dim package As New ExcelPackage()
Dim ws = package.Workbook.Worksheets.Add("Dgv Output") 'create sheet
'get array of datagridview data
Dim dataArray = GetDataGridView2DArray(DataGridView1)
'loop my 2d array and fill my excel file
For iColumn As Integer = dataArray.GetLowerBound(0) To dataArray.GetUpperBound(0)
For iRow As Integer = dataArray.GetLowerBound(1) To dataArray.GetUpperBound(1)
ws.Cells(iRow + 1, iColumn + 1).Value = dataArray(iColumn, iRow)
ws.Cells(iRow + 1, iColumn + 1).Style.Locked = True 'lock the cell
Next
Next
ws.Cells.AutoFitColumns() 'resize columns to fit
ws.Protection.AllowFormatColumns = True 'let user resize columns if he wants
ws.Protection.SetPassword("1") 'protect the sheet from editing
package.SaveAs(fi) 'save file
End Sub
Now you should be able to easily export your dynamic DataGridView with one click.
This is My Coding Tested And Output is Perfect :
Dim ExcelApp As Object, ExcelBook As Object
Dim ExcelSheet As Object
Dim i As Integer
Dim j As Integer
'create object of excel
ExcelApp = CreateObject("Excel.Application")
ExcelBook = ExcelApp.WorkBooks.Add
ExcelSheet = ExcelBook.WorkSheets(1)
With ExcelSheet
For Each col As DataGridViewColumn In Me.DataGridView1.Columns
ExcelSheet.Cells(1, col.Index + 1) = col.HeaderText.ToString
For i = 1 To Me.DataGridView1.RowCount
ExcelSheet.cells(i + 1, 1) = Me.DataGridView1.Rows(i - 1).Cells("First Column Name").Value
For j = 1 To DataGridView1.Columns.Count - 1
ExcelSheet.cells(i + 1, j + 1) = DataGridView1.Rows(i - 1).Cells(j).Value
Next
Next
Next
End With
ExcelApp.Visible = True
ExcelSheet = Nothing
ExcelBook = Nothing
ExcelApp = Nothing

Make particular column as combo box of DataGridView

I just want to make Data Grid. Where first and second column of data grid is for storing single value and third column as combo box.
The code that i tried is
Dim productGrid As New DataGridView
ProductGrid.Columns(0).Name = "CB"
ProductGrid.Columns(1).Name = "ProductGroup"
ProductGrid.Columns(2).Name = "Product"
Dim i As Integer
With ProductGrid
If .Rows.Count = 0 Then Exit Sub
i = 1
Dim objValueItems As New DataGridViewComboBoxCell
objValueItems.Items.Add("Server")
objValueItems.Items.Add("Standalone")
objValueItems.Items.Add("Demo")
objValueItems.Items.Add("Anywhere")
ProductGrid.Item(2, i).Value = objValueItems
End With
I am getting the error on "ProductGrid.Item(2, i).Value = objValueItems" this line. Error is " Index was out of range.
Bear in mind that all the cells of a ComboBox DGV Column have the same contents, thus you are not assigning a list of items to a given cell, but to all of them (to a column). You have to include this when adding the given column. Sample code:
Dim productGrid As New DataGridView
With productGrid
.Columns.Add("CB", "CB") 'Text-type column
.Columns.Add("ProductGroup", "ProductGroup") 'Text-type column
Dim objValueItems As New DataGridViewComboBoxColumn
With objValueItems
.Name = "Product"
.Items.Add("Server")
.Items.Add("Standalone")
.Items.Add("Demo")
.Items.Add("Anywhere")
End With
.Columns.Add(objValueItems) 'ComboBox-type column
End With

VB2010: Export datagrid to Excel only retieving one row

I just want to mention first, I do appreciate everyone who gives of their knowledge to help others learn...
I have been able to piece together code that exports my Datgrid to an Excel file and works fine, except I am only getting the first row of the datagrid. since the datagrid could contain one record, or hundreds, I need all rows to export to Excel.
Here is what I have been able to put together (again, it works fine, file is saved with column headers, but only writes the first row of data from my datagrid):
'Export to Excel
Dim ExApp1 As Excel.Application
Dim ExWkbk1 As Excel.Workbook
Dim ExWksht1 As Excel.Worksheet
Dim MisValue As Object = System.Reflection.Missing.Value
Dim i As Integer
Dim j As Integer
ExApp1 = New Excel.Application
ExWkbk1 = ExApp1.Workbooks.Add(MisValue)
ExWksht1 = ExWkbk1.Sheets("sheet1")
For i = 0 To dg7.RowCount - 2
For j = 0 To dg7.ColumnCount - 1
ExWksht1.Cells(i + 2, j + 1) = dg7(j, i).Value.ToString()
For k As Integer = 1 To dg7.Columns.Count
ExWksht1.Cells(1, k) = dg7.Columns(k - 1).HeaderText
ExWksht1.Cells(i + 2, j + 1) = dg7(j, i).Value.ToString()
Next
Next
Next
ExWksht1.SaveAs("C:\MyExcel.xlsx")
ExWkbk1.Close()
ExApp1.Quit()
releaseObject(ExApp1)
releaseObject(ExWkbk1)
releaseObject(ExWksht1)
Again, many thsanks in advance for helping me learn
First of all it seems to me this is a DataGridView as opposed to a DataGrid since from what I know DataGrid does not have "RowCount" or "ColumnCount" properties in either WinForms or Web.UI (I could be wrong).
If it is so is there a specific reason for
dg7.RowCount - 2
If the object you are trying to get data from is indeed a DataGridView then I suppose this task is to omit new(empty) rows. If I am correct that you are trying to export data from a DataGridView this way there is a property for the DataGridViewRow that will serve this purpose:
IsNewRow
So (while I have not used Excel in VB.NET recently) your cycle could be:
For i = 0 To dg7.ColumnCount - 1
ExWksht1Cells(1 , i + 1) = dg7.Columns[i].HeaderText
Next
For i = 0 To dg7.RowCount - 1
If Not dg7.Rows(i).IsNewRow Then
For j = 0 To dg7.ColumnCount - 1
ExWksht1.Cells(i+ 2, j + 1) = dg7(i, j).Value.ToString()
Next
End If
Next
As far as the excel object calls I can't give a reasonable comment since I've not used that object too much.Let me know if that's useful and/or correct.