I am binding the DataGrid to a datasource at runtime. After binding I want to populate a listbox with the column headers from the datagrid. How can I reference the column headertext for each column in the datagrid and then add them to my listbox

Platform: ASP.NET| Category: DataGrid

In the ItemDataBound Event write following code
VB.NET


Dim i As Integer = 0
If e.Item.ItemType = ListItemType.Header Then
	For i = 0 To e.Item.Cells.Count  -1
                	Me.ListBox1.Items.Add(e.Item.Cells(i).Text)
            	Next
End If

C#


if (e.Item.ItemType== ListItemType.Header)
{
	for(int i = 0; i <= e.Item.Cells.Count-1  ; i++)
	{
	this.ListBox1.Items.Add(new ListItem(e.Item.Cells[i].Text));
	}
} 

Share with

Related FAQs

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

Please submit your question and answer.