I know this question was answered, in C# and I have been trying to convert and get it to work, but have not been succesful?
I would greatly appreciate if you helped
this is an image from that question, i want to do something similar.
here is the link
ASP.NET GridView second header row to span main header row
Dim d As Date = Date.Today
d = d.AddDays(-1)
Label1.Text = d
'connects to datawarehouse
saocmd1.Connection = conn1
conn1.Open()
Dim ds As New DataSet
'selects sql query
'saocmd1.CommandText = MYQUERY"
saoda1.Fill(saods1, "salesasoftable")
Dim row As New GridViewRow(0, -1, DataControlRowType.Header, DataControlRowState.Normal)
Dim left As TableCell = New TableHeaderCell()
left.ColumnSpan = 3
row.Cells.Add(left)
Dim totals As TableCell = New TableHeaderCell()
totals.ColumnSpan = gridview1.Columns.Count - 3
totals.Text = "Totals"
row.Cells.Add(totals)
my error
Specified argument was out of the range of valid values.
Parameter name: index
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.ArgumentOutOfRangeException: Specified argument was out of the range of valid values.
Parameter name: index
Source Error:
Line 54: row.Cells.Add(totals)
Line 55:
Line 56: Dim t As Table = TryCast(gridview1.Controls(0), Table)
Line 57: If t IsNot Nothing Then
Line 58: t.Rows.AddAt(0, row)
The answer
Dim row As New GridViewRow(0, -1, DataControlRowType.Header, DataControlRowState.Normal)
'spanned cell that will span the columns I don't want to give the additional header
Dim left As TableCell = New TableHeaderCell()
left.ColumnSpan = 6
row.Cells.Add(left)
'spanned cell that will span the columns i want to give the additional header
Dim totals As TableCell = New TableHeaderCell()
totals.ColumnSpan = myGridView.Columns.Count - 3
totals.Text = "Additional Header"
row.Cells.Add(totals)
'Add the new row to the gridview as the master header row
'A table is the only Control (index[0]) in a GridView
DirectCast(myGridView.Controls(0), Table).Rows.AddAt(0, row)
From your question, it just seems that you are having trouble converting c# to vb.net. Here are 2 online converters that can help out.
http://www.developerfusion.com/tools/convert/csharp-to-vb/
http://converter.telerik.com/
Or tangible software has a converter that can convert entire projects, but note that it is not free.
The simplest method is to handle this on the RowCreated event. You would test the RowType being handled, and if it is the header, you then:
Clear the row cell(s) in question.
Adjust the Rowspan of the first cell to 2 (in the case of your image: Qty Shipped).
Remove the row cell immediately following (in the case of your image: Total Sales).
Create a new table to look like the one you want as shown.
Insert your new table into the row cell desired.
If you already have the solution you wish to use, but it's in C# and you can't convert it, try this tool: http://www.developerfusion.com/tools/convert/csharp-to-vb/
Related
I have an Excel file which I need to read through and extract particular values of a certain range into a datatable so I can then save that data into a table in a database.
Whilst debugging, on every loop I check the datatable visualizer to see what's going on and I find that I'm appending values of a different row, to the same row. Example in photo.
SamplePhoto
Here is the code responsible for that action.(Surrounded in a Try-Catch)
Using excel As New ExcelPackage (ulTarget.PostedFile.InputStream)
Dim _worksheet = excel.Workbook.Worksheets.First()
Dim _hasHeader = True
For Each cell In _worksheet.Cells(1,2,147,4)
_dataTable.Columns.Add(If(_hasHeader, cell.Value, String.Format("{0}", cell.Start.Column)))
If _worksheet.Cells.Value Is Nothing Then
Continue For
Next
Assume that the range 1,2,147,4 is correct as the data going into the datatable is correct, the row seperation is simply the problem. _dataTable is my DataTable (obvious I know but nothing bad in clarifying it) and _hasHeader is set to True because the Excel worksheet being uploaded has headers and I don't want them being put into the DataTable because the data will all end up in a table in SQL Server where appropriate column names exist.
Also ulTarget is my File uploader. I am using EPPlus most recent version for this.
Anybody have any suggestions as to how I can seperate the data into rows as per the example in the photo above? Happy to make any clarifications if needed.
Added the horizontal range of the columns without headers and read each cell row by row.
Using excel As New ExcelPackage(ulTargetFile.PostedFile.InputStream)
'Add the columns'
Dim _worksheet = excel.Workbook.Worksheets.First()
Dim _hasHeader As Boolean = False
For Each col In _worksheet.Cells("B1:D1")
_dataTable.Columns.Add(If(_hasHeader, Nothing, col.Value))
String.Format("{0}", col.Start.Column)
Next
'Add the rows'
For rowNumber As Integer = 1 To 147
Dim worksheetRow = _worksheet.Cells(rowNumber, 2, rowNumber, 4)
Dim row As DataRow = _dataTable.Rows.Add()
For Each cell In worksheetRow
row(cell.Start.Column - 2) = cell.Value
Next
Next
Good morning stackers!
I'm designing a massive update page. Here are the general steps:
I have a class called item which has two properties: Equipment number and new due date.
I have a textbox where i paste values from Excel, the values consist of two columns divided by a vbtab character: the columns are an equipment number and a new due date
A button is clicked and the values from textbox are parsed into a list(of item) and the equipment master builds a string for an SQL criteria for a commandtext.
The command fills a dataset from a database which gets equipment number and current due date.
I add manually a column to the dataset (new due date)
I iterate over the rows of the dataset, and i use the list(of item) find method matching equipment from the list and from the database to get a new due date from the textbox parsed values.
Everything is going well, except that when using the find method for more than 1 row in the dataset, the method fails:
Here is the code from point 5 and 6:
da.Fill(ds, "Equipments")
dt = ds.Tables(0)
ds.Tables(0).Columns.Add("column_1", Type.GetType("System.DateTime"))
Dim rw As DataRow
For Each rw In ds.Tables(0).Rows
Dim strsearch As String = rw(0).ToString
Dim fequnumb As item = myItemList.Find(Function(p) p.EquipNumber = strsearch)
rw(2) = fequnumb.DueDate <- Error occurs here
Next
Again, if instead of ftechid.DueDate I put a static value like Today()the code runs fine for the loop and fills correctly the gridview, but if i leave the ftechid.DueDate then an error is thrown after the first row:
Object reference not set to an instance of an object.
Any help is very much appreciated as to how to use the find method inside a for..each loop
If the Find function cannot match the string requested it returns Nothing and then you cannot set the due date from a variable that is Nothing. If this is a condition expected from the input then you need to protect the assignment to the new column with something like this.
For Each rw In ds.Tables(0).Rows
Dim strsearch As String = rw(0).ToString
Dim fequnumb As item = myItemList.FirstOrDefault(Function(p) p.EquipNumber = strsearch)
if fequnumb IsNot Nothing Then
rw(2) = fequnumb.DueDate <- Error occurs here
End if
Next
If this is not supposed to happen then you need to check your inputs
I'm new to ASP.NET VB, and i'm having trouble and dont really understand the code of putting in row seperators in a gridview. I have 5 columns and the fifth columns rows field text is what i would like inserted into the new seperator row. There may be more than one per question. To populate my gridview i'm calling upon a stored procedure "Select_Problems" and carrying a persons name from another page through "SvarRecord" and matching it up with the SP #Name. The fifth column in the SP has four possibilities "1. Person upset?", "2. Is Person is resisting?", and so on. My code is as follows and i would like to have an easy way to do this but to make it look nice along with a color to divide each question. Thank you
Public Sub LoadGrid2()
Dim MyConn As New SqlConnection((ConfigurationManager.ConnectionStrings("Customer_Service").ConnectionString))
Dim MyCmd As New SqlDataAdapter("Select_Problems", MyConn)
MyCmd.SelectCommand.CommandType = CommandType.StoredProcedure
MyCmd.SelectCommand.Parameters.Add(New SqlParameter("#Name", SqlDbType.VarChar))
MyCmd.SelectCommand.Parameters("#Name").Value = Session("SvarRecord")
Dim ds As New DataSet
MyConn.Open()
MyCmd.Fill(ds, "Overview")
GridView2.DataSource = ds
GridView2.DataBind()
End Sub
I know your supposed to do this in the Rowdatabound of the Gridview but I dont understand the code behind it to do this which would be great to do.
In the RowDataBound event, you could check the index of the row currently being bound and determine if it is the last row in a set of 5 and then apply styling to the row to give a visual cue of separation without actually generating a blank separator row, like this:
Protected Sub GridView2_RowDataBound(sender As Object, e As GridViewRowEventArgs)
' Add a visual separator via CSS border property to every 5th row
If e.Row.RowIndex Mod 5 = 0 Then
' Apply style to each cell in the row
For Each cell As TableCell In e.Row.Cells
cell.Attributes.Add("Style", "border-bottom: red 5px solid")
Next
End If
End Sub
Note: My math might be off by one so you may need to change the styling to the top of the row (border-top instead of border-bottom), but the idea is the same.
I don't know why this isn't working. I'm trying to create a table header section from the back end code, but everything is going into tbody.
Dim output As New Web.UI.WebControls.Table
'Create the header row
Dim hRow As New Web.UI.WebControls.TableHeaderRow
hRow.TableSection = Web.UI.WebControls.TableRowSection.TableHeader
hRow.Controls.Add(New Web.UI.WebControls.TableHeaderCell)
For Each d As GridDate In Dates
Dim hCell As New Web.UI.WebControls.TableHeaderCell
hCell.Text = d.Value
hRow.Controls.Add(hCell)
Next
output.Controls.Add(hRow)
The result is everything under tbody despite creating a header row and setting the section property to header. What am I doing wrong?
There was an error in the code I posted. In the last line of my code I was appending the new row to the controls collection:
output.Controls.Add(hRow)
Don't do this. It seems to bypass some of the properties unique to ASP.NET TableRows in final rending. In this case, it was ignoring the TableSection property despite being set correctly. You should append rows to the Rows collection instead:
output.Rows.Add(hRow)
Try this
Dim output As New Table
Dim hRow As New TableHeaderRow
For Each d As GridDate In Dates
Dim hCell As New TableHeaderCell
hCell.Text = d.Value
hCell.Scope = TableHeaderScope.Column
hRow.Cells.Add(hCell)
Next
output.Rows.Add(hRow)
This worked for me
I am building an ASP.NET application that needs dynamic tables. That's another issue that I've already posted about (and gotten a pretty good response!). Now, I'm running into another issue - I want to add new rows to my table, but given that I will have 10-12 tables on one page, each containing different objects in their rows (text boxes, check boxes, etc.) I need a way of simply generically adding a new row that has the same objects as the first row in the table. Here's my code:
Private Sub AddTableRow(ByRef originalTable As System.Web.UI.WebControls.Table)
Dim originalRow As System.Web.UI.WebControls.TableRow = originalTable.Rows(1)
Dim insertingRow As New System.Web.UI.WebControls.TableRow
Dim insertingCells(originalRow.Cells.Count) As System.Web.UI.WebControls.TableCell
Dim index As Integer = 0
For Each cell As System.Web.UI.WebControls.TableCell In originalRow.Cells
insertingCells(index) = New System.Web.UI.WebControls.TableCell
insertingCells(index).Controls.Add(cell.Controls.Item(0))
index += 1
Next
insertingRow.Cells.AddRange(insertingCells)
originalTable.Rows.Add(insertingRow)
End Sub
But I'm getting a null reference exception at the second to last line,
insertingRow.Cells.AddRange(insertingCells)
...and I can't figure out why. Is it because the contents of each cell are not being initialized with a new object? If so, how would I get around this?
Thanks!
EDIT:
The inside of my for loop now looks like this -
For Each cell As System.Web.UI.WebControls.TableCell In originalRow.Cells
Dim addedContent As New Object
Dim underlyingType As Type = cell.Controls.Item(0).GetType
addedContent = Convert.ChangeType(cell.Controls.Item(0), underlyingType)
insertingCells(index) = New System.Web.UI.WebControls.TableCell
insertingCells(index).Controls.Add(addedContent)
index += 1
Next
Stepping through with a debugger, I see that this strategy is working - but the additional table row still doesn't appear...and still does when I do this statically.
I think your culprit may be this line:
Dim insertingCells(originalRow.Cells.Count) As TableCell
Confusingly, the number you specify in an array declaration in VB.NET is the upper bound, not the number of elements. So Dim ints(10) As Integer will create an Integer() array with eleven elements, not ten (10 will be the highest index of the array).
Try this instead:
Dim insertingCells(originalRow.Cells.Count - 1) As TableCell