I have a gridview which is binded to a datasource like this:
var beerlist = (from b in ctx.beers
select new
{
id = b.beerid,
b.brand,
b.kind,
etc... }).ToList();
BeerListGridView.DataSource = beerlist;
DataBind();
i do this in the page_load event with: if (!IsPostBack).
now i add buttons in the onrowdatabound event like this:
if (e.Row.RowType == DataControlRowType.DataRow)
{
String beerid = e.Row.Cells[0].Text;
int columncount = e.Row.Cells.Count;
TableCell MyCell = new TableCell();
Button EditButton = new Button();
EditButton.ID = beerid;
EditButton.Text = "Edit";
EditButton.CommandName = "EditButton_Click";
EditButton.CommandArgument = beerid;
MyCell.Controls.Add(EditButton);
MyCell2.Controls.Add(InfoButton);
e.Row.Cells.AddAt(columncount, MyCell);
e.Row.Cells.AddAt(columncount + 1, MyCell2);
the rowcommand event looks like this:
if (e.CommandName != "Sort")
{
Session["beerid"] = e.CommandArgument;
if (e.CommandName == "EditButton_Click")
{
Response.Redirect("EditBeer.aspx");
}
else if (e.CommandName == "InfoButton_Click")
{
Response.Redirect("ViewRatingsRemarks.aspx");
}
}
in the sorting event i do the following:
if (e.SortExpression == "brand")
{
if (Session["brandsortorder"] == null)
{
var beerlist = (from b in ctx.beers
orderby b.brand
select new
{
id = b.beerid,
b.brand,
b.kind,
etc... }).ToList();
BeerListGridView.DataSource = beerlist;
DataBind();
Session["brandsortorder"] = "DESC";
}
else if etc..
when i click the edit button al the buttons disappear an i am left with the datagridview without the buttons. What am i doing wrong?
The reason that the buttons are disappearing is that you're binding (again) your datagrid and the buttons are not being re-created.
This article should get you started quickly on GridView with templates and editing.
Thanx for the advice, i did the following: added boundfields to gridview and set autogeneratecolumns of gridview to false:
<asp:GridView ID="BeerListGridView" AutoGenerateColumns="false" OnRowCommand="BeerListGridView_RowCommand" AllowSorting="true" OnSorting="BeerListGridView_Sorting" runat="server">
<Columns>
<asp:BoundField DataField="id" HeaderText="id" SortExpression="id" />
<asp:BoundField DataField="brand" HeaderText="brand" SortExpression="brand" />
<asp:BoundField DataField="kind" HeaderText="kind" SortExpression="kind" />
<asp:BoundField DataField="alcperc" HeaderText="alcperc" SortExpression="alcperc" />
<asp:BoundField DataField="taste" HeaderText="taste" SortExpression="taste" />
<asp:BoundField DataField="color" HeaderText="color" SortExpression="color" />
<asp:BoundField DataField="land" HeaderText="land" SortExpression="land" />
<asp:BoundField DataField="brewery" HeaderText="brewery" SortExpression="brewery" />
<asp:BoundField DataField="addedby" HeaderText="addedby" SortExpression="addedby" />
<asp:TemplateField><ItemTemplate><asp:Button Text="edit" CommandName="Edit" CommandArgument='<%# Eval("id") %>' runat="server" /></ItemTemplate></asp:TemplateField>
</Columns>
as you can see at the end i added the column with the edit buttons and filled in commandname and commandargument. It works!!! (onrowdatabound is not neccesary anymore...)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.