Reputation: 353
I have been struggling with this for a while now. I want to dynamically create data columns using the entity framework.
var query2 = (from c in dbc.FridayTimeSlots
where c.RoundType == "Back 9"
select c);
grvF2.Columns.Clear();
grvF2.DataSource = query2.ToList();
grvF2.Columns.Add(new BoundField { DataField = "TeeTime", HeaderText = "Tee-Off Time" });
grvF2.Columns.Add(new CommandField { HeaderText = "Select", SelectText = "Select Time", ButtonType = ButtonType.Link, ShowSelectButton = true });
grvF2.DataKeyNames = new string[] { "TimeID" };
grvF2.DataBind();
My problem, I want to only display the command field when the data field TeeTime = "something" and when TeeTime = "somethinig else" I don't want the commandfield there.
If tried just about everything but I can't get the solution. Any help would be greatly appreciated.
Upvotes: 1
Views: 86
Reputation: 7943
In GridView's RowDataBound
you can find the LinkButton
and make Visible=false
:
protected void grvF2_RowDataBound(object sender, GridViewRowEventArgs e)
{
GridViewRow gvr = e.Row as GridViewRow;
if (gvr != null && gvr.RowType == DataControlRowType.DataRow)
{
LinkButton lb = gvr.Cells[1].Controls[0] as LinkButton;
if (lb != null && gvr.Cells[0].Text.ToLower() != "something")
{
lb.Visible = false;
}
}
}
Assuming that you have this markup for the GridView
:
<asp:GridView ID="grvF2" runat="server"
AutoGenerateColumns="false" OnDataBound="grvF2_DataBound"
OnRowDataBound="grvF2_RowDataBound" >
</asp:GridView>
And the output may look like:
Upvotes: 1