Gridview Autosort Without allowing users to sort - asp.net

Hello everyone here is the problem, and I hope you can help me
I use a Gridview in my asp.net page to show the data from my sql database.
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataSourceID="SqlDataSource1">
<Columns>
<asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name"></asp:BoundField>
<asp:BoundField DataField="Email" HeaderText="Email" SortExpression="Email"></asp:BoundField>
<asp:BoundField DataField="College" HeaderText="College" SortExpression="College"></asp:BoundField>
</Columns>
</asp:GridView>
The gridview itself is working fine but i want to auto sort by College but I DONT want the users to be able to sort anything on click. Hope you can help thanks.

In your Data-Source SqlDataSource1 add order by College.
And change the bound filed
<asp:BoundField DataField="Name" HeaderText="Name">
......
Set property of gridview AllowSorting = 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

ASP.Net - Sorting a GridView without postback (bound to a generic list)

I'm trying to sort the columns for a GridView in ASP.Net, ideally without refreshing the page.
My data is compiled into a generic list on the server and bound to the GridView in the code-behind.
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
BackColor="White" CellPadding="3" GridLines="Vertical" AllowSorting="True"
CssClass="GridViewStyle" EnableSortingAndPagingCallbacks="True"
AllowPaging="True">
<Columns>
<asp:BoundField DataField="ID" HeaderText="ID" SortExpression="ID">
</asp:BoundField>
<asp:BoundField DataField="Title" HeaderText="Title" SortExpression="Title">
</asp:BoundField>
<asp:BoundField DataField="Customer" HeaderText="Customer" SortExpression="Customer">
</asp:BoundField>
</Columns>
When I try to click any of the column headers, nothing happens.
Is there any way to accomplish this without refreshing the page?
No, there is no way to accomplish this with a Gridview alone without causing a postback.
You can sort it on the client side with JavaScript (perhaps by abandoning GridView and instead using a Repeater combined with jQuery DataTables)
Or you can use an UpdatePanel or iframe to mask the postback. Those have their own complications and are probably not the best approach however. I personally absolutely hate using either of them and recommend you steer clear.

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.

Data Displayed Twice In Gridview (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">

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