ASP.NET GridView HeaderRow - asp.net

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.

Related

Template column automatically hides during postback

On the initial load of this page my template column, in an asp data grid, displays fine.
When I click any controls that force a post back the data grid reloads with the template column hidden. There is no visible attribute being set in the html. There is no code changing the visible property of the column in the code-behind.
I tried to hard-code the column's visibility to true and the property reads true in the code-behind but the column still gets hidden.
When trying to hide/show other columns in the data grid I'm unable to change them via the code-behind.
Any ideas on what is causing this column to auto-hide or why I can't effectively change the visible property in the code-behind or html?
<asp:DataGrid ID="dg" runat="server" AllowSorting="True" AutoGenerateColumns="False" CssClass="datagridPWQ">
<Columns>
<asp:BoundColumn DataField="ID" HeaderText="ID" />
<asp:TemplateColumn HeaderText="Add" >
<ItemTemplate>
<asp:Button ID="btnAdd" runat="server" Text="Add" CommandName="Add"
CssClass="buttonQ" CommandArgument='<%# DataBinder.Eval(Container.DataItem, "ID")%>' />
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:DataGrid>
Side note: I have thought about using a ButtonColumn instead but I would still like to know why this doesn't work. I remember seeing a similar error months ago but I don't remember how it was solved.

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.

Grid view sorting

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

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 Setting a DetailsView BoundField value from javascript

I'd like to know if it is possible to set a field of a DetailsView BoundField (in edit mode) from javascript? I can't set an id for the boundfield, so obviously can't use document.getElementById() to get to it, but I was wondering if there's another way to do it?
You could try to create an EditItemTemplate, place a TextBox inside and give it an ID of your choice.
Like this:
<Fields>
<asp:TemplateField HeaderText="Column1">
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
</Fields>
If there is stuff to be bound on the TextBox's Text property you could use Bind() method.
Good Luck.

Resources