I have given an access to stored procedure, which i'm not able to edit.
This Stored Procedure returns a Table with 2 Column, what I did is set a GridView's DataSource using SQLDataSource in this stored procedure. but I want this GridView to Sort an specific column to descending whenever this GridView Loads.
<asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="False"
DataSourceID="SqlDataSource2" ForeColor="Black"
Width="58%" Height="125px" AllowPaging="True" AllowSorting="True"
PageSize="5" >
<Columns>
<asp:BoundField DataField="DateOccur" HeaderText="Login Date"
SortExpression="DateOccur" />
<asp:BoundField DataField="TotalMinutesPlayed" HeaderText="Total Minutes"
SortExpression="TotalMinutesPlayed" />
</Columns>
<AlternatingRowStyle BackColor="#EFEFEF" />
</asp:GridView>
Thanks!~
You could also use the Gridview.Sort method
See Here.
If you can't sort it by editing the SQL itself, you can sort it programmatically by binding a DataView object to the datasource, specifying a Sort expression for the DataView, and then binding the DataGrid to the DataView object.
For a walkthrough of some example code, see here.
I was using a slightly modified version of the example code I found on MSDN.
Gridview Sort Example on MSDN
By combining the example I found with the SortExpression property I can sort on any column even using proprietary sorts my specification called for.
Related
I want to delete a row from an aspnet table, that have columnButton with [delete] button.
This is my code:
<asp:GridView ID="GridInduccionPersona" runat="server" EnablePartialRendering="true" AutoGenerateColumns="False" DataKeyNames="RUT" CellPadding="4" ForeColor="#333333" GridLines="None" DeleteMethod="GridInduccionPersona_RowDeleting">
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:BoundField DataField="RUT" HeaderText="Rut" InsertVisible="False"
.
.
.
**<asp:ButtonField ButtonType="Button" CommandName="Delete" HeaderText="Eliminar" ShowHeader="True" Text="Eliminar" />**
</Columns>
</asp:GridView>
but when I put the page on Chrome; said: The Select operation is not supported unless you specify SelectMethod.
I just want to acces to function in server, and delete from gridview and database.
I dont want to use linq or similar, Im working with stored procedure.
Any idea about that ? is SelectMethod important for delete from my column ? (I supposed that the button call the id, or some similar in gridview to delete)
From what I remember from ASP.net Webforms, the GridView needs the SelectMethod property configured to works properly.
SelectMethod should point to the method that obtains the records.
Without this property, GridView can't list the records, so you can't delete them.
So, set the SelectMethod property to target to the method that retrieves the records.
I need to perform a customized sort of a .NET DataGrid. The UserRole column returns a set of three string values from SQL via a stored procedure: No Access, View, and Update.
<asp:DataGrid ID="UserList" runat="server" AllowPaging="True" AutoGenerateColumns="False" AllowSorting="True" OnDataBinding="UserList_DataBinding">
<Columns>
<asp:BoundColumn DataField="FirstName" HeaderText="First Name"></asp:BoundColumn>
<asp:BoundColumn DataField="LastName" HeaderText="Last Name" SortExpression="LastName ASC, LastName"></asp:BoundColumn>
<asp:BoundColumn DataField="EmailAddress" HeaderText="Email Address"></asp:BoundColumn>
<asp:BoundColumn DataField="UserRole" HeaderText="User Role" SortExpression="UserRole DESC, UserRole"></asp:BoundColumn>
</Columns>
</asp:DataGrid>
I need to be able to sort to display the "Update" value in the UserRole column at the top of the table. As it current sorts in descending order via the SortExpression property, "View" comes before "Update".
Where the above code is legacy code I'm not in a position to be able to full rewrite the code but is there another way to achieve this kind of custom non-alpha sorting?
You should sort your data before binding it to the grid. Depending on how you access the underlying data you could use some sort of OrderBy clause and order DESC. If you simply want "Update" to be first, that will be a little harder.
I am attempting to use a Details view.
This is my DataSource
I am passing it this way.
IEnumerable<DataRow> row = Connection.GetDataTable([sql]).AsEnumerable();
this.dvOrderInformation.DataSource = row;
this.dvOrderInformation.DataBind();
I am binding it like so.
<asp:DetailsView ID="dvOrderInformation" runat="server" Height="50px" Width="100%" AutoGenerateRows="false">
<HeaderTemplate>
Order
</HeaderTemplate>
<FieldHeaderStyle Width="150px" />
<Fields>
<asp:BoundField HeaderText="Order Number:" DataField="OrderID" />
</Fields>
</asp:DetailsView>
When I try this I get the message.
DataBinding: 'System.Data.DataRow' does not contain a property with the name 'OrderID'.
or
A field or property with the name 'OrderID' was not found on the selected data source.
If I bind this directly to a DataGrid it works fine. Any ideas what I am doing wrong here.
You can adjust with
this.dvOrderInformation.DataSource = Connection.GetDataTable([sql]);
this.dvOrderInformation.DataBind();
Nota : it's normal row don't contain column, so he don't find column name
DataField="OrderID" is DataColumn.Name
Found the solution.
this.dvOrderInformation.DataSource = Connection.GetDataSet([sql]);
this.dvOrderInformation.DataBind()
The Key is that I must use a DataSet and not a DataTable.
I'm using a GridView to display some ifnromation in ASP.NET. I need it to simply display 0 in the gridview fields (there's only ever one row returned) if the dataset is empty, plus of course display the headers. How do I do this?
Yes bind a dummy dataset with one row, with all zeros as fields, to the grid with zeros as the field labels. That would be the best way.
Bind your grid and add the EmptyDataTemplate like so:
<asp:GridView ID="gridview1" runat="server" ShowFooter="true">
<Columns>
<asp:BoundField DataField="id" HeaderText="ID field" />
</Columns>
<EmptyDataTemplate>put whatever you want in here</EmptyDataTemplate>
</asp:GridView>
I have a web application that I am working on(ASP.NET 2.0 C#). In it I have a GridView whose data source is an Oracle database. I get the data into the gridview in my codebehind, and don't set the datasource directly.
I wanted to create a hyperlink field (NAME) that takes me to a details page about a specific record. What ends up happening is that it creates the Hyperlink field as well as the regular field that it gets from the datasource, which I don't want. If I remove the field from my SELECT statement, it gives an error saying something like: "NAME" not found in datasource.
How can I eliminate the regular field, and get a hyperlink field instead? I have tried Gridview.Columns.Remove(columnlocation), but that won't work coz the columns don't exist there originally.
Please Help
Thank you.
Instead of removing columns, disable AutoGenerateColumns property of the gridview and set your hyperlink column manually like that :
<asp:gridview id="GridView1"
autogeneratecolumns="false"
runat="server">
<asp:HyperLinkField DataNavigateUrlFields="UserID"
DataNavigateUrlFormatString="UserDetails.aspx?id={0}"
DataTextField="UserName" />
</asp:gridview>
Sounds like you have AutoGenerateColumns property set to TRUE on your grid. This means that a column is generated for EVERY column you return from your query.
If you want to have some custom columns, you should set AutoGenerateColumns="false" and add all the columns to the GirdView as asp:BoundField and your Hyperlink column as asp:TemplateField
Let me know if I'm off the mark with this
here's some code:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False">
<Columns>
<asp:BoundField DataField="Name" />
<asp:BoundField DataField="Whatever" />
<asp:TemplateField>
<ItemTemplate>
<a href='<%# Eval("UserId", "URL_TO_USER?userId={0}") %>'>Details</a>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>