I am building a gridview from an arraylist and want to include a footer to the bottom.
This is my c# code
gvOrder.DataSource = orderItemList;
gvOrder.DataBind();
gvOrder.ShowFooter = true;
gvOrder.Columns[0].FooterText = "Totals:";
gvOrder.Columns[2].FooterText = Convert.ToString(quantity);
gvOrder.Columns[4].FooterText = Convert.ToString(priceTotal);
Here is my asp code
<asp:GridView ID="gvOrder" runat="server" AutoGenerateColumns="False" ShowFooter="True">
<Columns>
<asp:BoundField DataField="ItemTitle" HeaderText="Title" />
<asp:BoundField DataField="ItemFormat" HeaderText="Format" />
<asp:BoundField DataField="ItemQuantity" HeaderText="Quantity" />
<asp:BoundField DataField="ItemPrice" HeaderText="Price" />
<asp:BoundField DataField="ItemTotal" HeaderText="Total" />
</Columns>
</asp:GridView>
The order of things matter. You have to set the footer values before DataBind() is called.
gvOrder.Columns[0].FooterText = "Totals:";
gvOrder.Columns[2].FooterText = Convert.ToString(quantity);
gvOrder.Columns[4].FooterText = Convert.ToString(priceTotal);
gvOrder.DataSource = orderItemList;
gvOrder.DataBind();
gvOrder.ShowFooter = true;
But you can set the values of the Footer Row after DataBind if you specify the footerrow cells instead of the Colums.
gvOrder.FooterRow.Cells[1].Text = "After DataBind";
Related
I have an asp:linkbutton. When it's pressed I want to get a list of DetailID's based on which of the checkboxes in my GridView are checked. I have a lblTesting to see the list of DetailID's that I'm generating. My goal is to get a list of DetailID's that I pass to an edit page.
I have looked at several examples on Stack Overflow but I cannot get any value other than the checkbox is checked.
Here is the link I have at the top of my page:
<span style="float:right;"><asp:Linkbutton ID="getURLLink" runat="server" Text="Edit Die Detail" OnClick="GetLink"></asp:Linkbutton></span>
<asp:Label runat="server" ID="lblTesting"></asp:Label>
Here is my GridView:
<asp:GridView ID="DieListDetailGridView" runat="server" AutoGenerateColumns="false" OnRowDataBound="DieListDetailGridView_RowDataBound" DataKeyNames="DieID" HeaderStyle-HorizontalAlign="Center" HorizontalAlign="Center" ForeColor="#333333" GridLines="Both">
<RowStyle BackColor="#F5F5DC" />
<AlternatingRowStyle BackColor = "White" ForeColor="#284775" />
<Columns>
<asp:TemplateField HeaderText="Edit" HeaderStyle-HorizontalAlign="Center">
<ItemTemplate>
<asp:CheckBox ID="chkBox" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="DieNum" HeaderText="Die Number" />
<asp:BoundField DataField="DieID" HeaderText="Die ID" Visible="false" />
<asp:BoundField DataField="DetailID" HeaderText="Detail ID" Visible="false" />
<asp:HyperLinkField DataTextField="DetailNum" HeaderText="Detail Number" DataNavigateUrlFields="DetailID" DataNavigateUrlFormatString="~/Tooling/DieDetail.aspx?DetailID={0}" />
<asp:BoundField DataField="Description" HeaderText="Description" />
<asp:BoundField DataField="MaterialType" HeaderText="Material Type" />
<asp:BoundField DataField="Hardness" HeaderText="Hardness" />
<asp:BoundField DataField="MinSpares" HeaderText="Min Spares" />
<asp:BoundField DataField="MaxSpares" HeaderText="Max Spares" />
<asp:BoundField DataField="CurrentSpares" HeaderText="Current Spares" />
<asp:BoundField DataField="DetailNotes" HeaderText="Detail Notes" />
</Columns>
</asp:GridView>
And my GetLink function:
protected void GetLink(object sender, EventArgs e)
{
// start off empty
string DetailIDList = "";
foreach (GridViewRow row in DieListDetailGridView.Rows) {
if (row.RowType == DataControlRowType.DataRow)
{
CheckBox chkBox = (row.Cells[0].FindControl("chkBox") as CheckBox);
string DetailID = row.Cells[3].Text;
if (chkBox.Checked)
{
DetailIDList = DetailIDList + DetailID + ",";
}
}
}
// Remove the last , from the list
DetailIDList = DetailIDList.Remove(DetailIDList.Length - 1, 1);
// comma delimeted list of checked Detail ID's
lblTesting.Text = DetailIDList;
//Response.Redirect("~/Tooling/DieDetail.aspx?DetailIDList=");
}
I can't figure out why my DetailID is empty.
I've tried doing row.Cells[0].FindControl("DetailID") but that also didn't work.
What I am expecting to get from the DetailID are integers like "28925", "16423" etc.
I was asked to not show the column on the page, which is why visible = false. I still need to reference the ID to pass it to the edit page. I need to do the same edit to multiple Detail ID's.
What am I doing wrong?
The answer to having a list of ID's even though the datafield is set to not be visible was found on another StackOverflow question: ASP.NET: GridView value set to null when column is not visible
The answer was to remove the "Visible=false" from the GridView and on each row created (after the column has the data added for the row) make it not visible.
This code fixed my issue:
protected void DieListDetailGridView_RowCreated(object sender, GridViewRowEventArgs e)
{
e.Row.Cells[2].Visible = false;
e.Row.Cells[3].Visible = false;
}
I'm using the below code to add a new row to a ASP.net gridview. The problem is that every new row I create is blank. I looped through the cells in the row and found that most of them were spaces ( ). I've looked everywhere for a different piece of code to use, but everything leads me to: create a new datatable, add a row to it, set the gridview datasource to the datatable. I've used this control a dozen times before and never got this before, it's very odd.
Class Level Variable
public partial class DMREntry : System.Web.UI.Page
{
private DataTable _Parts
{
get { return (DataTable)ViewState["Parts"]; }
set { ViewState.Add("Parts", value); }
}
...
The add row code. Note that my textboxes and my gridview are within an update panel.
if (_Parts == null)
{
_Parts = new DataTable();
_Parts.Columns.Add("Part No");
_Parts.Columns.Add("Qty");
_Parts.Columns.Add("Description");
_Parts.Columns.Add("Vendor");
_Parts.Columns.Add("Vendor Part");
_Parts.Columns.Add("Cost");
_Parts.Columns.Add("PO Number");
_Parts.Columns.Add("Delivery Date");
_Parts.Columns.Add("Total Cost");
}
DataRow dr = _Parts.NewRow();
dr["Part No"] = txtPartNo.Text;
dr["Qty"] = txtQty.Text;
dr["Description"] = txtDescription.Text;
dr["Vendor"] = txtVendor.Text;
dr["Vendor Part"] = txtVendorPart.Text;
dr["Cost"] = txtCost.Text;
dr["PO Number"] = txtPONumber.Text;
dr["Delivery Date"] = txtDeliveryDate.Text;
dr["Total Cost"] = txtTotalCost.Text;
// At this point, DR has the correct values
_Parts.Rows.Add(dr);
_Parts.AcceptChanges();
gvParts.DataSource = _Parts;
gvParts.DataBind();
upGrid.Update();
GridView markup:
`<asp:GridView ID="gvParts" runat="server" AutoGenerateColumns="False" PageSize="5"
Width="787px" BackColor="White" BorderColor="#CCCCCC" BorderStyle="None"
BorderWidth="1px" CellPadding="3"
onselectedindexchanged="gvParts_SelectedIndexChanged1" AllowPaging="True"
AllowSorting="True">
<Columns>
<asp:TemplateField HeaderText="Part No"></asp:TemplateField>
<asp:TemplateField HeaderText="Qty"></asp:TemplateField>
<asp:TemplateField HeaderText="Description"></asp:TemplateField>
<asp:TemplateField HeaderText="Vendor"></asp:TemplateField>
<asp:TemplateField HeaderText="Vendor Part"></asp:TemplateField>
<asp:TemplateField HeaderText="Cost"></asp:TemplateField>
<asp:TemplateField HeaderText="PO Number"></asp:TemplateField>
<asp:TemplateField HeaderText="Delivery Date"></asp:TemplateField>
<asp:TemplateField HeaderText="Total Cost"></asp:TemplateField>
<asp:CommandField ButtonType="Button" ShowEditButton="True" />
<asp:CommandField ButtonType="Button" ShowDeleteButton="True" />
</Columns>
<FooterStyle BackColor="White" ForeColor="#000066" />
<HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="White" ForeColor="#000066" HorizontalAlign="Left" />
<RowStyle ForeColor="#000066" />
<SelectedRowStyle BackColor="#669999" Font-Bold="True" ForeColor="White" />
<SortedAscendingCellStyle BackColor="#F1F1F1" />
<SortedAscendingHeaderStyle BackColor="#007DBB" />
<SortedDescendingCellStyle BackColor="#CAC9C9" />
<SortedDescendingHeaderStyle BackColor="#00547E" />
</asp:GridView>`
Leaves me with:
My grid properties are the default, except:
AutoGenerateColumns = false
AllowPaging = true
AllowSorting = true
PageSize = 5
If I turn AutoGenerateColumns on, the data from the table does go into the auto-generated columns. But not for when I create the columns manually. I am using the template field column type for the manually added columns. They're just text fields, I couldn't find a better option.
Any help appreciated...
Why you are using asp:TemplateField ? You did not add any control in the templatefield.
Just change TemplateField to BoundField then it will solve your problem.
Like:
<asp:BoundField DataField="Part No" HeaderText="Part No" />
<asp:BoundField DataField="Qty" HeaderText="Qty" />
<asp:BoundField DataField="Description" HeaderText="Description" />
<asp:BoundField DataField="Vendor" HeaderText="Vendor" />
<asp:BoundField DataField="Vendor Part" HeaderText="Vendor Part" />
<asp:BoundField DataField="Cost" HeaderText="Cost" />
<asp:BoundField DataField="PO Number" HeaderText="PO Number" />
<asp:BoundField DataField="Delivery Date" HeaderText="Delivery Date" />
<asp:BoundField DataField="Total Cost" HeaderText="Total Cost" />
Try creating inside the an tag
<asp:TemplateField HeaderText="Example">
<ItemTemplate>
<%# Eval("TestColumn") %>
</ItemTemplate>
</asp:TemplateField>
I have a GridView like this:
<asp:GridView ID="GridViewAllPeopleEditMode" runat="server"
AutoGenerateColumns="false"
AutoGenerateEditButton="true"
AllowPaging="true"
PageSize="20"
OnRowEditing="GridViewAllPeopleEditMode_RowEditing"
OnRowCancelingEdit="GridViewAllPeopleEditMode_RowCancelingEdit"
OnRowUpdating="GridViewAllPeopleEditMode_RowUpdating"
OnPageIndexChanging="GridViewAllPeopleEditMode_PageIndexChanging">
<Columns>
<asp:BoundField DataField="id" HeaderText="BusinessEntityID" ReadOnly="true"/>
<asp:BoundField DataField="FirstName" HeaderText="FirstName"/>
<asp:BoundField DataField="MiddleName" HeaderText="MiddleName"/>
<asp:BoundField DataField="LastName" HeaderText="LastName"/>
</Columns>
</asp:GridView>
I want the page_load to set the DataField attributes before setting the DataSource()/DataBind(), instead of writing them by myself in the .aspx page.
Is it possible to do that, or do I have to change the BoundFields?
Thanks a lot!
You can use like this in code side
C#
BoundField field = (BoundField)this.GridViewAllPeopleEditMode.Columns[0];
field.DataField = "To";
VB
Dim field As BoundField = DirectCast(Me.GridViewAllPeopleEditMode.Columns(0), BoundField)
field.DataField = "To"
I have a gridview :
<asp:GridView ID="gvReportingListeOF" runat="server" AutoGenerateColumns="false" Visible="false">
<Columns>
<asp:BoundField DataField="cod_wo" HeaderText="N° OF" />
<asp:BoundField DataField="cod_ref" HeaderText="Référence article" />
<asp:BoundField DataField="lbl_article" HeaderText="Désignation article" />
<asp:BoundField DataField="dat_sequence_wo" HeaderText="Séquence" />
<asp:BoundField DataField="wo_qty" HeaderText="Qté prévue" />
<asp:BoundField DataField="qty_revue" HeaderText="Qte revue" />
</Columns>
</asp:GridView>
And I wish changing dynamically the header text with code behind C#. It is possible ? Because they doesn't have ID...
Thanks
Try the above
gvReportingListeOF.Columns[ColumnIndex].HeaderText = "Header text"
You need to write the code in the RowDataBound Event like this.
if (e.Row.RowType == DataControlRowType.Header)
{
e.Row.Cells[0].Text = "First Column";
e.Row.Cells[1].Text = "Second Column";
}
I'm trying to make a grid view with asp.net. I want that columns' headers have a colspan like that :
But for the moment I have :
My code is :
<asp:GridView ID="GV" runat="server" AutoGenerateColumns="false" OnPreRender="gridView_PreRender">
<Columns>
<asp:BoundField HeaderText="" />
<asp:BoundField HeaderText="Num OF" DataField="cod_wo" />
<asp:BoundField HeaderText="Num Seq" DataField="dat_sequence" />
<asp:BoundField HeaderText="Article" DataField="article"/>
<asp:BoundField HeaderText="Qté OF Prévue" DataField="wo_qty" />
<asp:BoundField HeaderText="Qté OF Revue" DataField="qty_revue" />
<asp:BoundField HeaderText="Composant" DataField="composant" />
<asp:BoundField HeaderText="Restant à assigner" DataField="restant_a_assigner" />
<asp:BoundField HeaderText="Assignation HU" />
<asp:BoundField HeaderText="Qté totale" DataField="qte_totale_assignee" />
<asp:BoundField HeaderText="Num HU" DataField="num_hu" />
<asp:BoundField HeaderText="Qté" DataField="qte_assignee" />
</Columns>
</asp:GridView>
and in code behind C# i have :
public static void MergeColumns(GridView gridView)
{
gridView.HeaderRow.Cells[0].RowSpan = 2;
gridView.HeaderRow.Cells[1].RowSpan = 2;
gridView.HeaderRow.Cells[2].RowSpan = 2;
gridView.HeaderRow.Cells[3].RowSpan = 2;
gridView.HeaderRow.Cells[4].RowSpan = 2;
gridView.HeaderRow.Cells[5].RowSpan = 2;
gridView.HeaderRow.Cells[6].RowSpan = 2;
gridView.HeaderRow.Cells[7].RowSpan = 2;
gridView.HeaderRow.Cells[8].ColumnSpan = 3;
}
Do you have an idea ? thanks
You can do by using RowCreated event of grid view.For better understanding follow This Link
Hope it solves your problem.