I am trying to present two rows per record simply because having them all presented in a single will make to wide to present to the user. I tried creating a new row during rowcreated, but all it did was display the text in the header. In my code below,
I am trying to display Eval("column5") and Eval("column6") under its own row within the same record.
Below is what I have so far...
Protected Sub gridview1_RowCreated(ByVal sender
As Object,
ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs)
Dim gr As
New GridViewRow(0, 0, DataControlRowType.DataRow, DataControlRowState.Normal)
Dim mc As
New TableCell()
mc.ColumnSpan = 3
mc.RowSpan = 2
mc.Text = "This is row 2" gr.Cells.Add(mc)
A better option would be to use a repeater or a datalist instead. The gridview is intended to be one table row per data row. A datalist is more flexible and a repeater is too. A datalist allows you to set the template for how each row is rendered including
a separator template and an item template.
--JJ
Please mark as answered if I helped.
I don't answer personal emails unless I know you or of you. Feel free to post in the forum to get an answer from me.
Marked as answer by jwhite128 on Dec 10, 2007 02:35 PM
I was hoping that I wouldn't have to use a datalist or repeater... I really like the ease of use of the gridview and its paging and sorting capabilities. I've also tried adding multiple labels to the grid, but unfortunately some of the items are too long
in length.
Please mark as answered if I helped.
I don't answer personal emails unless I know you or of you. Feel free to post in the forum to get an answer from me.
This worked to display the last column as a 2nd row. The header has an extra blank column, so it's not real pretty, but functional at least. I close off the current TableCell/TableRow, open a new one with a cell that is ColSpan the width of the table and
then re-open the row and cell. In this case I indented past the first column and used the right-most 11 of 12 columns to display a pair of wide text fields.
public string TitleNotes(object ttl, object notes)
{
StringBuilder r = new StringBuilder();
// Close the current cell and row and start a new one
r.Append("</td></tr><tr><td> </td><td colspan=11>");
if (ttl != DBNull.Value)
{ // Title should never but null, but still worth checking
r.Append("<b>");
r.Append((string)ttl);
r.Append("</b>");
}
// Display the notes if present
if (notes != DBNull.Value)
{
r.Append(" ");
r.Append((string)notes);
}
return r.ToString();
}
jwhite128
Member
14 Points
28 Posts
gridview 2 rows per record
Dec 10, 2007 12:52 AM|LINK
I am trying to present two rows per record simply because having them all presented in a single will make to wide to present to the user. I tried creating a new row during rowcreated, but all it did was display the text in the header. In my code below, I am trying to display Eval("column5") and Eval("column6") under its own row within the same record.
Below is what I have so far...
Protected Sub gridview1_RowCreated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs)
Dim gr As New GridViewRow(0, 0, DataControlRowType.DataRow, DataControlRowState.Normal)
Dim mc As New TableCell()
mc.ColumnSpan = 3
mc.RowSpan = 2
mc.Text = "This is row 2" gr.Cells.Add(mc)
Me.gridview1.Controls(0).Controls.AddAt(0, gr)
End Sub
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="1" cellspacing="0" PageSize="10" HeaderStyle-BackColor="#CCDDEE" Width="100%" CssClass="GridTable" GridLines="Horizontal" BorderColor="#CECED8" OnRowCreated="gridview1_RowCreated">
<Columns>
<asp:TemplateField HeaderText="" ItemStyle-HorizontalAlign="Center">
<ItemTemplate>
<%# Eval("column1")%>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="" ItemStyle-HorizontalAlign="Center">
<ItemTemplate>
<%# Eval("column2")%>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="" ItemStyle-HorizontalAlign="Center">
<ItemTemplate>
<%# Eval("column3")%>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="" ItemStyle-HorizontalAlign="Center">
<ItemTemplate>
<%# Eval("column4")%>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="" ItemStyle-HorizontalAlign="Center">
<ItemTemplate>
<%# Eval("column5")%>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="" ItemStyle-HorizontalAlign="Center">
<ItemTemplate>
<%# Eval("column6")%>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
jose_jimenez
Contributor
2504 Points
484 Posts
Re: gridview 2 rows per record
Dec 10, 2007 01:22 AM|LINK
A better option would be to use a repeater or a datalist instead. The gridview is intended to be one table row per data row. A datalist is more flexible and a repeater is too. A datalist allows you to set the template for how each row is rendered including a separator template and an item template.
--JJ
I don't answer personal emails unless I know you or of you. Feel free to post in the forum to get an answer from me.
vinz
All-Star
128483 Points
18150 Posts
MVP
Re: gridview 2 rows per record
Dec 10, 2007 01:48 AM|LINK
Have you tried adding two Labels under a templatefield like
<asp:TemplateField HeaderText="" ItemStyle-HorizontalAlign="Center">
<ItemTemplate>
<Label1>
<Label2>
</ItemTemplate>
</asp:TemplateField>
Then populate the label with the data from your DB
MessageBox Controls for WebForms | Blog | Twitter | Linkedin
jwhite128
Member
14 Points
28 Posts
Re: gridview 2 rows per record
Dec 10, 2007 02:35 PM|LINK
I was hoping that I wouldn't have to use a datalist or repeater... I really like the ease of use of the gridview and its paging and sorting capabilities. I've also tried adding multiple labels to the grid, but unfortunately some of the items are too long in length.
Thank you for your advice.
jose_jimenez
Contributor
2504 Points
484 Posts
Re: gridview 2 rows per record
Dec 11, 2007 12:03 AM|LINK
4guysfromRolla did an article on paging using the pageddatasource for the repeater and datalist. See http://aspnet.4guysfromrolla.com/articles/081804-1.aspx.
They also have a sorting article.
good luck.
--JJ
I don't answer personal emails unless I know you or of you. Feel free to post in the forum to get an answer from me.
timhoffmann
Member
2 Points
2 Posts
Re: gridview 2 rows per record
Jan 28, 2010 02:15 PM|LINK
This worked to display the last column as a 2nd row. The header has an extra blank column, so it's not real pretty, but functional at least. I close off the current TableCell/TableRow, open a new one with a cell that is ColSpan the width of the table and then re-open the row and cell. In this case I indented past the first column and used the right-most 11 of 12 columns to display a pair of wide text fields.
<Columns>
<asp:TemplateField>...</asp:TemplateField>
...
<asp:TemplateField>
<ItemTemplate>
<%# TitleNotes(Eval("Title"), Eval("Notes")) %>
</ItemTemplate>
</asp:TemplateField>
</Columns>
public string TitleNotes(object ttl, object notes)
{
StringBuilder r = new StringBuilder();
// Close the current cell and row and start a new one
r.Append("</td></tr><tr><td> </td><td colspan=11>");
if (ttl != DBNull.Value)
{ // Title should never but null, but still worth checking
r.Append("<b>");
r.Append((string)ttl);
r.Append("</b>");
}
// Display the notes if present
if (notes != DBNull.Value)
{
r.Append(" ");
r.Append((string)notes);
}
return r.ToString();
}