I have a grid view of Cart Items, which was bonded at run-time, Now my requirement for that is i want two header row in Grid as given below in image , How will it be possible ? can anyone help me?
My Design For Grid http://content.screencast.com/users/Pr6546/folders/Default/media/f7da2da4-f80e-4674-b1d4-0ccd872966d2/Capture.PNG
http://www.screencast.com/t/g4HkqlSpx
Below is my grid Source
<asp:GridView ID="gvCheckOutItems" GridLines="None" ShowFooter="true" DataKeyNames="Item_No"
Width="100%" border="0" CellSpacing="0" CellPadding="5" AutoGenerateColumns="false"
CssClass="Checkout-Grid" runat="server" OnRowCreated="gvCheckOutItems_RowCreated">
<Columns>
<asp:TemplateField HeaderStyle-Width="7%">
<ItemTemplate>
<asp:LinkButton ID="lnkBtnRemove" runat="server" OnClick="lnkBtnRemove_Click" Text="Remove"
CssClass="blue-link"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField HeaderText="Item No" HeaderStyle-HorizontalAlign="Left" ItemStyle-HorizontalAlign="Left"
DataField="Item_No" HeaderStyle-Width="7%" />
<asp:BoundField HeaderText="Title" HeaderStyle-HorizontalAlign="Left" FooterStyle-HorizontalAlign="Center"
ItemStyle-HorizontalAlign="Left" DataField="Title" HeaderStyle-Width="25%" />
<asp:BoundField HeaderText="Offered By" HeaderStyle-HorizontalAlign="Left" ItemStyle-HorizontalAlign="Left"
DataField="Trainer" HeaderStyle-Width="16%" FooterText="<strong>Order Total</strong>" />
<asp:TemplateField>
<ItemTemplate>
<asp:TextBox ID="txtItemFormat" runat="server" Text='<%# Eval("Item_Format") %>'
Visible="false"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField HeaderText="Format" HeaderStyle-HorizontalAlign="Left" ItemStyle-HorizontalAlign="Left"
DataField="Format" HeaderStyle-Width="15%" />
<asp:BoundField HeaderText="Duration" ItemStyle-HorizontalAlign="Right" DataField="Duration"
HeaderStyle-Width="7%" />
<asp:TemplateField HeaderStyle-Width="7%" HeaderStyle-HorizontalAlign="Right" ItemStyle-HorizontalAlign="Right"
FooterStyle-HorizontalAlign="Right" HeaderText="Quantity">
<ItemTemplate>
<asp:TextBox ID="txtQuantity" Enabled="false" CssClass="quantity" Text='<%#Eval("Quantity")%>'
onblur="fnquantityEmpty(this)" onkeyup="extractNumber(this,0,false);" onkeypress="return blockNonNumbers(this, event, false, false);"
runat="server" MaxLength="4" AutoPostBack="true" OnTextChanged="Qty_Changed"></asp:TextBox>
</ItemTemplate>
<FooterTemplate>
<strong>
<asp:Label ID="lblFooterQuantity" runat="server"></asp:Label></strong>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Price" ItemStyle-HorizontalAlign="Right" HeaderStyle-HorizontalAlign="Center"
HeaderStyle-Width="7%">
<ItemTemplate>
<asp:Label ID="lblPrice" runat="server" Text='<%#getConvertedPrice(Eval("Price")) %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:TextBox ID="txtItemOwnerID" runat="server" Text='<%# Eval("ItemOwnerID") %>'
Visible="false"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Subtotal" ItemStyle-HorizontalAlign="Right" HeaderStyle-HorizontalAlign="Right"
FooterStyle-HorizontalAlign="Right" HeaderStyle-Width="8%">
<ItemTemplate>
<asp:Label ID="lblSubTotal" runat="server" Text='<%#getConvertedPrice(Eval("Subtotal")) %>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<strong>
<asp:Label ID="lblFooterTotalPrice" runat="server" Text="" ToolTip="Total"></asp:Label></strong>
</FooterTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Regards
Have you considered abandoning the gridview and using a repeater control instead? This would give you more flexibility.
Best thing I would suggest is add a row which should be on top of your datasource and for Title & Type/ Format add Offered By & Terms/Schedule as values respectively.
This would generate the desired output for you.
Happy Coding!!!
An option could be to handle the RowDataBound and on the RowType for Header you set your own RenderMethod and write the HTML yourself.
e.Row.SetRenderMethodDelegate(New RenderMethod(AddressOf RenderHeader))
Please check the links below. They provide a solution on how to apply multiple rows in GridView. Hopefully, it will get you going.
Real World GridView: Two Headed & Grouping GridViews
Dynamic Multiple Row Column Grid Header
Related
I want to display two column value in one DetailsView cell.
Aspx:
<asp:DetailsView ID="DetailsView1" runat="server" AutoGenerateRows="False" DataKeyNames="Id" DataSourceID="data">
<Fields>
<asp:BoundField DataField="TicketNo2" HeaderText="Ticket No" SortExpression="TicketNo2" />
<asp:BoundField DataField="TicketNo" HeaderText="Ticket Serial" SortExpression="TicketNo" />
</Fields>
</asp:DetailsView>
The above code is showing values in two seperate columns of the DetailsView.
How can I display both values in one column?
I find out the answer to display two column value in one details view cell. Given below code worked for me.
ASPX:
<asp:DetailsView ID="DetailsView1" runat="server" AutoGenerateRows="False" DataKeyNames="Id" DataSourceID="data">
<Fields>
<asp:TemplateField HeaderText="Ticket No" SortExpression="TicketNo2">
<EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label6" runat="server" Text='<%# Bind("TicketNo2") %>'></asp:Label>
<asp:Label ID="Label7" runat="server" Text='<%# Bind("TicketNo") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Fields>
</asp:DetailsView>
I have GridView:
<asp:GridView ID="MyGridView" runat="server" ShowFooter="true"
AutoGenerateColumns="False" Visible="True">
<Columns>
<asp:BoundField DataField="id" ItemStyle-HorizontalAlign="center"/>
<asp:BoundField DataField="fullName" />
<asp:TemplateField HeaderText="situation>">
<ItemTemplate>
<asp:DropDownList ID="dl_situation" runat="server">
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:TextBox ID="tbNr" runat="server"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Data">
<ItemTemplate>
<asp:TextBox ID="tbDate" runat="server"></asp:TextBox>
</asp:RequiredFieldValidator>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
It work fine, but when I add RegularrExpressionValidator for textbox with id="tbDate" mygrid does not work
This regular expression must accept datetime in format "dd.MM.yyyy"
<asp:GridView ID="MyGridView" runat="server" ShowFooter="true"
AutoGenerateColumns="False" Visible="True">
<Columns>
<asp:BoundField DataField="id" ItemStyle-HorizontalAlign="center"/>
<asp:BoundField DataField="fullName" />
<asp:TemplateField HeaderText="situation>">
<ItemTemplate>
<asp:DropDownList ID="dl_situation" runat="server">
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:TextBox ID="tbNr" runat="server"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Data">
<ItemTemplate>
<asp:TextBox ID="tbDate" runat="server"></asp:TextBox>
<asp:RegularExpressionValidator ID="RegularCert" runat="server"
ControlToValidate="tbDate" ErrorMessage="Incorect Data"
ValidationExpression=
"(0[1-9]|[12][0-9]|3[01])[-.](0[1-9]|1[012])[-.](19|20)[0-9]{2}"
Display="Dynamic"></asp:RegularExpressionValidator>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"
ErrorMessage="*" ControlToValidate="tbDate">
</asp:RequiredFieldValidator>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
I think you typed "tbData" instead of "tbDate" in the ControlToValidate property
<asp:RegularExpressionValidator ID="RegularCert" runat="server"
ControlToValidate="tbDate" ErrorMessage="Incorect Data" ValidationExpression= "(0[1-9]|[12][0-9]|3[01])[-.](0[1-9]|1[012])[-.](19|20)[0-9]{2}"
Display="Dynamic"></asp:RegularExpressionValidator>
I changed ClientIDMode to Predictable for TextBox, RegularExpressionValidator and RequiredFieldValidator and began to work
change ControlToValidate="tbData" to ControlToValidate="tbDate" for <asp:RegularExpressionValidator
Have you tried placing the <asp:RegularExpressionValidator ...> inside the
<ItemTemplate>
<asp:TextBox ID="tbDate" runat="server"></asp:TextBox>
</ItemTemplate>
also you need to indicate some text to display if it fails the regex validation
actually what Hallie said is more than likely the answer ha
My GridView is DataBound to a SQL data connection which should almost always return data. So using an EmptyDataTemplate does not help me because the GridView should never be empty. But I want the first few rows to be editable so the user can add new information to the GridView. So I've crafted my Select statement to always come back with 3 empty rows. I want those rows to contain TextBoxes instead of Labels. But this:
<asp:GridView ID="GridView1" runat="server"
AutoGenerateColumns="False" DataSourceID="SqlDataSource1"
BorderColor="White" BorderStyle="Solid"
onselectedindexchanged="GridView1_SelectedIndexChanged" ShowFooter="False"
ViewStateMode="Disabled">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<input type="checkbox" id ="CheckBox1" class="checkbox" />
</ItemTemplate>
<ItemStyle HorizontalAlign="Center" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Serial" SortExpression="Serial">
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("Serial") %>'></asp:Label>
</ItemTemplate>
<ItemStyle HorizontalAlign="Center" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Model" SortExpression="Model">
<EditItemTemplate>
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("Model") %>'></asp:Label>
</ItemTemplate>
<ItemStyle HorizontalAlign="Center" />
</asp:TemplateField>
<asp:TemplateField HeaderText="From Store">
<ItemTemplate>
<asp:DropDownList ID="DropDownList1" runat="server" CssClass="dropdownlist"
DataSourceID="SqlDataSource2" DataTextField="Store"
SelectedValue='<%# Bind("Store") %>'>
</asp:DropDownList>
</ItemTemplate>
<ItemStyle HorizontalAlign="Center" />
</asp:TemplateField>
<asp:TemplateField HeaderText="To Store">
<ItemTemplate>
<asp:DropDownList ID="DropDownList2" runat="server" CssClass="dropdownlist"
DataSourceID="SqlDataSource2" DataTextField="Store">
</asp:DropDownList>
</ItemTemplate>
<ItemStyle HorizontalAlign="Center" />
</asp:TemplateField>
</Columns>
<FooterStyle HorizontalAlign="Center" ForeColor="White" BorderColor="White" BorderStyle="Solid" />
<HeaderStyle ForeColor="White" BorderColor="White" BorderStyle="Solid" />
<RowStyle BorderColor="White" BorderStyle="Solid" ForeColor="White" />
</asp:GridView>
Produces this:
Where the first 3 rows have uneditable Labels rather than TextBoxes. Is what I want to do possible?
In your template fields add <FooterTemplate></FooterTemplate> This makes the footer row of your gridview a place where you can add new rows. Of course you will need to put items inside the <FooterTemplate>, but the work the same as your <ItemTemplates
So to serve my specific purpose, I decided just to create a table separate from this with an empty row with textboxes. Then a button that used JQuery to take the values from that table to append them into the uneditable row in the DataGrid. Removed the header row from the DataGrid so it all looked like the same table.
How can I get a Product_ID from a gridviewrow without creating a column because every time I have to make the column visible and invisible to perform gridview data operation.
Thanks in advance.
Make it part of one of your columns using a label similar to this:
<asp:TemplateField HeaderText="Name" SortExpression="name">
<ItemTemplate>
<asp:Label ID="productdIdLabel" runat="server" Text='<%# bind("Product_ID") %>' Visible="false"></asp:Label>
<asp:Label Visible="true" runat="server" ID="productNameLabel" Text='<%# bind("Product_Name") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
Hope this helps! Cheers
<asp:HiddenField ID="HiddenField1" runat="server" Value"<%# Eval("id")%>" />
<asp:TemplateField HeaderText="Name" SortExpression="name" visible="False">
<ItemTemplate>
<asp:label id="prodId" runat=server" Text="<%# Eval("id")%>" ></asp:label>
</ItemTemplate>
I have a GridView that I use to show my users the result of a search. I want to allow them to choose which columns are shown on the GridView when performing their search. Simple enough, yes? I wanted to try doing this using just databinding, no events. Unfortunately, my code fails to update the GridView using checkboxes bound to the column's Visible property. The state of the chechboxes changes, but the Visible property of the columns does not.
Snippet of Search.aspx:
<myControl:FacultyGridView ID="FacultyGridView1" runat="server" />
<asp:Repeater ID="Repeater1" runat="server">
<ItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" Text='<%# Eval("HeaderText") %>' Checked='<%# Bind("Visible") %>' AutoPostBack=true/></ItemTemplate>
</asp:Repeater>
Code-behind snippet in Search.aspx.cs:
protected void Page_Load(object sender, EventArgs e)
{
Repeater1.DataSource = FacultyGridView1.GridView.Columns;
Repeater1.DataBind();
}
To be clear, the GridView is exposed as a public property of a user control named FacultyGridView. Relevant snippet of FacultyGridView.ascx:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
AllowPaging="True" AllowSorting="True" PageSize="25">
<PagerSettings Mode="NumericFirstLast" Position="TopAndBottom" />
<Columns>
<asp:BoundField DataField="Name" HeaderText="Name" ReadOnly="True" SortExpression="Name" />
<asp:TemplateField HeaderText="University" SortExpression="UniversityID">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Eval("University.Name") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Division">
<ItemTemplate>
<asp:Repeater ID="Repeater1" runat="server" DataSource='<%# Eval("DivisionMemberships") %>'>
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Eval("Division.Name") %>'></asp:Label>
</ItemTemplate>
</asp:Repeater>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="Title" HeaderText="Title" ReadOnly="True" SortExpression="Title" />
<asp:TemplateField HeaderText="Research Type">
<ItemTemplate>
<asp:Repeater ID="Repeater1" runat="server" DataSource='<%# Eval("ResearchTypeMappings") %>'>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Eval("ResearchType.Name") %>'></asp:Label>
</ItemTemplate>
</asp:Repeater>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="Expertise" HeaderText="Expertise" ReadOnly="True" SortExpression="Expertise" />
<asp:HyperLinkField DataNavigateUrlFields="Website" DataTextField="Website" HeaderText="Website"
SortExpression="Website" />
<asp:BoundField DataField="Phone" HeaderText="Phone" ReadOnly="True" SortExpression="Phone" />
<asp:TemplateField HeaderText="Email Address" SortExpression="EmailAddress">
<ItemTemplate>
<asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl='<%# Eval("EmailAddress", "mailto:{0}") %>'
Text='<%# Eval("EmailAddress") %>'></asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Finally, I should mention that the GridView is bound by a Button on the page, but I am not getting updates to the Visible property whether I play with the checkboxes before or after databinding. Furthermore, I have not seen my desired behavior when binding the repeater only on the first Page_Load() using if(!IsPostBack), nor by not using Checkbox.AutoPostback true or false. Any clue as to what I'm doing wrong? I expect it to be something simple, but I'm a bit green here.
As a note: I know how to do this easily with events, but I want to do it with databinding as a learning exercise.
Probably because every time the grid is bound to the data, the column & settings are recreated (with-out your changes).