In this article, I am going to show how we can add a column value in a DataGrid. To add a column value in this program I am using a function. In the function, I am passing the value of that particular column. Let us see how to do this through the below code.
The default.aspx code is:
- <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head runat="server">
- <title>Count a column Value</title>
- </head>
- <body>
- <form id="form1" runat="server">
- <div>
- <asp:DataGrid id="MyGrid" runat="server" AutoGenerateColumns="False" CellPadding="2" CellSpacing="0" BorderStyle="Solid" BorderWidth="4" Gridlines="Both" BorderColor="ActiveCaption" ItemStyle-Font-Name="Verdana" ItemStyle-Font-Size="10pt" HeaderStyle-Font-Name="Verdana" HeaderStyle-Font-Size="14pt" HeaderStyle-Font-Bold="True" HeaderStyle-BackColor="#ccffcc" FooterStyle-Font-Name="Verdana" FooterStyle-Font-Size="10pt" FooterStyle-Font-Bold="True" FooterStyle-ForeColor="White" FooterStyle-BackColor="Blue" OnItemDataBound="MyDataGrid_ItemDataBound" ShowFooter="True">
- <Columns>
- <asp:BoundColumn HeaderText="Product ID" DataField="ProductID" />
- <asp:BoundColumn HeaderText="Product Name" DataField="ProductName" />
- <asp:BoundColumn HeaderText="Unit In Price" DataField="UnitPrice" ItemStyle-HorizontalAlign="center"> </asp:BoundColumn>
- <asp:BoundColumn HeaderText="Units In Stock" DataField="UnitsInStock" ItemStyle-HorizontalAlign="center" HeaderStyle-HorizontalAlign="Center">
- </asp:BoundColumn>
- </Columns>
- </asp:DataGrid>
- </div>
- </form>
- </body>
- </html>


Join the conversation! Your thoughts help the community grow.