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" />
Related
here is the scene, I am making a web project of blogsite. Users can post a blog but the admin approval is necessary before showing the blogs on the webpage. I have created an admin panel page where all the pending blogs are shown in a table. What I want is to click a item "View" which will redirect to another webpage to show the full blog and admin will approve or decline the blog from that page. How do I get the key from table for a certain row. In database the blogs are stored with primary key, I just need to get that certain key from table. I have used Boundfield datafield in GridView.
Here is the code:
<Columns>
<asp:BoundField DataField="BID" HeaderText="Blog ID" />
<asp:BoundField DataField="BTitle" HeaderText="Title" />
<asp:BoundField DataField="BCategory" HeaderText="Category" />
<asp:BoundField DataField="BDate" HeaderText="Posted on" />
<asp:BoundField DataField="BStatus" HeaderText="Status" />
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="selectBlog" Text="View" runat="server" CommandArgument='<%# Eval("bID")%>' OnClick="gview_SelectedIndexChanged" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
It's look like this-
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.
As the title suggests, I am attempting to set the UpdateMehod of a GridView to false. When the Update buttons is currently clicked, the event fires validation of a separate control, even though the separate control is in a separate validation group. The grid view is shown below:
<asp:GridView ID="BikesItems"
runat="server"
CssClass="table table-hover"
ItemType="WLL.DAL.Bike"
DataKeyNames="BikeID"
SelectMethod="BikesItems_GetData"
UpdateMethod="BikesItems_UpdateItem"
DeleteMethod="BikesItems_DeleteItem"
AutoGenerateEditButton="true"
AutoGenerateDeleteButton="true"
AutoGenerateColumns="false"
ValidationGroup="BikeGrid">
<Columns>
<asp:DynamicField DataField="BikeID" />
<asp:DynamicField DataField="BikeName" />
<asp:DynamicField DataField="Description" />
<asp:DynamicField DataField="ImgPath" />
<asp:DynamicField DataField="Price" />
<asp:DynamicField DataField="ManufacturerID" />
</Columns>
</asp:GridView>
How do I set the CausesValidation to false for the update button, bearing in mind it is dynamically generated? Or does a more elegant solution exist? Thanks
If you just wish to disable the validation group in the code behind (server-side), Use Page.GetValidators("ValidationGroup") method. You can pass in the name of the ValidationGroup and the method returns a ValidatorCollection over which you can iterate.
Or ClientSide, you can iterate over the Page_Validators array and check each validator group name that matches the group you're looking for and disable each as needed.
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>