Reinitializing the state of gridview after changes done - c#

How can I reinitialize the state of the GridView structure?
For instance, I have defined a set of BoundFields in my aspx page, however, the columns' names and its visibility changes accordingly to conditions.
How can I reset the whole GridView structure, including its column names and visibility to the one I have defined in my aspx page?
Is there a way to do so? Is there a general function to do such or must I define a new function to reset each and every column back to its original state considering I do not only have one Gridview with at least 30 columns in each.
EDIT:
FOR EXAMPLE:
IN MY .ASPX PAGE:
<ASP:GRIDVIEW RUNAT='SERVER' ID='GVEXAMPLE'>
<ASP:BOUNDFIELD HEADERTEXT="ABC" />
<ASP:BOUNDFIELD HEADERTEXT="DEF" />
<ASP:BOUNDFIELD HEADERTEXT="GHI" />
</ASP:GRIDVIEW>
IN MY .ASPX.CS PAGE:
GVEXAMPLE.Columns[1].HeaderText = "this is my new header text";
IS THERE A PREDEFINED FUNCTION IN C# THAT I CAN USE TO RESET THE HEADERTEXT BACK TO ITS ORIGINAL NAMING, 'DEF'?
OR DO I HAVE TO DEFINE MY OWN FUNCTIONS TO RESET ITS NAMING LIKE
GVEXAMPLE.Columns[1].HeaderText = "DEF";

Related

asp GridView add data to a certain column (after TemplateField) with Boundfield?

I have a asp:GridView wich in code behind is asigned to a List with Objects.
gwMyGridview.DataSource = test; // test is a list with some objects (two fields)
gwTract.DataBind();
I can add buttons and stuff to the columns, but my data (two columns) are always the last two columns. I know BoundField can be used, but I still get two columns in the end.
How can i do this: column1 = data1, column2 = button, column3 = data.
Now it looks like this: button, data, data
Thanks in advance!
Don't let the GridView to auto generate the columns. To do this, follow these steps:
1- Click on the GridView's Tasks (little arrow on top of the GridView when you click on it) and click on Edit Columns, and uncheck the "Auto-Generate fields" field, or from the Markup set the AutoGenerateColumns property to false
2 - Add your columns manually to the GridView, by either adding them in the previous dialog (Fields) or in the markup, in the exact order you want them to a appear in your GridView:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False">
<Columns>
<asp:BoundField DataField="data1" HeaderText="Data1" />
<asp:TemplateField HeaderText="CustomColumn"></asp:TemplateField>
<asp:BoundField DataField="data2" HeaderText="Data2" />
</Columns>
</asp:GridView>
After that you can bind normally.

ASP.NET Gridview with Link button column on Dynamic Grid

ASP.NET Gridview with Link button column on Dynamic Grid
The ASP.NET Gridview should have a linked column - 1st column, when clicked should take to another page with the clicked cell value. the Grid is a dynamic one, that is the columns are not fixed, no of columns/ column itself is dynamic. I added a asp control just for the first columns and remaining columns are dynamic, in the code behind and I add the first column "linkbutton" always.
<Columns>
<asp:TemplateField HeaderText="linkbutton">
<ItemTemplate>
<asp:LinkButton ID="linkbutton" runat="server" Text='<%#EVal("linkbutton") %>'
CommandName="ShowDetails" CommandArgument='<%#Eval("linkbutton") %>'>
</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
linkbutton is part of the datatable, so when I bind the datatable to gridview it appears twice, once for the templatefield and once from the datatable bind. gridview.column().visible=false didnot work as it considers linkbutton as the only column not the datatable columns.
i tried to add the Linkbutton control from code behind that one also didnt work.
I would set the autogeneratecolumns to false, and then use asp:BoundFields in order to show the columns from the datatable, other than the linkbutton, which you are handling with the asp:TemplateField.
You can then use GridView.Columns().Visible to hide/show different columns.
You were not able to use GridView.Columns().Visible with the auto generated columns because they are not added to Columns():
Explicitly declared column fields can be used in combination with
automatically generated column fields. When both are used, explicitly
declared column fields are rendered first, followed by the
automatically generated column fields. Automatically generated column
fields are not added to the Columns collection.

DetailsView binds to columns in database and asp:bound columns resulting in duplicate columns shown?

I have a DetailsView that has a Fields element and some bound columns. The DetailsView is bound to a SqlDataSource. Code on dev and in Environment B is exactly the same, but on dev, I am seeing the columns from the database as well as columns that are bound, so it results in a duplicate columns shown. In Environment B, this is not happening, so I am wondering if it is a data issue, but I am not sure where to start or look?
<asp:DetailsView id="dv" runat="server" DataSourceID="sql" DataKeyNames="Id" OnDataBound="dv_DataBound" OnItemInserted="dv_ItemInserted" OnModeChanged="dv_ModeChanged" HeaderText="Add New" AutoGenerateInsertButton="true">
<Fields>
<asp:BoundField HeaderText="Name" DataField="sName" />
</Fields>
</asp:DetailsView>
The above DetailsView will generate a form that displays label sName with a textbox next to it and another label Name with a TextBox next to it, but the exact same code in Environment B only generates the label Name with a TextBox next to it. This has me thinking it is a data issue, but I can't pinpoint where to start looking. The SqlDataSource.SelectCommand is just a stored proc that is similar to:
SELECT * FROM TABLE_1
Does the DetailsView has something like AutoGenerateColums in gridview?

Adding new Columns to GridView with a new DataSource

I have a gridview that is loaded from a DataSet, in which most of the fields are generated through templates.
One of them is an Image that changes according to the value that is returned from the dataset.
Here is an example:
<asp:TemplateField HeaderText="Attention">
<ItemTemplate>
<asp:Image ID="alertFlag" runat="server" ImageUrl='<%# Eval("alertFlag").ToString().Equals("True") ? "~/Images/Warning-32.png" : "" %>' />
</ItemTemplate>
</asp:TemplateField>
Now I have a dropdown list, in which, depending on what is chosen, a new request to the database is made, and new columns are added, but the previous ones must stay.
Should I create a new GridView or how should I add new the new columns, keep them hidden, and then show them when the proper event is fired. this includes loading new data from the database and binding to the gridview. And how would I add the condition above programatically?
Thanks.
I would say that you will use the same gridview and have column with visible set to true or false. I will suggest that you have a property in the code behind such as ShowColumnX which will return true or false based on the dropdown value. In the aspx, you will then bind the visible property to that value such as Visible="<%= ShowColumnX %>"
If your existing column is setup to bind value correctly to a field of your data source, then you dont have to do anything different then what you have already done such as rebinding the data source on postback.

How to Bind List<myclass> data with gridview?

Look please my web service codes return type List
i get data from web service with listformat List; also created a gridview below and return list to gridview datasource.Bur eror occurs:
A field or property with the name 'name' was not found on the selected data source.
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:BoundField HeaderText="Ad" DataField="name" />
<asp:BoundField HeaderText="SoyAd" DataField="surname" />
<asp:BoundField HeaderText="Numara" DataField="number" />
</Columns>
</asp:GridView>
wstest ws = new wstest();
GridView1.DataSource = ws.GetList(); ;
GridView1.DataBind();
Check the contents of your list that you generate. If you do not have a field called name that gets output when you call GetList() then it's going to break because the GridView is looking for it and it's not there to bind to.
You may have to paste some more code for us to verify that, however.
Edit: based on your link of the class you created, it seems that you need to verify that your property "Name" is used with the proper capitalization in all areas. C# is case sensitive so that is most likely why it is being thrown off. Make sure for your other properties as well.
Edit 2: It should also be noted that the confusion may be from the fact that your private members are all lowercase, while your properties are capitalized. Your GridView is going to be binding to the public properties, so that is why you want to make sure the Gridview is looking at the capitalized properties "Name", "SurName", etc.
This line
<asp:BoundField HeaderText="Ad" DataField="name" />
is saying to get a proprty value from each item in teh list, and bind it to this column within the grid. It would seem that the item within the list don't have a property "Name"

Categories

Resources