What I want to do is just display a few specific columns from my data source on my gridview, but for some reason I'm seeing first the boundfields that I want, followed by every column in the data source.
<asp:GridView DataSourceId="dsTasks" ID="TasksGridView" runat="server">
<Columns>
<asp:BoundField DataField="field1" HeaderText="field1" />
<asp:BoundField DataField="field2" HeaderText="field2" />
<asp:BoundField DataField="field3" HeaderText="field3" />
<asp:BoundField DataField="field4" HeaderText="field4" />
<asp:BoundField DataField="field5" HeaderText="field5" />
<asp:BoundField DataField="field6" HeaderText="field6" />
<asp:BoundField DataField="field7" HeaderText="field7" />
</Columns>
</asp:GridView>
and here is my code for the datasource, just in case thats where it is
<asp:EntityDataSource ID="dsTasks" runat="server"
ConnectionString="name=Entities" DefaultContainerName="Entities"
EnableFlattening="false" EntitySetName="Tasks" ></asp:EntityDataSource>
Sorry if this is a stupid question...I'm fairly new to .NET in general (try a week)
Also, as a side question, would it be better for me to bind the data this way, or do it in the code-behind on page_load?
Set AutoGenerateColumns to false
<asp:GridView AutoGenerateColumns="false" DataSourceId="dsTasks" ...
Personally, I prefer binding from markup than from code when and where it's possible.
You might have to set autogeneratecolumn to false for the gridview
Related
I have a simple grid on my ASPX page and I am binding it with data coming from a select query on button click event. I am not sure how to bind columns of this table with my grid as currently I am getting 8 columns, 4 with the header given in aspx page and 4 with headers of table columns. Below is my button click event code.
protected void btnSearch_Click(object sender, EventArgs e)
{
MyBookListCont myBookListCont = new MyBookListCont();
gdvMyBooks.DataSource = myBookListCont.SearchBookDetailsCont();
gdvMyBooks.DataBind();
}
And below is aspx code of gridview.
<asp:GridView ID="gdvMyBooks" runat="server">
<Columns>
<asp:BoundField DataField="BK_NM" HeaderText="Book Name" />
<asp:BoundField DataField="ATHR_NM" HeaderText="Author Name" />
<asp:BoundField DataField="BUY_YR" HeaderText="Buy Year" />
<asp:BoundField DataField="PRICE" HeaderText="Price" />
</Columns>
</asp:GridView>
Looks like a silly question, but help would be much appreciated.
This is because you have explicitly specified 4 columns in this design code :
<Columns>
<asp:BoundField DataField="BK_NM" HeaderText="Book Name" />
<asp:BoundField DataField="ATHR_NM" HeaderText="Author Name" />
<asp:BoundField DataField="BUY_YR" HeaderText="Buy Year" />
<asp:BoundField DataField="PRICE" HeaderText="Price" />
</Columns>
Now here, as you can see, there is a HeaderText property which will override the table column name and display the text that is mentioned in here.
You have a couple of options here that you can try :
First, if you want all columns like this with custom header text, you can define all the other columns with these 4 in the same manner as well. That will display all the data and column headers as per the given format.Something like this :
<asp:GridView ID="gdvMyBooks" AutoGenerateColumns="False" runat="server">
<Columns>
<asp:BoundField DataField="BK_NM" HeaderText="Book Name" />
<asp:BoundField DataField="ATHR_NM" HeaderText="Author Name" />
<asp:BoundField DataField="BUY_YR" HeaderText="Buy Year" />
<asp:BoundField DataField="PRICE" HeaderText="Price" />
//other columns using same syntax as above.
</Columns>
</asp:GridView>
Also, if you dont want to bind all columns coming in your query, you can use this attibute in your gridview,
AutoGenerateColumns = False
And just specify the columns that you need like you have currently done.
The other thing is, if you want to straight away bind the result in your query to your GridView, then just remove those 4 BoundField statements and leave as it is. This will bind your table to GridView with the same header names as the column names.
I hope this makes things clear.
You have to set your GridView.AutoGenerateColumns to false :
<asp:GridView ID="gdvMyBooks" runat="server" AutoGenerateColumns="False">
Otherwise, it will bind all the field of your object.
Is it possible to change a gridviews DataSouceId to a gridview DataSouce after the gridview is loaded. I have a gridview with a DataSouceId that is equal to SqlDatasource. I have a button when pressed executes a some LINQ which I want to change what's in the gridview.
var sce = from pk in db.Tables
where pk.Score > 20
select new { pk.First_Name, pk.Last_Name, pk.Score };
GridView1.DataSourceID = "";
GridView1.DataSource = sce;
GridView1.DataBind();
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="First Name" DataSourceID="SqlDataSource2" AllowSorting="True">
<Columns>
<asp:BoundField DataField="First Name" HeaderText="First Name" ReadOnly="True" SortExpression="First Name" />
<asp:BoundField DataField="Last Name" HeaderText="Last Name" SortExpression="Last Name" />
<asp:BoundField DataField="Score" HeaderText="Score" SortExpression="Score" />
<asp:BoundField DataField="Nationality" HeaderText="Nationality" SortExpression="Nationality" />
</Columns>
</asp:GridView>
I tried to change to a empty string. This compiles but crashes when the button is clicked.
Is what I'm trying possible?
The problem is that the query defines columns with different names from what the GridView expects. For example, compare:
pk.First_Name
and
DataField="First Name"
These are different, theerefore GridView is not able to find the column in data source and throws exception.
The best option would be to use underscore in GridView as well. That might require some refactoring in your initial SQL, but it should e done anyway - having spaces in column names is very brittle and uncommon, better refrain from it altogether:
<asp:BoundField DataField="First_Name" ...
Or you can define a TemplateField that looks for column with either name and displays what was found. But that sounds like a terribly huge overhead.
i have to display data like this in grid view:
the problem is that:
for each unique set of manufacturer , model and type, the number of values associated with watts column changes ... that is, there is no fixed numbers of values in this single column. how can i display such a format in my grid view? Kindly help ...
Could you just place another (nested) gridView in that column? You'd have to use a template column in your parent grid, but then for each row, the number of records in that column could be different.
<asp:GridView ID="parentGridView" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="Manufacturer" />
<asp:BoundField DataField="ModelNo" HeaderText="Model No" />
<asp:BoundField DataField="Type" />
<asp:TemplateField HeaderText="Watts">
<ItemTemplate>
<asp:GridView ID="nestedWattsView" runat="server" AutoGenerateColumns="true" ShowHeader="false" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
i have code of grid view in aspx.cs it show an error, i cannot understand how it solve, So please anyone can help me? Error picture is attached.
A GridView can just have one DataSource. Since you are setting the DataSource programmatically remove the DataSourceID from the aspx part since that is used for declarative datasource controls like SqlDataSource or ObjectDataSource.
For example:
<asp:gridview id="CustomersGridView"
datasourceid="CustomersSource" <-- !!! REMOVE THIS !!!
autogeneratecolumns="False"
emptydatatext="No data available."
allowpaging="True"
runat="server" DataKeyNames="CustomerID">
<Columns>
<asp:BoundField DataField="CustomerID" HeaderText="CustomerID"
InsertVisible="False" ReadOnly="True" SortExpression="CustomerID" />
<asp:BoundField DataField="CompanyName" HeaderText="CompanyName"
SortExpression="CompanyName" />
<asp:BoundField DataField="FirstName" HeaderText="FirstName"
SortExpression="FirstName" />
<asp:BoundField DataField="LastName" HeaderText="LastName"
SortExpression="LastName" />
</Columns>
</asp:gridview>
I think you have applied datasource Id from Aspx page to Grid.
like,
DataSourceID="DatasourceId"
Use one only.
go in your default.aspx, find the tag that contains the attribute ID="GridView1" and wipe from it the attribute DataSourceID="[SOMETHING]"
I have a list of objects called Activity and I want to display the date, type and notes for each and every one of these activities. This is the code I'm using.
<asp:GridView ID="gvTable" runat="server" AllowSorting="true" ShowHeader="true">
<Columns>
<asp:BoundField DataField="ActivityDate" HeaderText="Date"
HeaderStyle-CssClass="date" />
<asp:BoundField DataField="ActivityType" HeaderText="Type" />
<asp:BoundField DataField="ActivityNotes" HeaderText="Notes" />
</Columns>
<PagerSettings Position="Bottom" Mode="NextPrevious" PageButtonCount="5"
PreviousPageText="Older activities" NextPageText="Newer activities" />
</asp:GridView>
However, all of the attributes of each object is displayed in the header. How can I force it to display only the columns that I want to use?
gvTable.AutoGenerateColumns = false
or
<asp:GridView ID="gvTable" runat="server" AutoGenerateColumns="False" AllowSorting="true" ShowHeader="true">
should do the trick.
Set the attribute on your gridview:
AutoGenerateColumns="false"
You need to set the AutoGenerateColumns property on the grid to false.
Have you tried AutoGenerateColumns="false" in the gridview?