Gridview loses ItemTemplate after columns are removed - asp.net

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

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

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

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