Grid view sorting - asp.net

i am using this code for sorting in grid view but this is not working. please help
<asp:TemplateField SortExpression="FirstName">
<HeaderTemplate>
Name
</HeaderTemplate>
<ItemTemplate>
<%# Eval("Name")%>
</ItemTemplate>
</asp:TemplateField>

You mentioned that you don't see link at the table's header.
It might help you,
Make sure that you set the AllowSorting="true" and created a event to handle it OnSorting="gvActivities_Sorting"
<asp:GridView runat="server" ID="gvActivities" AllowSorting="true"
OnSorting="gvActivities_Sorting">
If you did it you should get a link in the header.
If not post the all gridview decleration

Related

Add TextBox to DataGrid

I have a web page built in .NET where I am a very simple DataGrid with several fields. I would like to have one of those field have its data placed in a TextBox so the user can edit the default description. Is there an easy way to do this by default so the user doesn't have to click an edit button for the row?
Yes, disable AutoGenerateColumns and define your own fields:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:TemplateField>
<HeaderTemplate></HeaderTemplate>
<ItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Eval("Data") %>'></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Updating this data to the database might be more difficult though.

Dynamic Data GridView Pager missing DynamicHyperlink Edit parameters

Edit: Feb 7, 2012 - Turns out the key parameter disappears from the GridView Edit link after a Sort as well, so it doesn't appear to be the Pager after all, but the problem persists... any ideas very welcome.
I have a Asp.Net Dynamic Data app. It uses the standard GridViewPager.aspx in Custom List.aspx which is marked up like this:
<asp:GridView ID="GridView1" runat="server" DataSourceID="GridDataSource" EnablePersistedSelection="True" CssClass="DDGridView" HeaderStyle-CssClass="th" RowStyle-CssClass="td" CellPadding="6" AllowSorting="True" AllowPaging="True" PageSize="3"
OnRowDataBound="GridView_OnRowDataBound" OnRowDeleting="GridView_OnRowDeleting">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:LoginView ID="LoginView1" runat="server">
<RoleGroups>
<asp:RoleGroup Roles="admin">
<ContentTemplate>
<asp:DynamicHyperLink ID="EditLink" runat="server" Action="Edit" Text="edit" />
<asp:LinkButton ID="LinkButton1" runat="server" CommandName="Delete" Text="delete" OnClientClick='return confirm("Are you sure you want to delete this item?");' />
</ContentTemplate>
</asp:RoleGroup>
</RoleGroups>
</asp:LoginView>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<PagerStyle CssClass="DDFooter" />
<PagerTemplate>
<asp:GridViewPager runat="server" />
</PagerTemplate>
<EmptyDataTemplate>
There are currently no items in this table.
</EmptyDataTemplate>
</asp:GridView>
On page 1 of the List all is well. The Edit Link rendered includes ?key=xxxxx and the Edit form opens to the correct record.
Once we page off of Page 1 however, the Edit Link rendered on each row has no 'key' parameter and the Edit form always opens to the first row in the database.
I am at a loss to explain this or even where to look. There is no custom code attached to any event associated with the Edit DynamicHyperlink or the GridviewPager.
Has anyone experienced this or have any suggestions as to what might be the issue?
Found the answer here:
LinkButton CommandArgument is empty when it is inside LoginView in a GridView
Turns out good 'ol Microsoft forgot to wire LoginView to fire row-level databind events inside a GridView. No databind, no link parameters!
In deference to a positive attitude, I'll make no comment on the level of organization it takes to allow that out the door....

Asp.net 2.0 Gridview edit

i have used grid view to display data. Now i need to edit the row. I have kept edit link button in template field of grid-view. Now when i click the edit button, i need to retrieve the data for particular row into the server controls, so that user can enter the data into it.
How can i do that?
let me know if any info required..
thanks!
UPDATED
See http://img18.imageshack.us/i/editform.jpg/
Now, when i click edit from below grid, the data in grid should come up in above form.
There is a different template available in Gridview and you have to use it properly. For example, if you want to edit something, the editTemplate is available for that.. look at the following sample:
<asp:GridView runat="server" ID="grd">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:Label runat="server" ID="lbl"></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox runat="server" ID="Textbox1"></asp:TextBox>
<asp:HiddenField runat="server" ID="hdf" />
</EditItemTemplate>
<InsertItemTemplate>
<asp:TextBox runat="server" ID="Textbox1"></asp:TextBox>
<asp:HiddenField runat="server" ID="hdf" />
</InsertItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
have a look on these article
http://www.asp.net/data-access/tutorials/using-templatefields-in-the-gridview-control-cs
http://programming.top54u.com/post/ASP-Net-GridView-Edit-ItemTemplate-Mode.aspx
This is such a general question that you should really review how the GridView works in the first place.
Try reviewing the following example from MSDN regarding GridView editting;
http://msdn.microsoft.com/en-us/library/ms972948.aspx

Gridview loses ItemTemplate after columns are removed

I'm trying to bind a datatable to a gridview where I've removed some of the autogenerated columns in the code behind.
I've got two template columns and it seems that when I alter the gridview in code behind and remove the non-templated columns that the templates loose the controls that are in them.
Using the following as a sample, "Header A" will continue to be visible but "Header B" will dissapear after removing any columsn that are located at index 2 and above. I'm creating columns in my codebehind for the grid as a part of a reporting tool. If I don't remove the columns then there doesn't seem to be an issue.
<asp:GridView ID="DataGrid1" runat="server" AutoGenerateColumns="false" AllowPaging="True" PageSize="10" GridLines="Horizontal">
<Columns>
<asp:TemplateField HeaderText="Header A" >
<ItemTemplate >
Text A
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<HeaderTemplate>
Header B
</HeaderTemplate>
<ItemTemplate>
Text B
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
For i = 2 To DataGrid1.Columns.Count - 1
DataGrid1.Columns.RemoveAt(2)
Next
EDIT
So from what I've read this seems to be a problem that occurs when the grid is altered. Does anyone know of a good workaround to re-initialize the template columns or set them up again so that when the non-template columns are removed that hte templates don't get removed as well?
Do you need the GridView to have ViewState? Try turning off ViewState.
<asp:GridView ID="DataGrid1" runat="server" AutoGenerateColumns="false" AllowPaging="True" PageSize="10" GridLines="Horizontal" EnableViewState="false">
hi solved with visibile=false.
on databind .net will not associate values and don t create them on html page

ASP.NET GridView HeaderRow

Is it possible to set the a gridview's headerrow property from the aspx code? I'd like to be able to include extra controls to the header such as textbox for filter. I can do this from the C# code behind by dynamically adding the controls, but doing this seems to introduce some suitable problems that I'd rather avoid.
Use a header template field.
<columns>
<asp:templatefield>
<headerstyle backcolor="Navy" forecolor="White"/>
<headertemplate>
<asp:textbox id="txtFilter" runat="server"/>
</headertemplate>
</asp:templatefield>
</columns>
See here.

Resources