Is there a way to get datatable into report format below using Repeater, Gridview, DataList, etc.?
Given DataTable:
dept, section, title
-----------------------
IT,Tech,Tech1
IT,Tech,Tech2
IT,Dev,Dev1
IT,Dev,Dev2
HR,Ben,Spec1
Display Format for Repeater, DataList, GridView, etc.
IT
Tech
Tech1
Tech2
Dev
Dev1
Dev2
HR
Ben
Spec1
You can use nested repeater to fulfill this requirement, assuming no. of columns are fixed in the datatable.
You will have to put 3 repeaters.
example:
<asp:Repeater ID="repeaterDept" runat="server">
<ItemTemplate>
<asp:Repeater ID="repeaterSection" runat="server">
<ItemTemplate>
<asp:Repeater ID="repeaterTitle" runat="server">
</asp:Repeater>
</ItemTemplate>
</asp:Repeater>
</ItemTemplate>
</asp:Repeater>
Now, derive distinct Departments from your datatable and bind repeaterDept with that. On, ItemDataBound event, retrieve respective child sections of the binding department and bind it with repeaterSection. Follow the same for repeaterTitle.
I think, this should fulfill your requirement.
Related
I want to bind data from one field to gridview, but in gridview it must be displayed as two column, how can I make it possible? Can anybody help me..
Using a Repeater having float (with < 50% width) on item style you can achieve something like that.
markup
<asp:Repeater runat="server" ID="rptMyData">
<ItemTemplate>
<div style="float:left;width: 45%">
<%# Eval("MyField") %>
</div>
</ItemTemplate>
</asp:Repeater>
Totally new to webforms user controls, I am bit confused, on how to create a user control and fill some data on it.
for(int i = 0; i < Price.EpList.Count(); i++)
{
Price.EpList[i].Amount.ToString();
Price.EpList[i].Code.ToString();
Price.EpList[i].Desc.ToString();
Price.EpList[i].ID.ToString();
}
EpList is a list that contains info that i want to display in webpage on tabular format with checkboxes on each row.
Take a look at the Repeater Control. You don't have to loop through your list, you just bind the list to the repeater and define the html template you want for each repeated item.
http://www.w3schools.com/aspnet/aspnet_repeater.asp
EDIT: That article uses Visual Basic, so here's the C# translation:
Assuming this repeater:
<asp:Repeater runat="server" ID="uxEpList">
<ItemTemplate>
<%--Html goes here--%>
<%# Eval("Amount")%>
<%# Eval("Code")%>
<%# Eval("Desc")%>
<%# Eval("ID")%>
</ItemTemplate>
</asp:Repeater>
In code behind:
uxEpList.DataSource = Price.Eplist;
uxEpList.DataBind();
If you need to nest a repeater inside another one (using the Desc property from your comment) you can do it like this, by setting the DataSource property declaratively (note the single quotes):
<asp:Repeater runat="server" ID="uxEpList">
<ItemTemplate>
<asp:Repeater Datasource='<%# Eval("Desc")%>' runat="server">
<ItemTemplate>
//etc...
I have GridView with my DAO as data source rendering some person data (name, surname...). I want to add to rendered table simple link to expanded view with more informations. Ithought I could add html link with POST argument to each row. But every html I try to pass to GridView gets escaped. Can I somehow unescape it? Or is there any other simple way?
It is my private, very quick project, I donĀ“t need any robust solution. Just the simplest and quickest one. Thanks.
It sounds like you want to use an ItemTemplate in your gridview to render HTML controls within the grid:
asp:GridView ID="myGrid" runat="server">
<Columns>
<asp:TemplateField HeaderText="Last Name">
<ItemTemplate>
<!--Any HTML can go here, below is a label -->
<label><%# DataBinder.Eval (Container.DataItem, "lastname") %><label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
If we add a customer header to the gridview, will it add extra row?
Currently I have a gridview with four columns and, when I add a custom header gridview coming with a five rows.
My code looks like this...
<asp:TemplateField HeaderText="" ItemStyle-Width="4%" HeaderStyle-HorizontalAlign="Center" ItemStyle-HorizontalAlign="Center">
<HeaderTemplate>
<asp:DropDownList ID="Select" runat="server">
<asp:ListItem>Country</asp:ListItem>
<asp:ListItem>Region</asp:ListItem>
<asp:ListItem>Title</asp:ListItem>
</asp:DropDownList>
</HeaderTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="" ItemStyle-Width="4%" HeaderStyle-HorizontalAlign="Center" ItemStyle-HorizontalAlign="Center">
<ItemTemplate>
<asp:HiddenField ID="Id" Value='<%#Eval("id")%>' runat="server" />
<asp:Literal ID="ltrImage" runat="server"></asp:Literal>
</ItemTemplate>
</asp:TemplateField>
and three other TemplateFields...
Is there any problem of adding header this way? Any other way to add customer header without having this issue?
My desired output should look like this
Search Result Search By (Dropdownlist)
Data column1 Data column2 Data column3 Data column4
The one I am getting now is
Sort By (Dropdownlist)
Data column1 Data column2 Data column3 Data column4
Could anyone help if have an idea?
Thanks in advance
You just added another column with custom header to your gridview. If you want to customize a header of the first column, just customize the header of your first template field:
<asp:TemplateField HeaderText="" ItemStyle-Width="4%" HeaderStyle-HorizontalAlign="Center" ItemStyle-HorizontalAlign="Center">
<HeaderTemplate>
<asp:DropDownList ID="Select" runat="server">
<asp:ListItem>Country</asp:ListItem>
<asp:ListItem>Region</asp:ListItem>
<asp:ListItem>Title</asp:ListItem>
</asp:DropDownList>
</HeaderTemplate>
<ItemTemplate>
<asp:HiddenField ID="Id" Value='<%#Eval("id")%>' runat="server" />
<asp:Literal ID="ltrImage" runat="server"></asp:Literal>
</ItemTemplate>
</asp:TemplateField>
If this dropdownlist is too big or if you want add some additional text in the header, you can always create HeaderTemplate for other TemplateFields of merge some of the column's headers in code behind (example for "merging" two first headers, Id of the gridView is gridView1):
protected void gridView1_PreRender(object sender, EventArgs e)
{
int indexOfColumnToSpan = 0;
int indexOfColumnToRemoveHeader = 1;
gridView1.HeaderRow.Cells[indexOfColumnToSpan].ColumnSpan = 2;
gridView1.HeaderRow.Cells.RemoveAt(indexOfColumnToRemoveHeader);
}
well, you have not actually added a header row, but a header column .. which is basically just another column with more controls ..
why dont you place your DropDownList before your GridView, and then you can write some code behind handling the DropDownList_SelectedIndexChanged event to sort the Data ?
I have implemented this without using the filters within the GridView. Then you can handle the events separately, and populate the GridView accordingly.
What you are actually adding is a column, instead of a row. That's why you are getting that result.
Hope this helps!
I have a GridView that is populated via a database, inside the GridView tags I have:
<Columns>
<asp:TemplateField>
<ItemTemplate><asp:Panel ID="bar" runat="server" /></ItemTemplate>
</TemplateField>
</Columns>
Now, I want to be able to (in the code) apply a width attribute to the "bar" panel for each row that is generated. How would I go about targeting those rows? The width attribute would be unique to each row depending on a value in the database for that row.
<asp:Panel ID="bar" runat="server" Width='<%# Eval("Width") %>' />
If you like, you can change Eval("Width") to the expression that calculates the width.
You are going to want to handle the GridView's RowCreated event. This occurs just after the GridView creates each row and will give you programmatic access to the row and all the controls contained within it.
Here is the MSDN page on the event.
I suggest you to not use Eval, if you can, because it's a little slower. In that cases I generally prefer to cast my data source to his base type:
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:Panel ID="bar" runat="server" Width='<%# ((yourCustomData)Container.DataItem).Width %>' />
</ItemTemplate>
</TemplateField>
</Columns>
where yourCustomData is the row type of your data source (i.e. the element of a List<>).
This method is really faster than Eval.
Edit: oh, don't forget to include in the page a reference to the namespace that contains yourCustomData
<%# Import Namespace="yourNameSpace.Data" %>
I suggest you attach a callback handler to the OnRowDataBound event. Each row binding will fire an event which you can handle in your callback handler.
In that callback handler, you can get the info about the item being bound to the row and apply the width according to the values in your database.