How to display the total of a particular column at the footer of the DataGrid
<asp:DataGrid id=’DataGrid1′ OnItemDataBound=’ItemDB’ ShowFooter =true runat=’server’></asp:DataGrid> VB.NET Dim UnitPrice As Double Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load ’Put user code to initialize the page here ’Bind the Data to datagrid End Sub Protected Sub UPTotal(ByVal _unitprice As Double) UnitPrice += _unitprice End Sub ’UPTotal Public Sub ItemDB(ByVal sender As Object, ByVal e As DataGridItemEventArgs) If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem Then UPTotal(Double.Parse(e.Item.Cells(1).Text.ToString)) Else If e.Item.ItemType = ListItemType.Footer Then e.Item.Cells(0).Text = ‘ Total ‘ e.Item.Cells(1).Text = UnitPrice.ToString() End If End If End Sub ’ItemDB C# double UnitPrice; private void Page_Load(object sender, System.EventArgs e) { // Put user code to initialize the page here if(!Page.IsPostBack ) { //Bind the DataGrid } } protected void UPTotal(double _unitprice) { UnitPrice += _unitprice; } public void ItemDB(object sender, DataGridItemEventArgs e) { if ((e.Item.ItemType == ListItemType.Item) ||( e.Item.ItemType == ListItemType.AlternatingItem)) { UPTotal(Double.Parse ( e.Item.Cells[1].Text )); } else if (e.Item.ItemType ==ListItemType.Footer ) { e.Item.Cells [0].Text =’ Total ‘; e.Item.Cells[1].Text =UnitPrice.ToString (); } }
How to access the TemplateColumn information on SelectedIndexChanged event of a 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; }
How to access information from the controls in the TemplateColumn in SelectedIndexChanged Event
<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> <asp:Label text=<%#DataBinder.Eval(Container.DataItem , ‘Productid’)%> ID=’lbl1′ Runat=server ></asp:Label> </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.FindControl(‘lbl1’), Label).Text End Sub C# protected void SelectedIndexChg(object sender, System.EventArgs e) { Label1.Text = ((Label)DataGrid1.SelectedItem.FindControl (‘lbl1’)).Text ; }
How to add a Column dynamically to the datagrid to include custom expressions
<asp:DataGrid id=’DataGrid1′ runat=’server’></asp:DataGrid> VB.NET ’Fill the DataSet ds with data from database Dim dc As DataColumn dc = New DataColumn(‘Total’, Type.GetType(‘System.Double’)) dc.Expression = ‘UnitPrice * UnitsInStock’ ds.Tables(0).Columns.Add(dc) DataGrid1.DataSource = ds DataGrid1.DataBind() C# //Fill the DataSet ds with data from database DataColumn dc ; dc = new DataColumn(‘Total’, Type.GetType(‘System.Double’)); dc.Expression = ‘UnitPrice * UnitsInStock’; ds.Tables[0].Columns.Add(dc); DataGrid1.DataSource = ds; DataGrid1.DataBind();
How to fill a DataGrid with an array
<asp:DataGrid id=’DataGrid1′ runat=’server’></asp:DataGrid> VB.NET Dim strArray As String() = {‘Tom’, ‘Jerry’, ‘Harry’, ‘Mickey’} DataGrid1.DataSource = strArray DataGrid1.DataBind() C# string[] strArray = {‘Tom’,’Jerry’, ‘Harry’, ‘Mickey’}; DataGrid1.DataSource = strArray; DataGrid1.DataBind();