How to access the TemplateColumn information on SelectedIndexChanged event of a datagrid

Platform: ASP.NET| Category: DataGrid

<asp:Label id='Label1' runat='server'>Label</asp:Label>
<asp:DataGrid id='DataGrid1' OnSelectedIndexChanged='SelectedIndexChg' AutoGenerateColumns='False' runat='server'>
<Columns>
	<asp:ButtonColumn Text='Select' ButtonType='PushButton' CommandName='Select'></asp:ButtonColumn>
	<asp:TemplateColumn HeaderText='ProductId'>
		<ItemTemplate>
			<%#DataBinder.Eval(Container.DataItem , 'Productid')%>
		</ItemTemplate>
	</asp:TemplateColumn>
	<asp:TemplateColumn HeaderText='ProductName'>
		<ItemTemplate>
			<%#DataBinder.Eval(Container.DataItem , 'ProductName')%>
		</ItemTemplate>
	</asp:TemplateColumn>
</Columns>
</asp:DataGrid>

VB.NET


Protected Sub SelectedIndexChg(ByVal sender As Object, ByVal e As System.EventArgs)
        Label1.Text = CType(DataGrid1.SelectedItem.Cells(1).Controls(0), DataBoundLiteralControl).Text & CType(DataGrid1.SelectedItem.Cells(2).Controls(0), DataBoundLiteralControl).Text
    End Sub

C#


protected void SelectedIndexChg(object sender, System.EventArgs e)
{
        Label1.Text =  ((DataBoundLiteralControl)DataGrid1.SelectedItem.Cells[1].Controls[0]).Text + ((DataBoundLiteralControl)DataGrid1.SelectedItem.Cells[2].Controls[0]).Text;
}

Share with

Related FAQs

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

Please submit your question and answer.