Here I will show how to do RowDataBound event in grid view in asp.net.For this I have given a simple example which is bind the percentage data to label in grid view.
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" Width="100%" OnRowDataBound="GridView1_RowDataBound"> <Columns> <asp:TemplateField HeaderText="Student Id"> <ItemTemplate> <asp:Label ID="lblstudentid" runat="server" Text='<%#Eval("StudentID") %>'></asp:Label> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Student Name"> <ItemTemplate> <asp:Label ID="lblstudentname" runat="server" Text='<%#Eval("Name") %>'></asp:Label> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Total"> <ItemTemplate> <asp:Label ID="lblTotal" runat="server" Text='<%#Eval("total")%>'></asp:Label> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Percentage"> <ItemTemplate> <asp:Label ID="lblPercentage" runat="server"></asp:Label> </ItemTemplate> </asp:TemplateField> </Columns> </asp:GridView>
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { Label lblPercentage = (Label)e.Row.FindControl("lblPercentage"); Label lblTotal = (Label)e.Row.FindControl("lblTotal"); if (total > 0) { float percentage = float.Parse(lblTotal.Text) / total; percentage = percentage * 100; lblPercentage.Text = percentage.ToString(); } else { lblPercentage.Text = "0"; } } }
No comments:
Post a Comment