[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1453
  • Last Modified:

Databinding Gridview error (System.Data.DataRowView)

I'm getting the error "DataBinding: 'System.Data.DataRowView' does not contain a property with the name 'Horas Norm'.". I've adapted some code found on the net, but I can't seem to get past this.
This is the class:
Public Class GridViewTemplate
  Implements ITemplate

  Dim _templateType As ListItemType
  Dim _columnName As String

  Public Sub New(type As ListItemType, colname As String)
    _templateType = type
    _columnName = colname
  End Sub

  Public Sub InstantiateIn(container As Control) Implements ITemplate.InstantiateIn
    Dim lbl As New Label
    Dim txt As New TextBox
    Select Case _templateType
      Case ListItemType.Header
        lbl.Text = _columnName
        Select Case _columnName
          Case "Nº"
          Case "Nome"
          Case "Admissão"
          Case "Demissão"
          Case "Categoria"
          Case "Vencimento"
          Case "Dias"
          Case "Motivo"
          Case "Início"
          Case "Fim"
          Case Else
        End Select
        container.Controls.Add(lbl)
      Case ListItemType.Item
        Select Case _columnName
          Case "Nº", "Nome", "Admissão", "Categoria", "Vencimento", "Dias", "Motivo", "Início", "Fim"
            AddHandler lbl.DataBinding, AddressOf tb1_DataBinding
            container.Controls.Add(lbl)
          Case Else
            AddHandler txt.DataBinding, AddressOf tb1_DataBinding
            txt.Columns = 20
            container.Controls.Add(txt)
        End Select
      Case ListItemType.EditItem
      Case ListItemType.Footer
    End Select
  End Sub

  Protected Sub tb1_DataBinding(sender As Object, e As EventArgs)
    If TypeOf sender Is TextBox Then
      Dim txtData As TextBox = sender
      Dim container As GridViewRow = CType(txtData.NamingContainer, GridViewRow)
      Dim dataValue As Object = DataBinder.Eval(container.DataItem, _columnName)
      If Not IsDBNull(dataValue) Then
        txtData.Text = dataValue.ToString()
      End If
    Else
      Dim txtData As Label = sender
      Dim container As GridViewRow = txtData.NamingContainer
      Dim dataValue As Object = DataBinder.Eval(container.DataItem, _columnName) 'error here
      If Not IsDBNull(dataValue) Then
        txtData.Text = dataValue.ToString()
      End If
    End If
  End Sub

End Class

Open in new window

This is the code that calls the class:
        For Each col As DataColumn In Table1.Columns
          Dim bfield As New TemplateField()
          bfield.HeaderTemplate = New GridViewTemplate(ListItemType.Header, col.ColumnName)
          bfield.ItemTemplate = New GridViewTemplate(ListItemType.Item, col.ColumnName)
          GridView1.Columns.Add(bfield)
        Next
        GridView1.DataSource = Table1
        GridView1.DataBind()

Open in new window

The original code I adapted (C#) can be found here: http://www.codeproject.com/KB/aspnet/create_template_columns.aspx
0
Cluskitt
Asked:
Cluskitt
1 Solution
 
sonawanekiranCommented:
Have you set gridview property autogenerate columns equals to false

AutoGenerateColumns="False"
0
 
CluskittAuthor Commented:
Yes
0
 
CluskittAuthor Commented:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" BorderColor="Black"
                BorderStyle="Solid" BorderWidth="1px" PageSize="50" CellPadding="0" Style="font-family: Arial;
                font-size: small" PagerSettings-PageButtonCount="50" EditRowStyle-Wrap="False"
                EmptyDataRowStyle-Wrap="False" FooterStyle-Wrap="False" AlternatingRowStyle-Wrap="False"
                PagerStyle-Wrap="False" RowStyle-Wrap="False" SelectedRowStyle-Wrap="False" SortedAscendingCellStyle-Wrap="False"
                SortedAscendingHeaderStyle-Wrap="False" SortedDescendingCellStyle-Wrap="False"
                SortedDescendingHeaderStyle-Wrap="False" AllowPaging="True">
                <AlternatingRowStyle Wrap="False" />
                <EditRowStyle Wrap="True" />
                <EmptyDataRowStyle Wrap="False" />
                <FooterStyle Wrap="False" />
                <HeaderStyle Font-Bold="True" Font-Names="Arial" Font-Size="X-Small" HorizontalAlign="Center"
                  VerticalAlign="Middle" />
                <PagerSettings PageButtonCount="50" />
                <PagerStyle Font-Names="Arial" Font-Size="X-Small" VerticalAlign="Middle" Wrap="True" />
                <RowStyle Font-Names="Arial" Font-Size="X-Small" Wrap="False" />
                <SelectedRowStyle Wrap="False" />
                <SortedAscendingCellStyle Wrap="False" />
                <SortedAscendingHeaderStyle Wrap="False" />
                <SortedDescendingCellStyle Wrap="False" />
                <SortedDescendingHeaderStyle Wrap="False" />
              </asp:GridView>

Open in new window

0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
CluskittAuthor Commented:
Ok, I found out why the error is generated, now I need to find a way to fix it:
The columns are created dynamically and their names are taken from a table. The problem is that I had a name 'Horas Norm.' and the code was searching for 'Horas Norm' (without the dot). This also happens for names with parenthesis.
Now, I know I can simply remove those from the names and it will work, but I would like to get it working with special characters. Any idea how?
0
 
CodeCruiserCommented:
> but I would like to get it working with special characters.

You have special characters in column names in DB?
0
 
ddayx10Commented:
Well lots of special characters are allowed in column names, but its sort of a frowned on practice.

Anyway to get SQL to accept your column names you can try surrounding them with square brackets (in your case using some sort of concatenation probably).

Ex.

[Column#Name]

Dim dataValue As Object = DataBinder.Eval(container.DataItem, "["& _columnName & "]") 'error here
0
 
CluskittAuthor Commented:
I don't use special characters as column names. What happens is that my dynamic SQL query (stored procedure) generates the column names dynamically based on a table field. In that table, I have normal descriptions, including special characters, like () and dots.
0
 
ddayx10Commented:
OK lets replace the term "special characters" with non-standard choices. I used the term bc you had used it yourself above :)

Never-the-less if I read the above right your error is occuring in your SQL statement because SQL is not understanding the syntax due to your column name irregularities ('Horas Norm.). These irregularities can usually be ironed out by surrounding the syntax (if its a column name) with square brackets as I pointed out [Horas Norm.]

If this does not resolve your issue then there is more going on than you mentioned in response 37023693 above.
0
 
CluskittAuthor Commented:
The problem seems to be that, in this line:
Dim dataValue As Object = DataBinder.Eval(container.DataItem, _columnName)
_columnName has a value of "Horas Norm.", but the error generated complains that there is no column named "Horas Norm" (without the dot). Same thing for columns that have parenthesis.
0
 
ddayx10Commented:
Well none of the code you show displays where _columnName/ColumnName is being set only where it is being read from.

Something is happening when you are setting this property/variable that is removing the dot from the end of [Horas Norm.]

So as you pointed out the column exists in the table as [Horas Norm.] but your property is different.

Start looking at the areas where this property is set/instantiated.
0
 
CluskittAuthor Commented:
I did post the code where _columnName is being set. The first part is the class, that has a new() constructor where that variable is set. And in the code below I have:
bfield.ItemTemplate = New GridViewTemplate(ListItemType.Item, col.ColumnName)
Which passes along the column name of the DataTable object.
Anyway, that isn't much relevant. In the same line, seen at debug, _columnName has a value with the dot, but the error of the eval returns the value without the dot.
So the error is somewhere in that line itself.
0
 
ddayx10Commented:
Yes I saw the default constructor and that is how the implementation runs, but that doesn't show me what was passed to the constructor to set the value _ColumnName.

I will agree that that is not relevant if you stop the debugger on line 57 (from your code above) and the value for _columnName includes the dot. The datatable didn't misinterpret the columnname etc and then pass it along incorrectly for instance.

Anyway what you are saying is that in this line:
bfield.HeaderTemplate = New GridViewTemplate(ListItemType.Header, col.ColumnName)

You are sure the column name is coming out of the datacolumn correctly. And on line 58 of your code it is being passed in correctly with the dot (in this example).

Then you are getting an error from the eval which shows the columnname without the dot?

The error looks like this:

DataBinding: 'System.Data.DataRowView' does not contain a property with the name 'Horas Norm'

And did you ever try surrounding the _columnName with square brackets, or did you just dismiss that suggestion?

If the above scenario is true where the correct _columnName is being passed at line 58 for sure, and square brackets around the column (which is standard syntax to correct this exact situation) does not work then basically you're stuck because you can't really get in there easily and start overriding the Databinder.Eval method. At least that doesn't seem like a practical ideal.
0
 
CluskittAuthor Commented:
I've tried sending with []:
bfield.ItemTemplate = New GridViewTemplate(ListItemType.Item, "[" & col.ColumnName & "]")
It generates an error which I'm not sure how to translate. Something like: "The destination of an invocation triggered an exception". This when I have () on the fieldname. When I have a dot, it complains that "[Horas Norm" isn't a valid indexed expression, or something like that. Again, ignoring everything after the dot.
0
 
CluskittAuthor Commented:
Well, I guess this is the closest to an answer. We ended up trying new ways, as we can't afford to waste too much time on this. At a first stage, we're going to simply create half a dozen static pages, similar but with some differences and just redirect each user to the correct page. Also, any change in the underlying structure will have to be done on all of them.
Later on, we'll study again a process to go back to dynamic SQL.
0

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now