Total Footer in Grid View not working - html

when am trying to display total in footer it's not display .The grid view appear but without the footer I don't know what I miss but I know that there is no errors but the footer not display
my html :
<asp:TemplateField HeaderText="debit">
<ItemTemplate>
<asp:Label ID="lblDebAmount" runat="server" Text='<%# (Eval("v_flag").ToString() =="d" ) ? string.Format("{0:0.000}",float.Parse(Eval("v_amount").ToString())): "0.000" %>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:Label ID="totallblDebAmount" runat="server" />
</FooterTemplate>
</asp:TemplateField>
</Columns>
<FooterStyle BackColor="#336699" Font-
Bold="True" ForeColor="White" HorizontalAlign="Left" />
<HeaderStyle BackColor="#336699" Font-Bold="True" ForeColor="White"
HorizontalAlign="Left" />
my code :
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
decimal totalPrice = 0;
if (e.Row.RowType == DataControlRowType.DataRow)
{
Label lblDebAmount = (Label)e.Row.FindControl("lblDebAmount");
decimal debtotal = Decimal.Parse(lblDebAmount.Text);
totalPrice += debtotal;
}
if (e.Row.RowType == DataControlRowType.Footer)
{
Label totallblCAmount = (Label)e.Row.FindControl("totallblCAmount");
totallblCAmount.Text = totalPrice.ToString();
}
}

try to modify
Label totallblCAmount = (Label)e.Row.FindControl("totallblCAmount");
to be
Label totallblCAmount = (Label)e.Row.FindControl("totallblDebAmount");

Related

asp.net:How to place columns from DB inbetween Template Fields of GridView

Hello i want gridview which should be like
|Sl No|Std No|Student Name|Total Amount|Paid Amount|Unpaid Amount|Action|
Here I am setting Sl No ,Unpaid Amount , and Action Columns by Template Field.And Other i am directly getting from mysql DB and binding to gridview. But the columns from DB are apending to end of those 3 TemplateFields.And i cant use BoundFields for those 4 Fields from DB.
MY CODE IS LIKE THIS
<div class="GridviewDiv">
<asp:GridView runat="server" ID="gvDetails" AllowPaging="false" AutoGenerateColumns="true" Width="100%" OnRowDataBound="GridView1_RowDataBound1">
<HeaderStyle CssClass="headerstyle" />
<Columns>
<asp:TemplateField HeaderText="SlNo">
<ItemTemplate>
<%#Container.DataItemIndex+1 %>
<asp:Label ID="serialID" runat="server" Visible="false"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<%-- HERE I WANT THOSE COLUMNS FROM DATABASE--%>
<asp:TemplateField HeaderText="Unpaid Amount">
<ItemTemplate>
<asp:Label ID="UnpaidText" runat="server"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Take Fee">
<ItemTemplate>
<asp:Button ID="TakeFeeCash" runat="server" BackColor="#2C6AA0" BorderColor="#2C6AA0" align="right"
BorderStyle="Ridge" Font-Bold="True" ForeColor="White" OnClick="FeeCashButton_click"
Text="Take Fee" CommandName="Select" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
What i am getting is like this
IMAGE:
How to set my columns like above format?
Please Help.

Passing data to function

I want to pass the variable Date to my function HasBtnRights(). Any suggestions on how to do that?
<asp:TemplateField HeaderText="More info" Visible='<%#HasBtnRights(Eval("Date")) %>'>
<ItemTemplate>
<asp:Button runat="server" OnClientClick='openModalPopup(<%# Eval("Agreement")%>);' Text="Open"/>
</ItemTemplate>
</asp:TemplateField>
If you want to hide / show the button based on certain criteria, you can set the Visible property of the Button itself.
<asp:TemplateField HeaderText="More info" >
<ItemTemplate>
<asp:Button runat="server" Visible='<%#HasBtnRights(Eval("Date")) %>' OnClientClick='openModalPopup(<%# Eval("Agreement")%>);' Text="Open" />
</ItemTemplate>
</asp:TemplateField>
An then, in your code behind:
public bool HasBtnRights(DateTime myDate) {
// your date logic here
return true; // or return false;
}

Can't add TemplateField to Datagrid

I am trying to add a DropDownList into the DataGrid so the location column can be easily changed. I am getting an error with TemplateField, itemtemplate, label and DropDownList. I get the build error:
is not allowed within a
'System.Web.UI.WebControls.DataGridColumnCollection'.
<asp:DataGrid runat="server" CssClass="tblResults" OnItemDataBound="dgList_ItemCreated" AllowSorting="true" OnSortCommand="dgTrailer_Sort" ID="dgTrailers" DataKeyField="ID" AutoGenerateColumns="false">
<Columns>
<asp:BoundColumn DataField="TrailerOwner" HeaderText="Owner" SortExpression="TrailerOwner"></asp:BoundColumn>
<asp:BoundColumn DataField="TrailerMake" HeaderText="Trailer Make" SortExpression="TrailerMake"></asp:BoundColumn>
<asp:TemplateField HeaderText="Trailer Location">
<itemtemplate>
<asp:Label ID="lblLocation" runat="server" Text='<%# Eval("Location") %>' Visible = "false" />
<asp:DropDownList ID="ddlLocation" runat="server">
</asp:DropDownList>
</itemtemplate>
</asp:TemplateField>
<asp:BoundColumn DataField="Year" HeaderText="Year" SortExpression="Year"></asp:BoundColumn>
</Columns>
</asp:DataGrid>
TemplateField, itemtemplate, label and DropDownList all have green lines under them and which means its not a known element.
You should use TemplateColumn, when it comes to DataGrid as it is inherited from System.Web.UI.WebControls.DataGridColumn.
TemplateField is inherited from System.Web.UI.WebControls.DataControlField, which make sense with GridView.

asp hyperlinkfield confirmation message

I have to add confirmation message on hyperlink like "Are you sure you what to that?" But I'm seeing asp first time in my life and spend last three hours on google but cant find good example to use. Any ideas?
This is how it looks right now:
<asp:HyperLinkField DataNavigateUrlFields="data"
DataNavigateUrlFormatString="SMSReport.aspx?data={0}&db=1"
HeaderText="Ataskaita" Text="Ataskaita" />
The easiest way is to make button, styled like link and redirect with javascript.
<asp:Button runat="server" id="btnRedirect" BorderWidth="0" BorderStyle="None" BackColor="White" Text="link" OnClientClick="return Redirect();"/>
function Redirect() {
return confirm('Are u sure?');
}
If you want to work in grid column you can do something like
<<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:Button runat="server" id="btnRedirect" BorderWidth="0" BorderStyle="None" BackColor="White" Text="link" OnClientClick="return Redirect();" OnClick="btnRedirect_Click" CommandArgument='<%# Eval("YourUrl")%>' />
</ItemTemplate>
<ItemStyle Width="20px" HorizontalAlign="Center" />
</asp:TemplateField>
</Columns>
And Code behind "*.aspx.vb" file protected void btnRedirect_Click(object sender, EventArgs e)
{
Response.Redirect((sender as Button).CommandArgument);
}

GridView Column modification

I have bound my gridView with code behind which Contains 4 column but I want first column to be a linkbutton.
So I have added itemtemplate but now my grid contain 5 columns with two same columns, but I want only one column which is of linkbutton column.
This is my itemtemplate code
<Columns>
<asp:TemplateField HeaderText="File No">
<ItemTemplate >
<asp:LinkButton ID="LinkButton1" runat="server"
CommandArgument='<%# Eval("File") %>' Text='<%# Eval("File") %>'
OnCommand="show" ></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
Code behind grid binding :
Bussiness_logic.GridView_Bind(GridView1, "GET_DATA");
public static void GridView_Bind(GridView Grid_Name,String Procdure_name)
{
SqlDataAdapter dap = new SqlDataAdapter(Procdure_name, GetConnection());
DataSet ds = new DataSet();
dap.Fill(ds);
Grid_Name.DataSource = ds;
Grid_Name.DataBind();
}
Your Getting five columns because you Set GridView AutoGenerateColumn to trueAND add an extra template column in grid view.So GridView generate columns for all columns that are present in your data set and add you extra column i.e your template column.
Now to solve this issue Set AutoGenerateColoumn Property of GridView To false. Define Your Column Like below..
<Columns>
<asp:TemplateField HeaderText="File No">
<ItemTemplate >
<asp:LinkButton ID="LinkButton1" runat="server" CommandArgument='<%# Eval("File") %>' Text='<%# Eval("File") %>' OnCommand="show" ></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="NameofSecondColumnInYourDateSet" HeaderText="Column2"/>
<asp:BoundField DataField="NameofThirdColumnInYourDateSet" HeaderText="Column3"/>
<asp:BoundField DataField="NameofFourthColumnInYourDateSet" HeaderText="Column4"/>
</Columns>