I want to show sum of values on header text in grid view.
Code:
.aspx
<asp:GridView ID="gvTrHty" runat="server" AutoGenerateColumns="false" Width="100%"
Style="border: 0px solid #cdcdcd; background-color: #cdcdcd;" border="0" CellSpacing="1"
CellPadding="3" OnSorting="gvTrHty_Sorting" AllowSorting="true">
<Columns>
<asp:TemplateField HeaderText="S.No" ItemStyle-VerticalAlign="Middle" ItemStyle-HorizontalAlign="Center">
<ItemTemplate>
<%#Container.DataItemIndex+1 %>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField HeaderText="Employee" DataField="UserFullName" SortExpression="UserFullName" />
<asp:BoundField HeaderText="Course" DataField="CourseName" SortExpression="CourseName" />
<asp:BoundField HeaderText="Duration(Hrs)" DataField="Duration" SortExpression="Duration" />
<asp:BoundField HeaderText="Start Date" DataField="StartDate" DataFormatString="{0:dd-MMM-yyyy}"
SortExpression="StartDate" ItemStyle-HorizontalAlign="Center" ItemStyle-VerticalAlign="Middle" />
<asp:BoundField HeaderText="End Date" DataField="EndDate" DataFormatString="{0:dd-MMM-yyyy}"
SortExpression="EndDate" ItemStyle-HorizontalAlign="Center" ItemStyle-VerticalAlign="Middle" />
<asp:BoundField HeaderText="Result" DataField="RESULT" ItemStyle-HorizontalAlign="Center"
SortExpression="RESULT" ItemStyle-VerticalAlign="Middle" />
<asp:BoundField HeaderText="Year" DataField="Year" ItemStyle-HorizontalAlign="Center"
SortExpression="Year" ItemStyle-VerticalAlign="Middle" />
<asp:BoundField HeaderText="Category" DataField="Category" ItemStyle-HorizontalAlign="Center"
SortExpression="Category" ItemStyle-VerticalAlign="Middle" />
<asp:BoundField HeaderText="Sub Category" DataField="SubCategory" SortExpression="SubCategory" />
</Columns>
<HeaderStyle CssClass="header-link grid-Header DataGridFixedHeader gvHead " />
<RowStyle CssClass="gd_odd_txt gvRow" />
</asp:GridView>
In the below image format I want to get sum of Duration
Any ideas? Thanks in advance
Related
I have a weird issue with an ASP.NET GridView control. It won't display vertical lines, no matter what I do. I have set the following:
<asp:GridView runat="server" ID="gvActivity" AllowPaging="true" PageSize="10" AutoGenerateColumns="false" GridLines="Both" BorderColor="#000000" BorderStyle="Solid"
EmptyDataText="No Data Found" Width="100%">
<EmptyDataRowStyle ForeColor="#990000" HorizontalAlign="Center" Font-Bold="true" Font-Size="X-Large" />
<RowStyle BackColor="#ffffff" ForeColor="#000000" BorderColor="#000000" font-names="corbel, verdana, arial" Font-Size="14px" />
<AlternatingRowStyle BackColor="#ffffcc" ForeColor="#000000" BorderColor="#000000" font-names="corbel, verdana, arial" Font-Size="14px" />
<HeaderStyle BackColor="#006699" ForeColor="#ffffff" HorizontalAlign="Center" BorderColor="#000000" font-names="corbel, verdana, arial" Font-Size="14px" />
<Columns>
<asp:BoundField DataField="ActivityDate" HeaderText="Date" DataFormatString="{0:d}" ItemStyle-Width="20%" ItemStyle-HorizontalAlign="Right" />
<asp:BoundField DataField="NewCustomers" HeaderText="Customers" DataFormatString="{0:n0}" ItemStyle-Width="10%" ItemStyle-HorizontalAlign="Right" />
<asp:BoundField DataField="BidPackSales" HeaderText="Packs" DataFormatString="{0:n0}" ItemStyle-Width="10%" ItemStyle-HorizontalAlign="Right" />
<asp:BoundField DataField="BidPackTotal" HeaderText="Pack Ttl" DataFormatString="{0:c}" ItemStyle-Width="10%" ItemStyle-HorizontalAlign="Right" />
<asp:BoundField DataField="BPCommissions" HeaderText="Pack Comm." DataFormatString="{0:c}" ItemStyle-Width="10%" ItemStyle-HorizontalAlign="Right" />
<asp:BoundField DataField="BoxSales" HeaderText="Boxes" DataFormatString="{0:n0}" ItemStyle-Width="10%" ItemStyle-HorizontalAlign="Right" />
<asp:BoundField DataField="MBCommissions" HeaderText="Box Comm." DataFormatString="{0:c}" ItemStyle-Width="10%" ItemStyle-HorizontalAlign="Right" />
<asp:BoundField DataField="TotalCommissions" HeaderText="Total Comm." DataFormatString="{0:c}" ItemStyle-Width="10%" ItemStyle-HorizontalAlign="Right" />
<asp:BoundField DataField="PaidOn" HeaderText="Paid" DataFormatString="{0:d}" ItemStyle-Width="10%" ItemStyle-HorizontalAlign="Right" />
</Columns>
</asp:GridView>
The horizontal lines between the rows show up, but not the vertical lines separating cells. Any help on this?
Hum, the lines show up for me. So, either you have some stray css on that page.
I would also consider clearing out your browser cache. And also, try changing the zoom.
I don't have your data, but if take your gv, and shove in my own columns, say like this:
<asp:GridView runat="server" ID="gvActivity" AllowPaging="true"
PageSize="10" AutoGenerateColumns="false" GridLines="Both" BorderColor="#000000"
BorderStyle="Solid"
EmptyDataText="No Data Found" Width="100%">
<EmptyDataRowStyle ForeColor="#990000" HorizontalAlign="Center" Font-Bold="true" Font-Size="X-Large" />
<RowStyle BackColor="#ffffff" ForeColor="#000000" BorderColor="#000000" Font-Names="corbel, verdana, arial" Font-Size="14px" />
<AlternatingRowStyle BackColor="#ffffcc" ForeColor="#000000" BorderColor="#000000" Font-Names="corbel, verdana, arial" Font-Size="14px" />
<HeaderStyle BackColor="#006699" ForeColor="#ffffff" HorizontalAlign="Center" BorderColor="#000000" Font-Names="corbel, verdana, arial" Font-Size="14px" />
<Columns>
<asp:BoundField DataField="Fighter" HeaderText="Fighter" />
<asp:BoundField DataField="Engine" HeaderText="Engine" />
<asp:BoundField DataField="Thrust" HeaderText="Thrust" />
<asp:BoundField DataField="Description" HeaderText="Description" />
<asp:TemplateField HeaderText="Preview">
<ItemTemplate>
<asp:Image ID="Image2" runat="server" Width="150px"
ImageUrl='<%# Eval("ImagePath")%>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
And then code to load is this:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
LoadGrid();
}
void LoadGrid()
{
using (SqlConnection conn = new SqlConnection(Properties.Settings.Default.TEST4))
{
string strSQL = "SELECT * FROM Fighters";
using (SqlCommand cmdSQL = new SqlCommand(strSQL,conn))
{
conn.Open();
DataTable rstData = new DataTable();
rstData.Load(cmdSQL.ExecuteReader());
gvActivity.DataSource = rstData;
gvActivity.DataBind();
}
}
}
I see/get this:
And consider using bootstrap class. You probably have bootstrap included in your project, and they tend without efforts to make your gv look VERY nice.
Lets dump all of your color stuff, and try this:
<asp:GridView runat="server" ID="gvActivity" AllowPaging="true"
PageSize="10" AutoGenerateColumns="false"
EmptyDataText="No Data Found" Width="100%"
CssClass="table table-hover table-striped">
<Columns>
<asp:BoundField DataField="Fighter" HeaderText="Fighter" />
<asp:BoundField DataField="Engine" HeaderText="Engine" />
<asp:BoundField DataField="Thrust" HeaderText="Thrust" />
<asp:BoundField DataField="Description" HeaderText="Description" />
<asp:TemplateField HeaderText="Preview">
<ItemTemplate>
<asp:Image ID="Image2" runat="server" Width="150px"
ImageUrl='<%# Eval("ImagePath")%>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
And we now get this:
So, we see/get alternate row shading, and the text and spacing is actually quite nice. (padding around the boxes is better). And no need for a bunch of messy hand built formatting.
But, try a different browser, try change the zoom, try clearing your browser cache.
And if need be, give the bootstrap classes a try
these:
CssClass="table table-hover table-striped"
eg:
I have my GridView here .. (Automatically generated as WebForm Template)
I want to create a searchbox wherein I can search all the PTAID ..
Here's my codes
<asp:GridView ID="GridView1" runat="server" AllowPaging="True" AllowSorting="True" AutoGenerateColumns="False" ShowFooter="True" OnRowCommand="grdSearch_RowCommand" CssClass="table table-bordered table-hovered">
<Columns>
<asp:TemplateField >
<FooterTemplate>
<asp:TextBox ID="text_search" runat="server" CssClass="form-control" />
<asp:Button ID="btnSearch" CommandName="Search" runat="server" Text="Search" />
</FooterTemplate>
</asp:TemplateField>
<asp:BoundField DataField="PTAID" HeaderText="PTAID" SortExpression="PTAID" />
<asp:BoundField DataField="RequestID" HeaderText="RequestID" SortExpression="RequestID" />
<asp:BoundField DataField="RequestDate" HeaderText="RequestDate" SortExpression="RequestDate" />
<asp:BoundField DataField="ProvName" HeaderText="ProvName" SortExpression="ProvName" />
<asp:BoundField DataField="Amount" HeaderText="Amount" SortExpression="Amount" />
<asp:BoundField DataField="INorOUT" HeaderText="INorOUT" SortExpression="INorOUT" />
<asp:BoundField DataField="Supplier" HeaderText="Supplier" SortExpression="Supplier" />
<asp:BoundField DataField="Customer" HeaderText="Customer" SortExpression="Customer" />
<asp:BoundField DataField="Program" HeaderText="Program" SortExpression="Program" />
<asp:BoundField DataField="IssueDesc" HeaderText="IssueDesc" SortExpression="IssueDesc" />
<asp:BoundField DataField="Chargeable" HeaderText="Chargeable" SortExpression="Chargeable" />
<asp:BoundField DataField="Company" HeaderText="Company" SortExpression="Company" />
</Columns>
</asp:GridView>
How will I make a searchbox ? I tried the one I searched but then i wanted to show the GridView data and the searchbox altogether.
Please try following code, it should work:
<div>
Enter UserName :
<asp:TextBox ID="txtSearch" runat="server" />
<asp:ImageButton ID="btnSearch" text="Search" runat="server"
Style="top: 5px; position: relative; width: 26px;" />
<asp:ImageButton ID="btnClear" text="Clear" runat="server" Style="top: 5px;
position: relative; width: 14px;" /><br />
<br />
</p>
<asp:GridView ID="gvDetails" runat="server" AutoGenerateColumns="False" AllowPaging="True"
AllowSorting="True" DataSourceID="dsDetails" Width="540px" CssClass="Gridview" >
<HeaderStyle BackColor="#df5015" />
<Columns>
<asp:BoundField DataField="PTAID" HeaderText="PTAID" SortExpression="PTAID" />
<asp:BoundField DataField="RequestID" HeaderText="RequestID" SortExpression="RequestID" />
<asp:BoundField DataField="RequestDate" HeaderText="RequestDate" SortExpression="RequestDate" />
<asp:BoundField DataField="ProvName" HeaderText="ProvName" SortExpression="ProvName" />
<asp:BoundField DataField="Amount" HeaderText="Amount" SortExpression="Amount" />
<asp:BoundField DataField="INorOUT" HeaderText="INorOUT" SortExpression="INorOUT" />
<asp:BoundField DataField="Supplier" HeaderText="Supplier" SortExpression="Supplier" />
<asp:BoundField DataField="Customer" HeaderText="Customer" SortExpression="Customer" />
<asp:BoundField DataField="Program" HeaderText="Program" SortExpression="Program" />
<asp:BoundField DataField="IssueDesc" HeaderText="IssueDesc" SortExpression="IssueDesc" />
<asp:BoundField DataField="Chargeable" HeaderText="Chargeable" SortExpression="Chargeable" />
<asp:BoundField DataField="Company" HeaderText="Company" SortExpression="Company" />
</Columns>
</asp:GridView>
</div>
<asp:SqlDataSource ID="dsDetails" runat="server" ConnectionString="<%$ ConnectionStrings:DemoConnectionString %>" SelectCommand="SELECT [name], [address] FROM [stud_info]" FilterExpression="Name LIKE '%{0}%'">
<FilterParameters>
<asp:ControlParameter Name="PTAID" ControlID="txtSearch" PropertyName="Text" />
</FilterParameters>
</asp:SqlDataSource>
I have a dropdown in which I have two options,
a. Selected
b. Not Selected.
I want whenever the user selects any option, the search should filter on that basis. I have a column of Selection in gridview which has both option Selected and Not Selected. The values are coming from the table.
Please see the HTML of dropdown and button for your reference:
<div style="float: left;">
<asp:DropDownList ID="ddlSort" runat="server" AutoPostBack="false" OnSelectedIndexChanged="ddlSort_SelectedIndexChanged">
<asp:ListItem Text="--Select--" Value="1"></asp:ListItem>
<asp:ListItem Text="Selected" Value="2"></asp:ListItem>
<asp:ListItem Text="Not Selected" Value="3"></asp:ListItem>
</asp:DropDownList>
<asp:Button ID="btnSortSelection" runat="server" Text="Sort Selection" OnClick="btnSortSelection_Click" />
</div>
I tried with the below mentioned code but the page refreshes and the dropdown inside the gridview loses its value. So it did not returns my search result.:-
protected void ddlSort_SelectedIndexChanged(object sender, EventArgs e) {
SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["DefaultSQLConnectionString"].ConnectionString); //create connection
SqlCommand cmd = new SqlCommand("Select * from Career.Applicant where Selection =#Selected", conn);
cmd.Parameters.AddWithValue("#selection", ddlSort.SelectedValue);
}
I want to stop the dropdownlist values to get postback and also it should work when sorted
Please see the code with Updated Panel:-
<asp:UpdatePanel runat="server" ID="updateapplicants">
<ContentTemplate>
<div style="border: 1px solid #A8A8A8; width: 920px;">
<asp:GridView ID="gv_Applicants" runat="server" AutoGenerateColumns="false" AllowPaging="true" Width="920"
PageSize="5" OnPageIndexChanging="gv_Applicants_PageIndexChanging" OnRowCommand="gv_Applicants_RowCommand"
EmptyDataText="No Applicants Found."
AllowSorting="true"
OnSorting="gv_Applicants_Sorting"
OnRowDataBound="gv_Applicants_RowDataBound" RowStyle-CssClass="a12" AlternatingRowStyle-CssClass="a22" ForeColor="#333333" GridLines="None" CssClass="table_box" HeaderStyle-Height="35px" DataKeyNames="JobId">
<AlternatingRowStyle BackColor="#F0F0F0" />
<Columns>
<asp:BoundField DataField="FirstName" HeaderText="First Name" HeaderStyle-Width="84" />
<asp:BoundField DataField="LastName" HeaderText="Last Name" HeaderStyle-Width="106" />
<asp:BoundField DataField="ContactNumber" HeaderText="Contact" HeaderStyle-Width="98" />
<asp:BoundField DataField="Email" HeaderText="Email" HeaderStyle-Width="150" />
<asp:TemplateField HeaderText="Position" SortExpression="Position" HeaderStyle-Width="107">
<ItemTemplate>
<%# Eval("Job.Position") %>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Location" SortExpression="Location" HeaderStyle-Width="100">
<ItemTemplate>
<%# Eval("Job.Location") %>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="AppliedDate" DataFormatString="{0:MMMM dd, yyyy}" HeaderText="Date of Application" ReadOnly="true" HeaderStyle-Width="121" />
<asp:TemplateField HeaderText="Action" HeaderStyle-HorizontalAlign="Center">
<ItemTemplate>
<asp:LinkButton ID='lnkView' CommandName='v' Text='View' runat='server' CommandArgument='<%# Eval("ApplicantId") %>'></asp:LinkButton>
|
<asp:LinkButton ID='lnkdel' CommandName='d' Text='Delete' runat='server' CommandArgument='<%# Eval("ApplicantId") %>' OnClientClick="return confirm('Are you sure to delete?');"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Selection">
<ItemTemplate>
<asp:DropDownList ID="ddlSelection" runat="server" EnableViewState="true" AutoPostBack="true">
<asp:ListItem Text="None" Value="1"></asp:ListItem>
<asp:ListItem Text="Selected" Value="2"></asp:ListItem>
<asp:ListItem Text="Not Selected" Value="3"></asp:ListItem>
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="JobId" HeaderText="Job ID" Visible="false" ReadOnly="true" />
</Columns>
<EditRowStyle BackColor="#2461BF" />
<FooterStyle BackColor="#D8DADA" Font-Bold="True" />
<HeaderStyle BackColor="#D8DADA" Font-Bold="True" />
<PagerStyle BackColor="#D8DADA" HorizontalAlign="Center" />
<RowStyle BackColor="white" BorderStyle="Solid" BorderColor="#a8a8a8" BorderWidth="1px" Height="35" />
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
</asp:GridView>
</div>
<br />
<br />
<div id="divDetails" runat="server" class="force-overflow">
<div class="scrollbar" id="style-1">
<div style="border-left: 1px solid #A8A8A8; border-right: 1px solid #A8A8A8;">
<asp:DetailsView ID="dv_Applicants" HeaderStyle-Font-Bold="true" HeaderStyle-BackColor="#F7E1E1" runat="server" AutoGenerateRows="false" AllowPaging="false"
HeaderText="Applicant Details" OnDataBound="dv_Applicants_DataBound" Width="912px" BackColor="#F0F0F0" BorderStyle="Solid" BorderWidth="1" BorderColor="#F0F0F0"
RowStyle-Height="30" Font-Size="10">
<Fields>
<asp:TemplateField HeaderText="Position" HeaderStyle-CssClass="c_width">
<ItemTemplate>
<%#Eval("Job.Position") %>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Location" HeaderStyle-CssClass="c_width">
<ItemTemplate>
<%#Eval("Job.Location") %>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Business Unit" HeaderStyle-CssClass="c_width">
<ItemTemplate>
<%#Eval("Job.BusinessUnit") %>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="FirstName" HeaderText="First Name" HeaderStyle-Width="190" HeaderStyle-Font-Bold="true" />
<asp:BoundField DataField="LastName" HeaderText="Last Name" HeaderStyle-Width="190" HeaderStyle-Font-Bold="true" />
<asp:BoundField DataField="ContactNumber" HeaderText="Contact" HeaderStyle-Width="190" HeaderStyle-Font-Bold="true" />
<asp:BoundField DataField="Email" HeaderText="Email" HeaderStyle-Width="190" HeaderStyle-Font-Bold="true" />
<asp:BoundField DataField="PAN" HeaderText="PAN" HeaderStyle-Width="190" HeaderStyle-Font-Bold="true" />
<asp:BoundField DataField="DOB" DataFormatString="{0:MMMM dd, yyyy}" HeaderText="Date of Birth" HeaderStyle-Width="190" HeaderStyle-Font-Bold="true" />
<asp:BoundField DataField="Graduation" HeaderText="Graduation" HeaderStyle-Width="190" HeaderStyle-Font-Bold="true" />
<asp:BoundField DataField="GradOther" HeaderText="Other" HeaderStyle-Width="190" HeaderStyle-Font-Bold="true" />
<asp:BoundField DataField="GradDate" DataFormatString="{0:MMMM dd, yyyy}" HeaderText="Graduation Date" HeaderStyle-Width="190" HeaderStyle-Font-Bold="true" />
<asp:BoundField DataField="PostGraduation" HeaderText="Post Graduation" HeaderStyle-Width="190" HeaderStyle-Font-Bold="true" />
<asp:BoundField DataField="PGOther" HeaderText="Other" HeaderStyle-Width="190" HeaderStyle-Font-Bold="true" />
<asp:BoundField DataField="PGDate" DataFormatString="{0:MMMM dd, yyyy}" HeaderText="Post Graduation date" HeaderStyle-Width="190" HeaderStyle-Font-Bold="true" />
<asp:BoundField DataField="AnyOtherQual" HeaderText="Any Other Qualification" HeaderStyle-Width="190" HeaderStyle-Font-Bold="true" />
<asp:BoundField DataField="CurrentOrg" HeaderText="Organization" HeaderStyle-Width="190" HeaderStyle-Font-Bold="true" />
<asp:BoundField DataField="CurrentDesignation" HeaderText="Designation" HeaderStyle-Width="190" HeaderStyle-Font-Bold="true" />
<asp:BoundField DataField="CurrentFunctionalDesig" HeaderText="Current Functional Designation" HeaderStyle-Width="190" HeaderStyle-Font-Bold="true" />
<asp:BoundField DataField="CurrentCTC" DataFormatString="{0} lakhs per annum" HeaderText="CTC" HeaderStyle-Width="190" HeaderStyle-Font-Bold="true" />
<asp:BoundField DataField="YrsinCurrentRole" HeaderText="Current Experience" HeaderStyle-Width="190" HeaderStyle-Font-Bold="true" />
<asp:BoundField DataField="TotalExp" HeaderText="Total Experience" HeaderStyle-Width="190" HeaderStyle-Font-Bold="true" />
<asp:BoundField DataField="KeySkills" HeaderText="Skills" HeaderStyle-Width="190" HeaderStyle-Font-Bold="true" />
<asp:BoundField DataField="City" HeaderText="City" HeaderStyle-Width="190" HeaderStyle-Font-Bold="true" />
<asp:BoundField DataField="State" HeaderText="State" HeaderStyle-Width="190" HeaderStyle-Font-Bold="true" />
<asp:BoundField DataField="Country" HeaderText="Country" HeaderStyle-Width="190" HeaderStyle-Font-Bold="true" />
<asp:TemplateField HeaderText="Willing to Relocate" ControlStyle-CssClass="c_width" HeaderStyle-CssClass="c_width">
<ItemTemplate>
<%# Convert.ToBoolean(Eval("WillingtoRelocate")) == true ? "Yes" : "No" %>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="LocationPreference" HeaderText="Location Preference" HeaderStyle-Width="190" HeaderStyle-Font-Bold="true" />
<asp:BoundField DataField="AppliedDate" DataFormatString="{0:MMMM dd, yyyy}" HeaderText="Date of Application" ReadOnly="true" HeaderStyle-Font-Bold="true" />
<asp:HyperLinkField DataNavigateUrlFields="CVFilePath" Text="View Resume" ControlStyle-ForeColor="White" ControlStyle-CssClass="bg" Target="_blank" />
</Fields>
</asp:DetailsView>
</div>
</div>
</div>
</ContentTemplate>
</asp:UpdatePanel>
I have a couple of gridviews in my website. Here's the code:
changeSlides.aspx:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataKeyNames="ID" DataSourceID="AccessDataSource1">
<Columns>
<asp:CommandField ShowDeleteButton="True" ShowEditButton="True" />
<asp:BoundField DataField="ID" HeaderText="ID" InsertVisible="False"
ReadOnly="True" SortExpression="ID" />
<asp:ImageField DataImageUrlField="picPath"
DataImageUrlFormatString="PlaceImages/{0}" HeaderText="Picture" ControlStyle-CssClass="editPhotoGridFormat">
<ControlStyle CssClass="editPhotoGridFormat"></ControlStyle>
</asp:ImageField>
<asp:BoundField DataField="PicPath" HeaderText="Filename" />
<asp:BoundField DataField="Title" HeaderText="Title" SortExpression="Title" />
<asp:BoundField DataField="City" HeaderText="City" SortExpression="City" />
<asp:BoundField DataField="Country" HeaderText="Country"
SortExpression="Country" />
</Columns>
</asp:GridView>
EditBlogPost.aspx:
<asp:GridView ID="GridView1" runat="server" AllowSorting="True"
AutoGenerateEditButton="True"
DataSourceID="AccessDataSource1"
AutoGenerateColumns="False" DataKeyNames="ID"
AlternatingRowStyle-BackColor="Gray"
AlternatingRowStyle-CssClass="editGridFormat" RowStyle-CssClass="editGridFormat"
RowStyle-VerticalAlign="Top"
onselectedindexchanged="GridView1_SelectedIndexChanged">
<AlternatingRowStyle BackColor="Gray" CssClass="editGridFormat"></AlternatingRowStyle>
<Columns>
<asp:CommandField ShowSelectButton="True" ShowDeleteButton="True" />
<asp:BoundField DataField="ID" HeaderText="ID" />
<asp:BoundField DataField="BlogTitle" HeaderText="BlogTitle"
SortExpression="BlogTitle" />
<asp:ImageField DataImageUrlField="Image" HeaderText="Image"
DataImageUrlFormatString="~/PlaceImages/{0}" ControlStyle-CssClass="editPhotoGridFormat"
AlternateText="Either a wrong file type or a misspelled file name has occurred"
NullDisplayText="No picture on file" >
<ControlStyle CssClass="editPhotoGridFormat"></ControlStyle>
</asp:ImageField>
<asp:BoundField DataField="Caption"
HeaderText="Caption for Picture on Homepage" />
<asp:TemplateField headertext="Text for the homepage">
<EditItemTemplate>
<asp:TextBox id="PicTextBox" runat="server" text='<%# Bind("PicText")%>' textmode="MultiLine" height="300px" width="300px" />
</EditItemTemplate>
<ItemTemplate>
<%# Eval("PicText")%>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="TravelDate" HeaderText="Date of Travel"
SortExpression="TravelDate" DataFormatString="{0:MMMMMMMMM, dd, yyyy }" HtmlEncode="false" />
<asp:TemplateField headertext="Top of the Blog Post">
<EditItemTemplate>
<asp:TextBox id="BeginTextBox" runat="server" text='<%# Bind("BeginText")%>' textmode="MultiLine" height="300px" width="300px" />
</EditItemTemplate>
<ItemTemplate>
<%# Eval("BeginText")%>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="City" HeaderText="City" />
<asp:BoundField DataField="Country" HeaderText="Country"
SortExpression="Country" />
<asp:TemplateField headertext="Text at the End of the Post">
<EditItemTemplate>
<asp:TextBox id="EndTextBox" runat="server" text='<%# Bind("EndText")%>' textmode="MultiLine" height="300px" width="300px" />
</EditItemTemplate>
<ItemTemplate>
<%# Eval("EndText")%>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<RowStyle VerticalAlign="Top" CssClass="editGridFormat"></RowStyle>
</asp:GridView>
EditPeoplePhotos.aspx:
<asp:GridView ID="GridView1" runat="server" AllowSorting="True"
DataSourceID="AccessDataSource1" Width="493px">
<Columns>
<asp:CommandField ShowDeleteButton="True" ShowEditButton="True"
ShowSelectButton="True" />
<asp:ImageField DataImageUrlField="picPath"
DataImageUrlFormatString="photos/PeoplePhotos/{0}" HeaderText="Picture" ControlStyle-CssClass="editPhotoGridFormat">
</asp:ImageField>
</Columns>
</asp:GridView>
My update statements work, but my delete statements don't work in two of the three gridviews. My corresponding delete statements are all virtually the same: "DELETE FROM (Table Name) WHERE ID=?" but I get different error codes, which are as follows:
For changeSlides.aspx, it says "Entry with same key already exists"
For EditBlogPosts.aspx, it work fine. It deletes the blog.
For EditPeoplePhotos.aspx, it says "No value given for one or more required parameters"
Each of those pages use a separate, unrelated table in my database, and each query is only referring to one table, so there are no foreign keys used.
What is going on?
I have a GridView control that binds to a time(0) SQL data field. I would like to render the output as AM/PM (e.g. 1:35 PM) using the DataFormatString property. I have tried assigning several different values but the compiler throws a "Input string was not in a correct format." error in most cases. So far I have tried the following values:
{0:hh:mm tt}
{0:hh:mm:ss tt}
{0:hh:mm t}
{0:hh:mm:ss t}
{0:t} - complies but does not convert the format
{0:tt} - complies but does not convert the format
<asp:GridView SkinID="ResultsGrid" ID="GridViewAlerts" style="margin-top:2%"
runat="server" DataKeyNames="ID" AutoGenerateColumns="False" OnPageIndexChanging="GridViewAlerts_PageIndexChanging"
OnSorting="GridViewAlerts_Sorting" OnRowCommand="GridViewAlerts_RowCommand"
OnDataBound="GridViewAlerts_DataBound">
<Columns>
<asp:BoundField DataField="ID" HeaderText="ID" SortExpression="ID"
ItemStyle-HorizontalAlign="Left" HeaderStyle-HorizontalAlign="Left" ItemStyle-Width="5%">
<HeaderStyle HorizontalAlign="Left" />
<ItemStyle HorizontalAlign="Left" Width="5%" />
</asp:BoundField>
<asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name"
ItemStyle-HorizontalAlign="Left" HeaderStyle-HorizontalAlign="Left" ItemStyle-Width="10%">
<HeaderStyle HorizontalAlign="Left" />
<ItemStyle HorizontalAlign="Left" Width="10%" />
</asp:BoundField>
<asp:BoundField DataField="Description" HeaderText="Description" SortExpression="Description"
ItemStyle-HorizontalAlign="Left" HeaderStyle-HorizontalAlign="Left" ItemStyle-Width="30%">
<HeaderStyle HorizontalAlign="Left" />
<ItemStyle HorizontalAlign="Left" Width="30%" />
</asp:BoundField>
<asp:BoundField DataField="Interval" HeaderText="Interval" SortExpression="Interval" ItemStyle-HorizontalAlign="Left"
HeaderStyle-HorizontalAlign="Left" ItemStyle-Width="5%">
<HeaderStyle HorizontalAlign="Left" />
<ItemStyle HorizontalAlign="Left" Width="5%" />
</asp:BoundField>
<asp:BoundField DataField="Run_At" HeaderText="At" SortExpression="Run_At" ItemStyle-HorizontalAlign="Left"
HeaderStyle-HorizontalAlign="Left" DataFormatString="{0:t}" ItemStyle-Width="5%">
<HeaderStyle HorizontalAlign="Left" />
<ItemStyle HorizontalAlign="Left" Width="5%" />
</asp:BoundField>
<asp:BoundField DataField="Last_Run_Dt" HeaderText="Last Run" SortExpression="Last_Run_Dt" ItemStyle-HorizontalAlign="Left"
HeaderStyle-HorizontalAlign="Left" DataFormatString="{0: MM/dd/yyyy}" ItemStyle-Width="5%">
<HeaderStyle HorizontalAlign="Left" />
<ItemStyle HorizontalAlign="Left" Width="5%" />
</asp:BoundField>
<asp:BoundField DataField="Next_Run_Dt" HeaderText="Next Run" SortExpression="Next_Run_Dt" ItemStyle-HorizontalAlign="Left"
HeaderStyle-HorizontalAlign="Left" DataFormatString="{0: MM/dd/yyyy}" ItemStyle-Width="5%">
<HeaderStyle HorizontalAlign="Left" />
<ItemStyle HorizontalAlign="Left" Width="5%" />
</asp:BoundField>
<asp:BoundField DataField="Last_Result" HeaderText="Result" SortExpression="Last_Result" ItemStyle-HorizontalAlign="Left"
HeaderStyle-HorizontalAlign="Left" ItemStyle-Width="5%" >
<HeaderStyle HorizontalAlign="Left" />
<ItemStyle HorizontalAlign="Left" Width="5%" />
</asp:BoundField>
<asp:BoundField DataField="Active" HeaderText="Status" SortExpression="Active"
ItemStyle-HorizontalAlign="Left" HeaderStyle-HorizontalAlign="Left" ItemStyle-Width="5%">
<HeaderStyle HorizontalAlign="Left" />
<ItemStyle HorizontalAlign="Left" Width="5%" />
</asp:BoundField>
<asp:BoundField DataField="Owner" HeaderText="Owner" SortExpression="Owner"
ItemStyle-HorizontalAlign="Left" HeaderStyle-HorizontalAlign="Left" ItemStyle-Width="5%">
<HeaderStyle HorizontalAlign="Left" />
<ItemStyle HorizontalAlign="Left" Width="5%" />
</asp:BoundField>
<asp:CommandField ButtonType="Image" SelectImageUrl="~/Images/gear.png" HeaderText="Settings" ShowSelectButton="true" ShowHeader="True" ItemStyle-Width="5%" />
<asp:TemplateField HeaderText="Start/Stop" ItemStyle-Width="5%">
<ItemTemplate>
<asp:ImageButton runat="server" ID="ImageButtonStartStop" ToolTip="Start or stop this alert" ImageUrl="~/Images/start.png"
OnClientClick="GridViewAlerts_RowCommand" CommandName="StartStop" CausesValidation="false"
CommandArgument="<%# ((GridViewRow) Container).RowIndex %>" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Run" ItemStyle-Width="5%">
<ItemTemplate>
<asp:ImageButton runat="server" ID="ImageButtonRun" ToolTip="Manually run this alert" ImageUrl="~/Images/play.png"
OnClientClick="GridViewAlerts_RowCommand" CommandName="Run" CausesValidation="false"
CommandArgument="<%# ((GridViewRow) Container).RowIndex %>" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
![screenshot][1]
As #JozefBenikovský pointed out in the comments, you have to use the TimeSpan format strings. This requires you to escape the : field in the format string (or whatever character you use as the separator).
Note that the TimeSpan DOES NOT have an AM/PM designator format field string. To have the AM/PM designator, you will need to cast your SQL Server Time type to a DateTime and then apply the standard DateTime format strings.
The sample below illustrates both concepts:
<div>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:AdventureWorksConnectionString %>"
SelectCommand="SELECT ProductId, StoreTime, cast(StoreTime as DateTime) as StoreTimeAsDateTime FROM junk.dbo.[Product]">
</asp:SqlDataSource>
<asp:GridView ID="GridView1" runat="server"
AutoGenerateColumns="False"
DataKeyNames="ProductID" DataSourceID="SqlDataSource1">
<Columns>
<asp:BoundField DataField="ProductID"
HeaderText="ProductID"
InsertVisible="False" ReadOnly="True"
SortExpression="ProductID"
DataFormatString="{0:D6}" />
<asp:BoundField DataField="StoreTime"
HeaderText="StoreTime"
SortExpression="StoreTime"
DataFormatString="{0:hh\:mm\:ss}" />
<asp:BoundField DataField="StoreTimeAsDateTime"
HeaderText="StoreTimeAsDateTime"
SortExpression="StoreTime"
DataFormatString="{0:hh:mm:ss tt}" />
</Columns>
</asp:GridView>
</div>
Make sure the field in the database is also in the datetime format. For example, the DataFormatString property does not work if the table field is in varchar format.