Live Chat Icon For mobile
Live Chat Icon

I get the error message ‘DataGrid with id ‘DataGrid1′ could not automatically generate any columns from the selected data source’ when I bind a HashTable to the DataGrid

Platform: ASP.NET| Category: Collections and Lists

Set the AutoGenerateColumns property of the DataGrid to False.Sample code below
VB.NET


<asp:DataGrid AutoGenerateColumns=False   id='DataGrid1' runat='server'>
<Columns >
	<asp:TemplateColumn HeaderText='HashTable'>
		 <ItemTemplate>
                		<%# Container.DataItem.Key %> <br>
		                <%# Container.DataItem.Value %>
		</ItemTemplate>
	</asp:TemplateColumn>
</Columns>
</asp:DataGrid>

’In Page_Load
Dim ht As New Hashtable()
ht.Add('emp1', 'A')
ht.Add('emp2', 'B')
ht.Add('emp3', 'C')

DataGrid1.DataSource = ht
DataGrid1.DataBind()

C#


<asp:DataGrid AutoGenerateColumns=False   id='DataGrid1' runat='server'>
<Columns >
	<asp:TemplateColumn HeaderText='HashTable'>
		<ItemTemplate>
		                <%# ((DictionaryEntry)Container.DataItem).Key %><br>
		                <%# ((DictionaryEntry)Container.DataItem).Value %>
	               </ItemTemplate>
	</asp:TemplateColumn>
</Columns>
</asp:DataGrid>

//In Page_Load
Hashtable ht = new Hashtable();
ht.Add ('emp1', 'A');
ht.Add ('emp2', 'B');
ht.Add ('emp3', 'C');

DataGrid1.DataSource = ht;
DataGrid1.DataBind();

Share with

Related FAQs

Couldn't find the FAQs you're looking for?

Please submit your question and answer.