I want to hide a column in a gridview. I use the following code;
dgvTekleme.Columns[1].Visible = false;
but this does not work. (may be it does not work because of using that column in the code)
Is there any solution to hide a column in code-behind
you can do this manually.....
goto asp:gridview tag and in gridview tag set autogeneratecolumn="false"
if you don't want to display some column just don't write that column....
for example if you don't want to display prodId column just erase that line....
and write manually for displaying data like!!!!!!!!!!
<asp:GridView ID="gridview1" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:BoundField HeaderText="ProdID" DataField="prodid" ReadOnly="true" />
<asp:BoundField HeaderText="ProdName" DataField="ProdName" />
<asp:BoundField HeaderText="Quantity" DataField="quantity" />
<asp:BoundField HeaderText="SupplierID" DataField="SupplierId" />
<asp:BoundField HeaderText="StockLvl" DataField="stocklevel" />
<asp:BoundField HeaderText="MinStockLvl" DataField="minstocklevel" />
<asp:BoundField HeaderText="CostPrice" DataField="costprice" />
<asp:BoundField HeaderText="SalesPrice" DataField="saleprice" />
<asp:BoundField HeaderText="Loc" DataField="location" />
<asp:BoundField HeaderText="ProdCode" DataField="prodtypecode" />
<asp:CommandField ShowEditButton="true" ShowDeleteButton="true" />
</Columns>
</asp:GridView>
or you can check this link:
How to hide columns in an ASP.NET GridView with auto-generated columns?
Related
I have a grid view which display records by reading the data from a local database. I want to display a button called view with each record displayed. Simply meaning in each row there should be an asp.net button called view Details.
Here is my grid view code:
<asp:GridView ID="ItemView" runat="server" AllowPaging="True"
AutoGenerateColumns="False" CssClass="list" DataKeyNames="ItemID"
DataSourceID="ADDataSource" GridLines="Both" Width="215px">
<Columns>
<asp:BoundField DataField="itemID" HeaderText="Item ID" />
<asp:BoundField DataField="Item Name" HeaderText="Item Name" />
<asp:BoundField DataField="Item Brand" HeaderText="Item Brand" />
<asp:CommandField ShowDeleteButton="True" ShowEditButton="True" />
</Columns>
</asp:GridView>
I have added a button to delete records and edit records through common field property, but how do i add my own button called view to display in each row as the other 2 buttons.
Thank you.
I managed do it myself.
<asp:BoundField DataField="topic" HeaderText="Topic Name" />
<asp:BoundField DataField="Brand" HeaderText="Vehicle Brand" />
<asp:BoundField DataField="Model" HeaderText="Vehicle Model" />
<asp:BoundField DataField="Year" HeaderText="Vehicle Year" />
<asp:CommandField ShowDeleteButton="True" ShowEditButton="True" />
<asp:TemplateField ShowHeader="False">
<ItemTemplate>
<asp:Button ID="viewDetails" runat="server" Text="View" />
</ItemTemplate>
</asp:TemplateField>
I have a button in my rows in a gridview and when clicking the button some code behind handles the button click. I want to pass a commandname paramater through with each button click, which is really just an order id. I'm wondering how I can set the commmand name for each button.
<Columns>
<asp:BoundField DataField="status" HeaderText="Status" HeaderStyle-HorizontalAlign="Left" SortExpression="status" />
<asp:BoundField DataField="orderid" HeaderText="Order ID" HeaderStyle-HorizontalAlign="Left" SortExpression="orderid" />
<asp:BoundField DataField="customer" HeaderText="Customer" HeaderStyle-HorizontalAlign="Left" SortExpression="customer" />
<asp:BoundField DataField="email" HeaderText="Email" HeaderStyle-HorizontalAlign="Left" SortExpression="email" />
<asp:BoundField DataField="createddate" HeaderText="Created Date" HeaderStyle-HorizontalAlign="Left" SortExpression="createddate" />
<asp:BoundField DataField="promocode" HeaderText="Promo Code" HeaderStyle-HorizontalAlign="Left" SortExpression="promocode" />
<asp:BoundField DataField="grandtotal" HeaderText="Grand Total" HeaderStyle-HorizontalAlign="Left" SortExpression="grandtotal" DataFormatString="{0:C}"/>
<asp:ButtonField Text="View" CommandName="6182" ButtonType="Button" />
</Columns>
As you can see abve the last column is the button that appears on each row and I've currently set it to 6182 - just as an example. What I really want to set it to is the orderid value...
How can I do that?
Thanks,
I'd probably go for something similar to this:
<asp:ButtonField Text="View" CommandName='<%# Eval("orderid") %>' ButtonType="Button" />
That should insert the orderid for the bound row into that field. If it doesn't work, convert the column into a TemplateField, add a button and do the same. Report back :-)
I have a standard grid view, which contains a number of bounded fields and template fields, out of these I would like to make 4 columns invisible.
I know the css for this is visibility:hidden; but I do not know how to apply it to just these 4 columns, I have tried using the ControlStyle-CssClass property but it seems to apply the css to the whole table.
Is there a way to apply this css only to these 4 columns, also the plan is after I manage this is to write some Javascript for a button to switch the visibility on and off, so if you could please use some CSS/tags etc which can be easily changed client side it would be a huge plus.
A example:
<asp:GridView ID="gwFoo" runat="server" AutoGenerateColumns="False" DataKeyNames="Id"
Width="631px" OnRowDataBound="gwFoo_RowDataBound"
CssClass="customerDataTable" AllowSorting="True">
<Columns>
<asp:BoundField DataField="type" HeaderText="Type" SortExpression="type" />
<asp:TemplateField HeaderText="Number" SortExpression="id">
<ItemTemplate>
<asp:HyperLink ID="hlNumber" runat="server">[hlNumber]</asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="product" HeaderText="Product" SortExpression="product" />
<asp:BoundField DataField="startDate" HeaderText="Start Date" SortExpression="startDate" />
<asp:BoundField DataField="endDate" HeaderText="End Date" SortExpression="endDate" />
<asp:BoundField DataField="isActive" HeaderText="Is Active ?" SortExpression="isActive" />
<asp:BoundField DataField="markedForActivation" HeaderText="Marked for Activation ?" SortExpression="markedForActivation" />
<asp:BoundField DataField="status" HeaderText="Status" SortExpression="status" />
<asp:TemplateField HeaderText="Parent Order" SortExpression="Foo">
<ItemTemplate>
<asp:HyperLink ID="hlFoo" runat="server">[hlFoo]</asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Parent Document" SortExpression="Foo">
<ItemTemplate>
<asp:HyperLink ID="hlFoo" runat="server">[hlFoo]</asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="createdDate" HeaderText="Created Date" <itemstyle Visible="false"></itemstyle> SortExpression="createdDate" />
<asp:BoundField DataField="createdBy" HeaderText="Created By" SortExpression="createdBy" />
<asp:BoundField DataField="lastUpdated" HeaderText="Last Updated" SortExpression="lastUpdated" />
<asp:BoundField DataField="lastUpdatedBy" HeaderText="Last Updated By" SortExpression="lastUpdatedBy" />
</Columns>
</asp:GridView>
give the columns a class and set the class css to visiblity:hidden;
Using Visible="false" will works
ie
<asp:BoundField Visible="false" DataField="status" HeaderText="Status" SortExpression="status" />
I solved the problem, in a way in which I can also switch the invissibility on and off from a button.
First I refactored the bounded fields I wanted to hide in this manner:
<asp:BoundField DataField="createdBy" HeaderText="Created By" SortExpression="createdBy" >
<ItemStyle CssClass="fooClass"></ItemStyle>
</asp:BoundField>
The class fooClass is a empty css class, I used it only so that all the <td> </td> elements which are generated by the bounded field share a common class, thus allowing the jquery script I wrote bellow to hide or show them.
<script> $(".fooClass").hide(); </script>
// or
<script> $(".fooClass").show(); </script>
How to set the Boolean value into GridView Check boxes in C#?
This is my code simply
static List<bool> bh = new List<bool>(6);
dt.Columns.Add("Hold");
dr["Hold"] = rem[x].Hold;
bh.Add(Convert.Toboolean((string)dr["Hold"]));
gridview codes
<Columns>
<asp:BoundField DataField="RemitID" HeaderText="RemitID" />
<asp:BoundField DataField="TransferedDate" HeaderText="TransferedDate" />
<asp:BoundField DataField="ValueDate" HeaderText="ValueDate" />
<asp:BoundField DataField="RemitterName" HeaderText="RemitterName" />
<asp:BoundField DataField="BenificiaryName" HeaderText="BenificiaryName" />
<asp:BoundField DataField="AccountNo" HeaderText="AccountNo" />
<asp:BoundField DataField="Reference" HeaderText="Reference" />
<asp:CheckBoxField DataField="Hold" HeaderText="Hold" />
<asp:CheckBoxField DataField="Reject" HeaderText="Reject" />
<asp:CheckBoxField DataField="Delete" HeaderText="Delete" />
</Columns>
here i am using List<> because have to add set of datas. No runtime errors are occur but checkboxes not showing the values.. Thank you !
i use this code .. but when the hyperlinks are longer then field size the gridview expands verticaly. i want the link below as like a paragraph . give me a solution
<asp:gridview id="titlesGrid" runat="server"
datasourceid="titles"
width=90% cellpadding=5 font-size="8pt"
autogeneratecolumns=false
headerstyle-backcolor="maroon"
headerstyle-forecolor="khaki"
headerstyle-font-bold
rowstyle-verticalalign="top">
<columns>
<asp:hyperlinkfield headertext="Title"
datatextfield="title"
datanavigateurlformatstring="title_details.aspx?titleid={0}"
datanavigateurlfields="title_id" />
<asp:boundfield headertext="Title ID"
datafield="title_id" />
<asp:boundfield headertext="Category"
datafield="type" />
<asp:boundfield headertext="Pub ID"
datafield="pub_id" />
<asp:boundfield headertext="Price"
datafield="price"
htmlencode=false
dataformatstring="{0:n2}"
itemstyle-horizontalalign="right" />
</columns>
</asp:gridview>
Have you tried using <ItemStyle Wrap="True" /> for <asp:hyperlinkfield /> ?