Why do the gridview still appears even if the datasource is empty? - asp.net

<asp:GridView ID="gv_Info" runat="server" Width="1240px" BorderWidth="1px"
CellPadding="4" BorderColor="Gray" AlternatingItemStyle-BackColor="#f6f6eb"
BackColor="#F3372A" AutoGenerateColumns="False" ShowFooter="false">
<AlternatingRowStyle BackColor="White" Wrap="false" />
<Columns>
<asp:TemplateField HeaderText="Select">
<ItemTemplate>
<asp:CheckBox ID="chkSelectAsset" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="d" HeaderText="ATag">
<HeaderStyle HorizontalAlign="Center" />
<ItemStyle HorizontalAlign="Center" />
</asp:BoundField>
<asp:BoundField DataField="c" HeaderText="Description">
<HeaderStyle />
<ItemStyle HorizontalAlign="Center" />
</asp:BoundField>
<asp:BoundField DataField="a" HeaderText="AType">
<HeaderStyle />
<ItemStyle HorizontalAlign="Center" />
</asp:BoundField>
<asp:BoundField DataField="q" HeaderText="ID">
<HeaderStyle />
<ItemStyle HorizontalAlign="Center" />
</asp:BoundField>
</Columns>
</asp:GridView>
gv_InfoAssetsByParams.DataSource = ArrayAssets.ToList()
gv_InfoAssetsByParams.DataBind()
but when ArrayAssets is empty I want to make the gridview empty. SO I do this,
If ArrayAssets.Length > 0 Then
gv_Info.DataSource = ArrayAssets.ToList()
gv_Info.DataBind()
Else
gv_Info.DataSource = ArrayAssets.ToList()
gv_Info.DataBind()
End If
But it still appears even if i assign Nothing to the data source. How do I make it empty with headers?

ASP has a property called ShowHeader when empty. (http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.showheaderwhenempty.aspx)
Set ShowHeaderWhenEmpty="false", that should work.

After looking at your code, you are repeating the same thing in your IF statement.
I tried to replicate your problem on my end, and indeed if the datasource is empty the grid is not visible. I think you should look at the properties of your gridview and go from there. As well, I recommend using a repeater for more control of the visibility and layout when displaying data.

Related

ASP Net Gridview error Two components with the same id can't be added to the application

I have a gridview that is built by unioning several tables. Each table has a primary key called Pkey. My Problem is that the gridview errors out when it runs into the same Pkey number when displaying the data.
Here is the query
SELECT Pkey, Status, EffectiveDate, 'Budget Element' as RequestType, DescribeRequest
FROM tblBudgetElement
union
SELECT Pkey, Status, EffectiveDate, 'Expense Element' as RequestType, DescribeRequest
FROM tblExpenseElement
union
SELECT Pkey, Status, EffectiveDate, 'Expense Hl' as RequestType, DescribeRequest
FROM tblExpenseHLevel
I have tried using a simple bound field. And tried creating a hyperlink field in the code behind.
Here is the gridview
<asp:BoundField DataField="Pkey" HeaderText="Pkey">
<HeaderStyle HorizontalAlign="Left" />
<ItemStyle HorizontalAlign="Left" />
</asp:BoundField>
<asp:BoundField DataField="RequestType" HeaderText="Request Type">
<HeaderStyle HorizontalAlign="Left" />
<ItemStyle HorizontalAlign="Left" />
</asp:BoundField>
<asp:BoundField DataField="Status" HeaderText="Status">
<HeaderStyle HorizontalAlign="Left" />
<ItemStyle HorizontalAlign="Left" />
</asp:BoundField>
<asp:BoundField DataField="EffectiveDate" HeaderText="Effective Date" DataFormatString = "{0:d}" >
<HeaderStyle HorizontalAlign="center" />
<ItemStyle HorizontalAlign="center" />
</asp:BoundField>
<asp:BoundField DataField="DescribeRequest" HeaderText="Describe Request">
<HeaderStyle HorizontalAlign="Left" />
<ItemStyle HorizontalAlign="Left" />
</asp:BoundField>
<asp:TemplateField>
<HeaderStyle VerticalAlign="Top" BorderWidth="0"/>
<ItemStyle BorderWidth="0"/>
<ItemTemplate>
<asp:Image runat="server" BorderStyle="None" ID="Image1" ImageUrl="~/Images/MagnifyingClass.gif" />
<cc1:PopupControlExtender ID="PopupControlExtender1" runat="server"
PopupControlID="Panel1"
TargetControlID="Image1"
DynamicContextKey='<%# Eval("Pkey") %>'
DynamicControlID="Panel1"
DynamicServiceMethod="GetRequest" Position="right">
</cc1:PopupControlExtender>
<asp:Panel ID="Panel1" runat="server"></asp:Panel>
</ItemTemplate>
<ItemStyle HorizontalAlign="Center" />
</asp:TemplateField>
</Columns>
<PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#000099" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="Silver" Font-Bold="True" ForeColor="Black" />
<AlternatingRowStyle BackColor="#CCCCCC" />
</asp:GridView>
I don't think the problem is in the GridView definition, but in your SQL query. Without knowing the database structure, it looks like each table will have its own values for PKey, so of course you're going to encounter duplicate values. And I'll bet (though you didn't show that part of your GridView code) you have "PKey" defined as the DataKeyValue for the GridView.
Again without knowing the database structure, I'm guessing the query could be written differently (or the database slightly modified) to retrieve the rows you need, with a unique key for each one.

How can I reduce the overall height of an asp.net gridview control?

I'm trying to reduce the overall height of this gridview so there aren't so many "empty rows" at the bottom. I tried setting the Height attribute. I also tried setting the PagingSize to a smaller number and setting the AllowPaging Attribute=true
The datasource will return a variable number of rows, but the gridview needs to be fixed height. Right now it's taking up a lot more vertical real estate than any max number of HearingInfo would even reach. In other words, there is enough space at the bottom for at least 10 more rows, but there would never ever be 10 more rows.
I haven't worked with ASP.net web controls in a while...
<asp:GridView ID="gvHearingInfo" runat="server" AutoGenerateColumns="False" EnableModelValidation="True"
ShowHeader="False" Width="100%" GridLines="Vertical"
Height="50px">
<Columns>
<asp:BoundField DataField="Authority" HeaderText="Authority"
SortExpression="Authority">
<ItemStyle Width="144px" Wrap="False" HorizontalAlign="Left"
BorderColor="Black" />
</asp:BoundField>
<asp:BoundField DataField="PublicHearing" HeaderText="PublicHearing"
SortExpression="PublicHearing" >
<ItemStyle HorizontalAlign="Left" Width="431px" BorderColor="Black" />
</asp:BoundField>
<asp:BoundField DataField="HearingDate" HeaderText="HearingDate"
SortExpression="HearingDate" >
<ItemStyle HorizontalAlign="Center" Width="93px" BorderColor="Black" />
</asp:BoundField>
<asp:BoundField DataField="HearingTime" HeaderText="HearingTime"
SortExpression="HearingTime" >
<ItemStyle HorizontalAlign="Center" Width="92px" BorderColor="Black" />
</asp:BoundField>
<asp:BoundField DataField="DistrictTelephone" HeaderText="DistrictTelephone"
SortExpression="DistrictTelephone" >
<ItemStyle HorizontalAlign="Center" Width="94px" BorderColor="Black" />
</asp:BoundField>
</Columns>
</asp:GridView>
Results.DataSource = dataView;
Results.DataBind();
foreach(GridViewRow row in this.Results.Rows)
{
if (row.Equals(""))
{
Results.Rows[0].Visible = false;
}
}
or you will have to use the GridView.RowDataBound Event but I think the code should do the work...

Add more columns to GridView

I have a GridView, which is bound to a database table. This GridView shows the some books (price, author information, etc).
I wants to add a column "Add to Cart" to this GridView at the end of each book. I added one column with text "Add to cart". But, on debug, it shows an error as "Add to cart is not present in db."
So please tell me how to append this column to the GridView.
Markup
<asp:GridView ID="GridView1" runat="server" BorderStyle="Double"
BorderWidth="3px" CellPadding="4" GridLines="Horizontal" Height="260px"
Width="661px">
<FooterStyle BackColor="White" ForeColor="#333333" />
<HeaderStyle BackColor="#336666" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#336666" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="White" ForeColor="#333333" />
<SelectedRowStyle BackColor="#339966" Font-Bold="True" ForeColor="White" />
<SortedAscendingCellStyle BackColor="#F7F7F7" />
<SortedAscendingHeaderStyle BackColor="#487575" />
<SortedDescendingCellStyle BackColor="#E5E5E5" />
<SortedDescendingHeaderStyle BackColor="#275353" />
<Columns>
<asp:ButtonField Text="Add To Cart">
<FooterStyle HorizontalAlign="Right" />
<HeaderStyle HorizontalAlign="Right" />
<ItemStyle HorizontalAlign="Left" />
</asp:ButtonField>
</Columns>
</asp:GridView>
Code
public partial class WebForm9 : System.Web.UI.Page<p>
{
buybl obj = new buybl();
protected void Page_Load(object sender, EventArgs e)
{
obj.dept = "IT";
GridView1.DataSource = obj.select();
GridView1.DataBind();
}
}
Without your markup, it's hard to say. But, it sounds like you may have modelled the "Add To Cart" column after your columns that are bound to the database. You probably have your databound columns set up as BoundFields like this:
<Columns>
<asp:BoundField DataField="Title" HeaderText="Book Title" />
<asp:BoundField DataField="Author" HeaderText="Book Author" />
<asp:BoundField DataField="Price" HeaderText="Book Price" />
</Columns>
Your "Add To Cart" column will not be databound. If you just want to add a column that has a static button, you can add a CommandField to your GridView, like this:
<Columns>
<asp:BoundField DataField="Title" HeaderText="Book Title" />
<asp:BoundField DataField="Author" HeaderText="Book Author" />
<asp:BoundField DataField="Price" HeaderText="Book Price" />
<asp:CommandField SelectText="Add To Cart" ShowSelectButton="True" />
</Columns>
Or you can just add AutoGenerateSelectButton="True" to your GridView markup. Then you can handle the adding of items to your cart in the RowCommand or SelectedIndexChanged events that are fired when you click the Select button
you can add column like this in gridview
<asp:TemplateField >
<HeaderTemplate >
<asp:Button ID="ButtonName" runat="server" Text="Add to Cart" > </asp:Button>
</HeaderTemplate>
</asp:TemplateField>
Use the TemplateField column type -- unlimited customization:
http://msdn.microsoft.com/en-us/library/bb288032.aspx
<asp:TemplateField HeaderText="FirstName" SortExpression="FirstName">
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<% #Bind("FirstName") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<% #Bind("FirstName") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>

Question about ASP.NET gridviews

I have a gridview. Is it possible to modify the gridview so that I can have multiple rows in the column headers? For example, the following code generates the table in the picture below.
<asp:GridView ID="productsGridView" Runat="server" DataSourceID="productDataSource" AutoGenerateColumns="False" AllowSorting="True" BorderWidth="2px"
BackColor="White" GridLines="None" CellPadding="3" CellSpacing="1" BorderStyle="Ridge" BorderColor="White" AllowPaging="True">
<FooterStyle ForeColor="Black" BackColor="#C6C3C6"></FooterStyle>
<PagerStyle ForeColor="Black" HorizontalAlign="Right" BackColor="#C6C3C6"></PagerStyle>
<HeaderStyle ForeColor="#E7E7FF" Font-Bold="True" BackColor="#4A3C8C"></HeaderStyle>
<Columns>
<asp:BoundField HeaderText="Product" DataField="ProductName" SortExpression="ProductName"></asp:BoundField>
<asp:BoundField HeaderText="Unit Price" DataField="UnitPrice" SortExpression="UnitPrice" DataFormatString="{0:c}">
<ItemStyle HorizontalAlign="Right"></ItemStyle>
</asp:BoundField>
<asp:BoundField HeaderText="Units In Stock" DataField="UnitsInStock" SortExpression="UnitsInStock" DataFormatString="{0:d}">
<ItemStyle HorizontalAlign="Right"></ItemStyle>
</asp:BoundField>
<asp:BoundField HeaderText="Quantity Per Unit" DataField="QuantityPerUnit"></asp:BoundField>
</Columns>
<SelectedRowStyle ForeColor="White" Font-Bold="True" BackColor="#9471DE"></SelectedRowStyle>
<RowStyle ForeColor="Black" BackColor="#DEDFDE"></RowStyle>
</asp:GridView>
This table only has one row in the column header. What I am looking for is something like this:
Any Ideas on how I can achieve this? Is this even possible?
If you use a TemplateField instead, you can control the header template as well, which can be custom ASPX markup. The downside is that you have to display the data manually with Labels instead of using the simpler BoundField properties. However, this also allows you to layout the data in a custom layout to fit with the heading:
<Columns>
<asp:TemplateField>
<HeaderTemplate>
Weight<br />
Date | Time<br />
Product
</HeaderTemplate>
<ItemTemplate>
<asp:Label runat="server" Text='<%#Eval("Weight") %>'></asp:Label><br />
<asp:Label runat="server" Text='<%#Eval("Date") %>'></asp:Label> |
<asp:Label runat="server" Text='<%#Eval("Time") %>'></asp:Label><br />
<asp:Label runat="server" Text='<%#Eval("Product") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>

Gridview Column Width in ASP.NET 2.0

How do you control the column width in a gridview control in ASP.NET 2.0?
You can use the HeaderStyle-Width, ItemStyle-Width or FooterStyle-Width properties. These can be applied to all the columns or per-column basis.
<asp:GridView ID="GridView1" runat="server">
<HeaderStyle Width="10%" />
<RowStyle Width="10%" />
<FooterStyle Width="10%" />
<Columns>
<asp:BoundField HeaderText="Name" DataField="LastName"
HeaderStyle-Width="10%" ItemStyle-Width="10%"
FooterStyle-Width="10%" />
</Columns>
</asp:GridView>
I do it using the header style for the column:
<asp:BoundField HeaderText="Name" DataField="LastName">
<HeaderStyle Width="20em" />
</asp:BoundField>
Here's the C# code to do it programatically:
columnName.ItemStyle.Width = Unit.Percentage(someDouble);
Gridview.Columns[1].ItemStyle.Width = 100;
This will set the with in pixel.

Resources