Tag Archives: Gridview

DataBinding: ‘System.Data.DataRowView’ does not contain a property with the name due to “.” / Period in ColumnName

Recently I met up with error “DataBinding: ‘System.Data.DataRowView’ does not contain a property with the name”,  on page with dynamically generated Gridview on it.Error was coming at the runtime at timing of binding  the data.

I first checked for the columns in the dataset before binding to Gridview , all the columns are properly generated. then I started looking for the code which is generating gridview on fly. I was using following event to bind the data dynamically

protected void ValueDataBinding(object sender, EventArgs e)
{
object bound_value_obj = null;
Control ctrl = (Control)sender;
IDataItemContainer data_item_container = (IDataItemContainer)ctrl.NamingContainer;

bound_value_obj = DataBinder.Eval(data_item_container.DataItem, ColumnName); ( Exception was thown from above line while binding to column)

}

after much of debugging I found that ColumnName is  “Others…” and error was coming due to “.” / Period in ColumnName , the reason behind is  DataBinder.Eval Method Uses reflection to parse and evaluate a data-binding expression against an object at run time , so “.” can be treated with different , I havnt tried but I am sure it will throw the exception for other special chars also i.e. ;  , : ” ‘  etc.

This was solved by using

bound_value_obj =  DataBinder.GetPropertyValue(data_item_container.DataItem, ColumnName).ToString();


Advertisements

Create Serial no for records in Gridview / How to create a column with autogenerated Serial number of each row.

Well, I am not writing anything new or unknown here but it is new found for me!! Recently I had requirement in my project to show serial no as first column in GridView with all the data for understanding the number of rows in the grid view …

In this case we can take use of container’s DataItemIndex property which is keeping track of the index no of the rows bound to Gridsview

We can make use of it to show serial no. Just simple as follows (Details search local MSDN for “DataItemIndex” )

Consider following code I am using

<asp:GridView ID=”GridView1″ Width=860px Height=600px AllowPaging=”true” OnPageIndexChanging=”GridView1_PageIndexChanging” runat=”server”>

<Columns>

<asp:TemplateField>

<HeaderTemplate>Sr No. </HeaderTemplate>

<ItemTemplate> <%# Container.DataItemIndex + 1 %> </ItemTemplate>

</asp:TemplateField>

</Columns>

</asp:GridView>

We can see the it as follows –

Gridview With Serial No.
Gridview With Serial No.

We can also see its getting reflected irrespective of the Page Index –

Gridview with Page + Sr No.
Gridview with Page + Sr No.