I have two results from a data set. I want to add both results in one gridview column. I do not want to merge in the code behind. Is there another way to do that?
<asp:TemplateField HeaderText="Entered Date" SortExpression="Date">
<ItemTemplate>
<div style="text-align: center;">
<asp:Label ID="Label3" runat="server" Text='<%# formatDisplayDate(Eval("Date").ToString()) %>'></asp:Label>
</div>
</ItemTemplate>
</asp:TemplateField>
Now i would return "04/04/2009". I would like to return "04/04/2009-PASSED"
Have you tried:
formatDisplayDate(Eval("Date").ToString()) & "-" & Eval("OtherField").ToString()
If that doesn't work you should be able to set up a TemplateField for the combined rows like this:
<asp:TemplateField HeaderText="CombinedFieldName" SortExpression="CombinedFieldName">
<ItemTemplate>
<asp:Label ID="Label1" runat="server"
Text='<%# DataBinder.Eval(Container,"DataItem.FirstField") %>' >
</asp:Label>
-
<asp:Label ID="Label2" runat="server"
Text='<%# DataBinder.Eval(Container,"DataItem.SecondField") %>' >
</asp:Label>
</ItemTemplate>
</asp:TemplateField>
That said - it is usually much more efficient to do the field concatenation in the database operation if possible. I haven't tested, but I would assume that concatenating in the SELECT would be fastest, followed by concatenating in the data set in the codebehind, and combining them on the ASP.Net page would be slowest.
Another option would be to create a calculated column in the DataTable, using the DataColumn.Expression property
My first question is why don't you want to merge in the code behind? The more content you bind to a control the more data that gets put into that controls view state.
#Gary.Ray has a really good solution, but something you might find better is to use linq to build a single data object from your existing data sets that way you only include what you need and the data is a lot easier to isolate, think single responsibility pattern here, if you can move the code that deals with getting your data to somewhere that only deals with getting data your application will be MUCH easy to maintain in the future.
Related
I'm currently coding a web application using ASP.NET. So far my use of ASP.NET and the application has been going great, however there is one feature I would love to have, but I cannot for the life of me figure out how to do it.
I want to use some sort of list/form view, which will show both data records and empty blank templates. For example, I want to use this as an "event booking" system, where the user would click an open slot and this would then be registered to our database. This open slot is the empty template I would like.
I've done a quick paint picture to illustrate what I mean:
I assume that I'm in the right direction using a ListView, but I would like some feedback/help on a better method to achieve this.
This is my listview code (it is somewhat basic, barebones almost):
<asp:ListView ID="LV_AvailableUpdateSlots" runat="server" ItemPlaceholderID="itemPlaceholder">
<LayoutTemplate>
<asp:PlaceHolder ID="itemPlaceHolder" runat="server"></asp:PlaceHolder>
</LayoutTemplate>
<EmptyDataTemplate>No data for this date.</EmptyDataTemplate>
<ItemTemplate>
<asp:Panel ID="Pnl_AvailableSlot" runat="server" CssClass="UpdateDiarySlotPanel">
<div class="FloatLeftInline">
<asp:Label ID="Lbl_ClientName" runat="server" Text="Name: "></asp:Label>
<asp:Label ID="Lbl_ClientNameEval" runat="server" Text='<%# Eval("RefID") %>'></asp:Label>
</div>
<asp:Label ID="Lbl_ClientProductEval" runat="server" Text='<%# Eval("ClientMainProductID") %>'></asp:Label>
<div class="FloatRightInline">
<asp:Label ID="Lbl_Version" runat="server" Text="Version: "></asp:Label>
<asp:Label ID="Lbl_VersionEval" runat="server" Text= '<%# Eval("Version") %>'></asp:Label>
</div>
</asp:Panel>
</ItemTemplate>
<InsertItemTemplate>
Insert here.
</InsertItemTemplate>
<EmptyItemTemplate>No data found.</EmptyItemTemplate>
</asp:ListView>
A ListView sounds good, but I does not hurt to take a look at Repeater
Repeater Web Server Control Declarative Syntax
If you have the time do a simple test implementing using a ListView and a Repeater to compare the benefits and see which one fits better your needs and appearance
I'm wondering if it is possible to format a Grid View like the pattern below
Usual Grid View:
Name Address Age Gender <--- Fields Name
Example Example Example Example <--- Values
What I want to look like
"Fields" "Values"
Name Example
Address Example
Age Example
Gender Example
Any thought will be highly appreciated
you should look into using a repeater.
I think that GridView was simply not meant to be used that way. Usually, you will display several items in it and if those items are too many you will end up with the horizontal scrolling mistake (also, some nice arguments here).
If you are showing only one record, you should use a DetailsView control, which:
Displays the values of a single record from a data source in a table,
where each data row represents a field of the record. The DetailsView
control allows you to edit, delete, and insert records.
I got an Answer using this Codes:
<asp:GridView ID="GridView1" runat="server" DataSourceID="SqlDataSource1" AutoGenerateColumns="False" >
<Columns>
<asp:TemplateField HeaderText="">
<ItemTemplate>
Name: <asp:Label ID="Label1" runat="server" Text='<%# Eval("Name") %>'></asp:Label><br />
Address: <asp:Label ID="Label2" runat="server" Text='<%# Eval("Address") %>'></asp:Label><br />
Postcode: <asp:Label ID="Label3" runat="server" Text='<%# Eval("Postcode") %>'></asp:Label><br />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
I have the below column on my table what gets binded on pageload, the parameters in there work fine but i need to add an additional parameter which is the fullname which is the next column along but im having trouble figuring our the syntax,
here is my ASP
<asp:TemplateField HeaderText="ID">
<ItemTemplate>
<asp:HyperLink ID="hyperLeadID" runat="server" NavigateUrl='<%#Eval("ID","/documents/Q-Sheet.aspx?LeadID={0}&isHappyCallReferral=yes&isHappyName={1}") %>'
Text='<%#Eval("ID")%>'></asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Referral Name">
<ItemTemplate>
<asp:Label ID="lblRefName" CssClass="gvItem" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.Name") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
As you can see at the end of ID column i have added isHappyName={1} which i assumed it would select the next column along as it starts at 0 but it keeps throwing an error which is
"Index (zero based) must be greater than or equal to zero and less than the size of the argument list."
Can someone help me to pass the usersname through the URL
Thanks
Rewrite the hyperlink like this:
NavigateUrl='<%# string.Format("/documents/Q-Sheet.aspx?LeadID={0}&isHappyCallReferral=yes&isHappyName={1}", Eval("ID"), Eval("HappyName"))%>'
The problem is with a call to String.Format (presumably <asp:HyperLink> is doing this internally). It's only being provided with a single argument, whereas {1} refers to a second, non-provided, argument.
Try using <a href="#"> instead without using asp:HyperLink (and ASP.NET's built-in databinding) and setting the URI manually - I'm not a fan of System.Web.UI.WebControls at all for this reason (I strongly suggest you look into ASP.NET MVC when you get the chance).
I have created a dropdown list in a DetailsView control
I want to make that dropdown have multiple columns. Is that possible? If So how?
I've done a lot of searching and cant seem to find the solution.
Below is the code for my dropdown template:
<asp:TemplateField HeaderText="Division" SortExpression="fDivisionID">
<EditItemTemplate>
<asp:DropDownList
ID="DivisionDropDownList"
runat="server"
DataSourceID="DivisionSqlDataSource"
DataTextField="DivisionName"
DataValueField="DivisionID"
Text='<%# Bind("fDivisionID") %>'>
</asp:DropDownList>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("DivisionName") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
How do I add another column to that.. such as "Location"..
Thanks in advance
There are I think three ways actually.
Add two columns in your Select statement when fetching from database like below. or from here
Select Column1 + ' ' + Column2 From Yourtable
You can use third party controls.
Jquery Also supports this feature Here
I have a student enrollment gridview populated with Enrollment objects. The Enrollment object has student Id. With that Id, I am populating 2 columns (from 3 properties of Student object) of enrollment gridview.
Right now, I am doing it with below code. This means 3 round trips to database for the same object, which is so very bad. How can I use the same object to get the 3 properties?
<asp:TemplateField HeaderText="Student Name">
<ItemTemplate>
<asp:Label ID="lblName" runat="server" Text='<%# string.Format("{0} - {1}", StudentProfile.GetStudentProfileById(Convert.ToInt32(Eval("StudentId"))).FirstName, StudentProfile.GetStudentProfileById(Convert.ToInt32(Eval("StudentId"))).Surname) %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Student DOB">
<ItemTemplate>
<asp:Label ID="lblDOB" runat="server" Text='<%# StudentProfile.GetStudentProfileById(Convert.ToInt32(Eval("StudentId"))).DateOfBirth %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
Edit: FYI, I have to deal with updating the gridview rows which will involve adding textboxes and so to each row. Can you please keep the answers apt to this point?
Thank you very much!
The way you are binding data to the gridview is wrong. You need to get the Data in an Object/Collection and then bind that data as a DataSource to your GridView.
Take a look here Displaying Data With the ObjectDataSource and look at this as well Querying Data with the SqlDataSource Control