GridViewRow Column visible true,false - asp.net

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>

Related

How to multiply two columns and display that in another column in a gridview using vb.net

Here is the gridview part. Somebody please help me to find the answer. I want to multiply Count and Price and display the sum in Amount.
<asp:TemplateField HeaderText="Price">
<ItemTemplate>
<asp:Label ID="lblCardPrice" runat="server" Text='<%# Bind("Price")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Count" >
<ItemTemplate>
<asp:Label ID="lblCount" runat="server" Text='<%# Bind("Count")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Amount">
<ItemTemplate>
<asp:Label ID="lblAmount" runat="server" Text='<%# Eval("(Amount)")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
try this:
<asp:TemplateField HeaderText="Amount" >
<ItemTemplate >
<asp:Label ID="lblAmount" runat="server" Text='<%# Convert.ToInt32(Eval("Count"))*Convert.ToInt32(Eval("Price"))%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>

anyone can tell me how to compare the var

i want to compare this two value on grid view but when i using .text it will occur error?
the client side code look like this
<asp:TemplateField >
<ItemTemplate>
<asp:Label ID="lblremainqty" runat="server" Text='<%# Eval("qtycart") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Error" ItemStyle-HorizontalAlign="Center">
<ItemTemplate>
<asp:Label ID="lblError" runat="server" Text=''></asp:Label>
</ItemTemplate>
</asp:TemplateField>
Try use this
int.Parse(Qty.Text) > int.Parse(RemainQty.Text)
or if it is not int value use float or decimal Parse etc. I hope it will help.
PS
Qty.ToString() will return you name of type Label.
Operator >(Greater than) is only used to compare Integer Value Not
String Value. If still you need to use that operation Then you can
convert into Int and Use that operation
You can use :
Convert.ToInt32(Qty.Text) > Convert.ToInt32(RemainQty.Text)
the client side code look like this
<asp:TemplateField HeaderText="Quantity" ItemStyle-HorizontalAlign="Center">
<ItemTemplate>
<asp:Label ID="lblQty" runat="server" Text='<%# Eval("Quantity") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField >
<ItemTemplate>
<asp:Label ID="lblremainqty" runat="server" Text='<%# Eval("qtycart") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Error" ItemStyle-HorizontalAlign="Center">
<ItemTemplate>
<asp:Label ID="lblError" runat="server" Text=''></asp:Label>
</ItemTemplate>
</asp:TemplateField>

How to insert comma in the value which is present in Hyperlink column in gridview

Here is my markup:
<asp:TemplateField HeaderText="Loss" SortExpression="Loss"
HeaderStyle-CssClass="StrongText" HeaderStyle-Font-Bold="true"
HeaderStyle-Font-Size="Medium" HeaderStyle-ForeColor="Blue"
HeaderStyle-Font-Underline="true" HeaderStyle-VerticalAlign="Bottom"
ItemStyle-Width="3%" ItemStyle-HorizontalAlign="Left"
ItemStyle-VerticalAlign="Bottom" HeaderStyle-HorizontalAlign="Left">
<ItemTemplate>
<asp:HyperLink runat="server" ID="HyperLink8"
NavigateUrl='<%#"db_LossofPay.aspx?UserID="+ Eval("User ID")%>'
Text='<%# Eval("Loss","{0}")%>'
ForeColor="BLUE" Target="_blank"></asp:HyperLink>
<asp:Label ID="lblLoss" runat="server"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
Could you please help me with this? In BoundField I wrote DataFormatString="{0:N2}" which works fine, but I got stuck when using in hyperlink (template field)
Try with this:
<asp:HyperLink runat="server" ID="HyperLink8"
NavigateUrl='<%#"db_LossofPay.aspx?UserID="+ Eval("User ID")%>'
Text='<%# Eval("Loss","{0:#,##0.00}") %>' ForeColor="BLUE" Target="_blank" />
Try
Text='<%# String.Format("{0:N2}", DataBinder.Eval(Container.DataItem, "Loss"))%>'

Require Two Header Row in Grid View

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

Simple databinding to gridview columns

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).

Resources