Getting name of table columns in gridview in addition to custom names - c#

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.

Related

Is it possible to change gridviewid to to a gridview DataSouce after the gridview is loaded

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.

How can i make my grid view show multiple values (the number of values varies) in a single column cell?

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>

Grid View in aspx

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]"

GridView - accessing elements

I have a GridView control that lists Customer objects with these fields:
ID, FirstName, LastName, and SignUpDate.
I need to add a fifth column ('Edit') to the GridView control that allows me
to edit that particular Customer. It could be a LinkButton or a BoundButton.
So when the 'Edit' link/button is clicked, I want to access the Id and pass
it to the Click event. I have a method that takes an Id and edits the Customer.
The GridView is bound to a List of Customer objects. How can I do this?
<asp:GridView ...>
<Columns>
<asp:ButtonField HeaderText="Customer ID" DataTextField="Id" />
<asp:ButtonField HeaderText="First Name" DataTextField="FirstName" />
<asp:ButtonField HeaderText="Last Name" DataTextField="LastName" />
<asp:ButtonField HeaderText="Member Since" DataTextField="SignUpDate" />
<'Edit' link/button here. Want to pass 'Id' to a method when clicked />
</Columns>
</asp:GridView>
customerList.DataSource = customerList; // this is List<Customer>
customerList.DataKeyNames = new string[] {"Id"};
customerList.DataBind();
You need to add a commandfield to define your edit column.
<asp:CommandField ShowEditButton="True" />

GridView showing complete datasource as well as BoundFields

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

Categories