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.
Related
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";
I need to show the user in a grid the count of establishments that has each active status in the application. I already have the query but obviously it returns me 2 columns, one with the status name and one with the count. Should I use something like Pivot Columns in SQL Server and set the columns names and properties on the fly using row[0] or there is a better approach for what I need to do?
P.S.
I am using Obout Grid to display the data.
<obout:Grid ID="gridStatus" runat="server" FolderStyle="~/obout/grid/styles/style_6"
AllowAddingRecords="False" AutoGenerateColumns="False" AllowColumnReordering="True"
PageSize="10" ShowTotalNumberOfPages="True" AllowManualPaging="True" AllowFiltering="True"
AllowMultiRecordSelection="false"
PageSizeOptions="10,20,30,40,50,100,500,999">
<Columns>
<obout:Column DataField="name" HeaderText="Status" />
<obout:Column DataField="statusTotal" HeaderText="Total"/>
</Columns>
<ScrollingSettings ScrollWidth="100%" />
<FilteringSettings FilterLinksPosition="TopAndBottom" FilterPosition="Top" />
</obout:Grid>
But I can't use that format, the columns has to be each status.
You can either pivot results of your query in backend SQL or, if it becomes to complicated - retrieve your 2-column data into .NET code and build a DataTable programmaticaly (create number of columns in the new DataTable according to number of rows in the result, name the columns according the "name" field and fill it with one row of data according to "statusTotal" field).
Then in your obout grid set AutoGenerateColumns="True" and do not specify <Columns></Columns>. This way grid will generate columns according to the datasource.
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.
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?
Currently I am doing:
var items = from t in entity.Items
select new
{
Name = t.ItemName,
Description = t.ItemDescription
};
myDataGridView.DataSource = items.ToList();
The problem is that when bound to the DataGridView, I get two columns of "Name" and "Description". I want to rename these to "Item Name" and "Item Description" (example).
If I add an unbound Column to the datagridview, it just gets displayed along with my columns. I can not seem to create a databound column.
Can you post the grid's source?
You'll need AutogenerateColumns="false" to add columns manually.
To change the header text, you should be able to do the following:
<Columns>
<asp:BoundField DataField="Name" HeaderText="Item Name" ReadOnly="True" />
// .. etc
I found out how to use drag and drop of an object data source to bind the columns to a DataGridView directly, which allows me to change the header text.