Data Displayed Twice In Gridview (ASP.NET) - asp.net

I'm trying to make a page where information from the database are displayed on a page. For this, I'm using a Gridview control. The data displays fine, but it displays the same information twice. So basically, two tables are drawn by ASP and placed side by side.
Heres the code I'm using:
<asp:GridView ID="PackagesGV" runat="server" Width="520px">
<Columns>
<asp:BoundField DataField="ID" HeaderText="Package ID"/>
<asp:BoundField DataField="PackageName" HeaderText="Package Name"/>
<asp:BoundField DataField="PackageText" HeaderText="Package Text"/>
<asp:BoundField DataField="PackageImageID" HeaderText="Package Image"/>
<asp:BoundField DataField="PageID" HeaderText="Page ID"/>
</Columns>
</asp:GridView>
Also, the SQL Stored Procedure is pulling all of the fields required by the Gridview. The SQL is basically
"SELECT [ID], [PackageName], [PackageText], [PackageImageID], [PageID] FROM [Packages]"
So I'm not requesting the information twice using the Stored Procedure.
I've started ASP.NET in July, so I apologise now if this is something really simple.
Thanks!
Michael

You need to either set the GridView.AutoGenerateColumns Property to false or not set up the columns.
If you choose the former method your grid definition will become:
<asp:GridView ID="PackagesGV" runat="server" Width="520px" AutoGenerateColumns="False">

Related

in ASPX/VB using a gridview, how do I allow a checkbox on the row to be checked/unchecked without hitting some edit button?

I'm on an extreme time crunch, and its been years since doing any of this. I'm trying to take an old project, and enable a checkbox within the gridview.
My aspx gridview is:
<asp:GridView ID="grdReg" runat="server" AutoGenerateColumns="False" AllowSorting="True" EnableSortingAndPagingCallbacks="True" CssClass="table table-hover" GridLines="None">
<HeaderStyle CssClass="thead-dark" />
<Columns>
<asp:BoundField HeaderText="" DataField="pkID" ReadOnly="True"></asp:BoundField>
<asp:BoundField HeaderText="YourID" DataField="yourID" ReadOnly="True"></asp:BoundField>
<asp:BoundField HeaderText="Name" DataField="fullname" ReadOnly="True"></asp:BoundField>
<asp:BoundField HeaderText="Program" DataField="program" ReadOnly="True"></asp:BoundField>
<asp:BoundField HeaderText="Status" DataField="eStatus" ReadOnly="True"></asp:BoundField>
<asp:BoundField HeaderText="Recen" DataField="recent" ReadOnly="True"></asp:BoundField>
<asp:BoundField HeaderText="Enabled?" DataField="enabled" ReadOnly="False"></asp:BoundField>
</Columns>
</asp:GridView>
Now - the last column "Enabled?" is bound to a SQL Server bit field. Right now, when you display the aspx page - it shows 'True' or 'False' or blank if null.
Right now, I don't care if the field is a checkbox or shows the True/False - but when you click on it, I want it to toggle from T -> F or checked to unchecked. It's been too long since I've been in VB, and I need a simple way to accomplish this.
This will be a short term form, which is why I'm not terribly worried about "neatness" or polish. I just need it to work, and work quickly. If you give code snippets, PLEASE give them in context with the above code.
I finally solved by just clicking on the row to toggle the value. Not elegant, but works for now

Confirmation on GridView SelectedIndexChanging or SelectedIndexChanged

I have a GridView on an ASP page that has one bound field showing. Following is the code for this gridview.
<asp:GridView ID="GridView1" OnSelectedIndexChanging="confirmation" runat="server" AllowPaging="True" AutoGenerateColumns="False">
<Columns>
<asp:BoundField DataField="ImportFileDescription" HeaderText="File Description" SortExpression="ImportFileDescription" ReadOnly="True" >
</asp:BoundField>
<asp:CommandField ShowSelectButton="True" SelectText="Import" ItemStyle-HorizontalAlign="Right" ButtonType="Button" />
</Columns>
</asp:GridView>
When a record or row is selected, I need to have a confirmation box pop up, and then a sub will be executed on the server. Fumbling my way through this, I had thought that using the CommandField Select Button would work, but I am having issues. I am not sold on using the CommandField to accomplish this, I would be open to whatever method would be easiest. I could also have a checkbox or something next to each row and then only one "Import" button at the bottom that would import all that are selected. It really doesn't matter as long as I have: (1) the ability to choose which records will be imported and (2) the ability to create the confirmation box.
Any help is greatly appreciated.

ASP.net Gridview, best way to represent many to many?

I'm working on a product using ASP.net / vb.net.
What I need to do is represent a matrix of locations vs products and select which products are available in which locations by using a grid of checkboxes.
The products are provided via a table (ProductID, ProductName) as are locations (LocationID, LocationName) and I have a Many to Many table (ID, ProductID, LocationID). The issue I'm stuck with is how to create a gridview with dynamic columns and rows based on separate data sources.
Am I being really stupid here? (actually, don't answer that!)
Any help would be greatly appreciated.
Thanks,
Steve
There are two ways to do this :
Use a repeater control
Pivot the table at database end and bind it to gridview normally.
You can use nested Gridviews:
<asp:GridView ID="Products" runat="server">
<Columns>
<asp:BoundField DataField="ProductId" />
<asp:BoundField DataField="ProductName" />
<asp:TemplateField>
<ItemTemplate>
<asp:GridView ID="Locations" runat="server">
<Columns>
<asp:BoundField DataField="LocationId" />
<asp:BoundField DataField="LocationName" />
</Columns>
</asp:GridView>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
The Products gridview has a TemplateField with a gridview Locations inside.

EmptyDataTemplate still shows message even if records found

I was wondering if any one could shed some light on property in the GridView. As far as I know, if no records are found, the text message inside
will be displayed, when the GridView control is bound to data. Actually, it’s working fine, but it works even if the record is found, too.
Currently, the datagridview is like this:
<asp:GridView ID="gvNomineeSearchResults" runat="server" DataKeyNames="ind_cst_key"
AutoGenerateColumns="false" AutoGenerateSelectButton="true"
OnSelectedIndexChanging="gvNomineeSearchResults_SelectedIndexChanged" OnRowCreated="gvNomineeSearchResults_RowCreated"
CssClass="selectedItems nominee" Width="100%" Caption="Search Results">
<Columns>
<asp:BoundField DataField="lastname" HeaderText="Last Name" NullDisplayText=" " />
<asp:BoundField DataField="firstname" HeaderText="First Name" NullDisplayText=" " />
</Columns>
<EmptyDataTemplate>
<p>Your search did not return any active AIAA Associate Fellows or did not find an email address on file for the individual you are searching for.</p>
</EmptyDataTemplate>
Any idea why the message is still showing even if records are found ?
Just Check the condition its correctly working or not.
Check the condition Gridview while Binding.
if(Dataset1.Items.count>0)
{
//Put your codes Here
}
Before Binding a Data to the Gridview Clear the Gridview Items.
Gridview1.Items.Clear();
Hope this may helpful....

GridView will not update underlying data source

So I'm been pounding on this problem all day. I've got a LinqDataSource that points to my model and a GridView that consumes it. When I attempt to do an update on the GridView, it does not update the underlying data source. I thought it might have to do with the LinqDataSource, so I added a SqlDataSource and the same thing happens. The aspx is as follows (the code-behind page is empty):
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="Data Source=devsql32;Initial Catalog=Steam;Persist Security Info=True;"
ProviderName="System.Data.SqlClient"
SelectCommand="SELECT [LangID], [Code], [Name] FROM [Languages]" UpdateCommand="UPDATE [Languages] SET [Code]=#Code WHERE [LangID]=#LangId">
</asp:SqlDataSource>
<asp:GridView ID="_languageGridView" runat="server" AllowPaging="True"
AllowSorting="True" AutoGenerateColumns="False" DataKeyNames="LangId"
DataSourceID="SqlDataSource1">
<Columns>
<asp:CommandField ShowDeleteButton="True" ShowEditButton="True" />
<asp:BoundField DataField="LangId" HeaderText="Id" ReadOnly="True" />
<asp:BoundField DataField="Code" HeaderText="Code" />
<asp:BoundField DataField="Name" HeaderText="Name" />
</Columns>
</asp:GridView>
<asp:LinqDataSource ID="_languageDataSource" ContextTypeName="GeneseeSurvey.SteamDatabaseDataContext" runat="server" TableName="Languages" EnableInsert="True" EnableUpdate="true" EnableDelete="true">
</asp:LinqDataSource>
What in the world am I missing here? This problem is driving me insane.
You are missing the <UpdateParameters> sections of your DataSources.
LinqDataSource.UpdateParameters
SqlDataSource.UpdateParameters
It turns out that we had a DataBind() call in the Page_Load of the master page of the aspx file that was probably causing the state of the GridView to get tossed out on every page load.
As a note - update parameters for a LINQ query are not required unless you want to set them some non-null default.
This is a total shot in the dark since I haven't used ASP at all.
I've been just learning XAML and WPF, which appears to be very similar to what you've posted above and I know that for some UI controls you need to specify the binding mode to two-way in order to get updates in both directions.

Resources