How to hide a TemplateField column in a GridView - asp.net

How can I hide a TemplateField column in a GridView?
I tried the following:
<asp:TemplateField ShowHeader="False" Visible='<%# MyBoolProperty %>' >
<ItemTemplate>
<asp:LinkButton ID="attachmentButton" runat="server" ... />
</ItemTemplate>
but it didn't work and gives the following error:
Databinding expressions are only supported on objects that have a DataBinding event.
System.Web.UI.WebControls.TemplateField does not have a DataBinding event.
I tried also to hide it programmatically, but seems it's not possible to get a column by the name because there iss no name for TemplateField column.

protected void OnRowCreated(object sender, GridViewRowEventArgs e)
{
e.Row.Cells[columnIndex].Visible = false;
}
If you don't prefer hard-coded index, the only workaround I can suggest is to provide a HeaderText for the GridViewColumn and then find the column using that HeaderText.
protected void UsersGrid_RowCreated(object sender, GridViewRowEventArgs e)
{
((DataControlField)UsersGrid.Columns
.Cast<DataControlField>()
.Where(fld => fld.HeaderText == "Email")
.SingleOrDefault()).Visible = false;
}

For Each dcfColumn As DataControlField In gvGridview.Columns
If dcfColumn.HeaderText = "ColumnHeaderText" Then
dcfColumn.Visible = false
End If
Next

If appears to me that rows where Visible is set to false won't be accessible, that they are removed from the DOM rather than hidden, so I also used the Display: None approach. In my case, I wanted to have a hidden column that contained the key of the Row. To me, this declarative approach is a little cleaner than some of the other approaches that use code.
<style>
.HiddenCol{display:none;}
</style>
<%--ROW ID--%>
<asp:TemplateField HeaderText="Row ID">
<HeaderStyle CssClass="HiddenCol" />
<ItemTemplate>
<asp:Label ID="lblROW_ID" runat="server" Text='<%# Bind("ROW_ID") %>'></asp:Label>
</ItemTemplate>
<ItemStyle HorizontalAlign="Right" CssClass="HiddenCol" />
<EditItemTemplate>
<asp:TextBox ID="txtROW_ID" runat="server" Text='<%# Bind("ROW_ID") %>'></asp:TextBox>
</EditItemTemplate>
<FooterStyle CssClass="HiddenCol" />
</asp:TemplateField>

GridView1.Columns[columnIndex].Visible = false;

try this
.hiddencol
{
display:none;
}
.viscol
{
display:block;
}
add following code on RowCreated Event of GridView
protected void OnRowCreated(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Cells[0].CssClass = "hiddencol";
}
else if (e.Row.RowType == DataControlRowType.Header)
{
e.Row.Cells[0].CssClass = "hiddencol";
}
}

Am I missing something ?
If you can't set visibility on TemplateField then set it on its content
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton Visible='<%# MyBoolProperty %>' ID="foo" runat="server" ... />
</ItemTemplate>
</asp:TemplateField>
or if your content is complex then enclose it into a div and set visibility on the div
<asp:TemplateField>
<ItemTemplate>
<div runat="server" visible='<%# MyBoolProperty %>' >
<asp:LinkButton ID="attachmentButton" runat="server" ... />
</div>
</ItemTemplate>
</asp:TemplateField>

A slight improvement using column name, IMHO:
Private Sub GridView1_Init(sender As Object, e As System.EventArgs) Handles GridView1.Init
For Each dcf As DataControlField In GridView1.Columns
Select Case dcf.HeaderText.ToUpper
Case "CBSELECT"
dcf.Visible = Me.CheckBoxVisible
dcf.HeaderText = "<small>Select</small>"
End Select
Next
End Sub
This allows control over multiple column.
I initially use a 'technical' column name, matching the control name within.
This makes it obvious within the ASCX page that it's a control column.
Then swap out the name as desired for presentation.
If I spy the odd name in production, I know I skipped something.
The "ToUpper" avoids case-issues.
Finally, this runs ONE time on any post instead of capturing the event during row-creation.

protected void gvLogMessageDetail_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Header)
{
if (rdlForImportOrExport.SelectedIndex == 1)
{
e.Row.Cells[3].Visible = false;
e.Row.Cells[4].Visible = false;
e.Row.Cells[5].Visible = false;
}
else
{
e.Row.Cells[3].Visible = true;
e.Row.Cells[4].Visible = true;
e.Row.Cells[5].Visible = true;
}
}
if (e.Row.RowType == DataControlRowType.DataRow) //skip header row
{
try
{
if (rdlForImportOrExport.SelectedIndex == 1)
{
e.Row.Cells[3].Visible = false;
e.Row.Cells[4].Visible = false;
e.Row.Cells[5].Visible = false;
}
else
{
e.Row.Cells[3].Visible = true;
e.Row.Cells[4].Visible = true;
e.Row.Cells[5].Visible = true;
}
}
catch
{
ClientScript.RegisterStartupScript(GetType(), "Expand", "<SCRIPT LANGUAGE='javascript'>alert('There is binding problem in child grid.');</script>");
}
}
}

This can be another way to do it and validate nulls
DataControlField dataControlField = UsersGrid.Columns.Cast<DataControlField>().SingleOrDefault(x => x.HeaderText == "Email");
if (dataControlField != null)
dataControlField.Visible = false;

Related

Remove TemplateField during page load or before databind

I want to remove templatefield from gridview during pageload or before databind to GridView. I have 2 data sources for retrieving data. The data retrieved from one of the data sources do not have the columns ExpireDate and ExpireDays.
So I want to delete the templatefields corresponding to ExpireDate and ExpireDays if the GridView is populated from the data source that do not have those 2 fields.
Setting the visibility to false still will have error of DataRowView doesn't contain property name ExpireDate and ExpireDays.
Markup
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" OnRowCommand="GridView1_RowCommand" OnRowDataBound="GridView1_RowDataBound">
<Columns>
<asp:BoundField DataField="ID" HeaderText="No."/>
<asp:BoundField DataField="Name" HeaderText="Name"/>
<asp:BoundField DataField="CourseName" HeaderText="Course Enroll" />
<asp:BoundField DataField="SubMember" HeaderText="ChildMember" />
<asp:TemplateField HeaderText="Expiry Days">
<ItemStyle HorizontalAlign="Center" />
<ItemTemplate>
<asp:Label runat="server" ID="expDsL" Text=' <%# Eval("ExpiryDays") %>'>
</asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Expiry On">
<ItemStyle HorizontalAlign="Center" />
<ItemTemplate>
<asp:Label runat="server" ID="expDeL" Text=' <%# Eval("ExpiryDate") %>'>
</asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Function">
<ItemStyle HorizontalAlign="Center" Width="100px"></ItemStyle>
<ItemTemplate>
<asp:ImageButton ImageUrl="~/Images/editB.gif" ID="btnEdit" runat="server" ToolTip="Edit" CommandName="Edit" CommandArgument='<%# Eval("ID") %>' />
<asp:ImageButton ImageUrl="~/Images/delB.gif" ID="btnDelete" runat="server" ToolTip="Delete" CommandName="Delete" CommandArgument='<%# Eval("ID") %>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Code Behind
protected void Page_Load(object sender, EventArgs e)
{
if(Class.ToLower() != "classAA")
{
// using naveen answer can remove the column.
var expiryDateF = ((DataControlField)GridView1.Columns.Cast<DataControlField>().Where(fid => fid.HeaderText == "Expiry Days").SingleOrDefault());
var expiryDaysF = ((DataControlField)GridView1.Columns.Cast<DataControlField>().Where(fid => fid.HeaderText == "Expiry On").SingleOrDefault());
if (expiryDateF != null)
{
GridView1.Columns.Remove(expiryDateF);
}
if (expiryDaysF != null)
{
GridView1.Columns.Remove(expiryDaysF);
}
}
if(!this.IsPostBack)
{
BindData();
}
}
protected void SaveMember(object sender, EventArgs e)
{
//getting member information and perform checking.
bool success = dbbb.AddMem(memberdetails);
if(success == true)
{
BindData();
}
else
{
//prompt fail message.
}
}
protected void BindData()
{
DataTable dt = dbbb.RetrieveList();
if(dt != null)
{
GridView1.DataSource = dt;
GridView1.DataBind();
}
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if(e.Row.RowType == DataControlRowType.DataRow)
{
ImageButton btnDel = (ImageButton)e.Row.FindControl("btnDelete");
btnDel.Attributes.Add("onclick", "return confirm('Are you sure you want to delete this member?')");
}
}
How could I delete the column?
Two methods to achieve this
Method 1 - Remove by Index
if(!table.Columns.Contains("CustomerID1"))
{
//seven is the column index here. ie, 8th column
CustomersGrid.Columns.RemoveAt(7);
}
Method 2 - Remove using Columns Header Text
var expiryDateField= ((DataControlField)CustomersGrid.Columns
.Cast<DataControlField>()
.Where(fld => fld.HeaderText == "Expiry Date")
.SingleOrDefault());
if(expiryDateField != null)
{
CustomersGrid.Columns.Remove(expiryDateField);
}
Please not that
CustomersGrid is the name of the asp:GridView here.
table is the DataTable that acts as the DataSource of the GridView
The code should be called before DataBind of the GridView
Attach to the rowDataBound event of the grid, there you can set it dynamically.
You have to know the datasource name of the field you want to hide,
for example the headertext for the cell is "SomeHeader", but the databoundfield name for that cell is
"SomeOtherName"
then you have to check it like this (debug through GetColumnIndexByName) and
check the value of
((BoundField)cell.ContainingField).DataField
int GetColumnIndexByName(GridViewRow row, string columnName)
{
int columnIndex = 0;
foreach (DataControlFieldCell cell in row.Cells)
{
if (cell.ContainingField is BoundField)
if (((BoundField)cell.ContainingField).DataField.Equals(columnName))
break;
columnIndex++; // keep adding 1 while we don't have the correct name
}
return columnIndex;
}
remember that the code above will use a BoundField... then use it like:
protected void GridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
int index = GetColumnIndexByName(e.Row, "SomeOtherName");
string columnValue = e.Row.Cells[index].Text;
}
}

Multiple control types in a single GridView column

with: SELECT id, name, Flag1 FROM Table
Is it possible to dynamically display either a Button or a CheckBox in a single column based upon the bit value of the Flag1 field?
In other words:
If Flag1 = true then show checkbox as checked and readonly.
If Flag1 = false show Button with click event and pass id of row to handler.
Which event would i need to hook to make the change? DataBound?
How would I set up the checkbox/button in .aspx or .cs? Normally I use ItemTemplates with Gridviews...
<asp:TemplateField HeaderText="Under Warranty" >
<ItemTemplate>
<asp:Button ID="WButton" runat="server" CommandName="AddWarrantyTrackerItem" CommandArgument = "<%# (Container.DataItemIndex) %>" Text="Track Warranty" Visible="false" />
<asp:CheckBox ID="WFlag" runat="server" Visible="false" readonly="true" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Depot" >
<ItemTemplate>
<asp:Button ID="DButton" runat="server" CommandName="AddDepotTrackerItem" CommandArgument = "<%# (Container.DataItemIndex) %>" Text="Track Depot" Visible="false" />
<asp:CheckBox ID="DFlag" runat="server" Visible="false" readonly="true" />
</ItemTemplate>
</asp:TemplateField>
CodeBehind:
protected void gridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
CheckBox WFlag = (CheckBox)e.Row.FindControl("WFlag");
Button WButton = (Button)e.Row.FindControl("WButton");
CheckBox DFlag = (CheckBox)e.Row.FindControl("DFlag");
Button DButton = (Button)e.Row.FindControl("DButton");
var record = (System.Data.Common.DbDataRecord)e.Row.DataItem;
bool flagW = (bool)record.GetValue(14);
bool flagD = (bool)record.GetValue(15);
int reqnum = (int)record.GetValue(0);
if (flagW == false)
{
WButton.Visible = true;
WFlag.Visible = false;
}
else
{
WButton.Visible = false;
WFlag.Visible = true;
WFlag.Checked = true;
}
if (flagD == false)
{
DButton.Visible = true;
DFlag.Visible = false;
}
else
{
DButton.Visible = false;
DFlag.Visible = true;
DFlag.Checked = true;
}
}
}
Yes, you can use a TemplateField and RowDataBound to switch visibility :
aspx:
<Columns>
<asp:TemplateField HeaderText="Flagged">
<ItemTemplate>
<asp:Button ID="ButtonFlag" runat="server" Text="flag it" Visible="true" />
<asp:CheckBox ID="CheckFlag" runat="server" Checked="true" Visible="false" />
</ItemTemplate>
</asp:TemplateField>
Codebehind:
protected void gridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
CheckBox chkFlag = (CheckBox)e.Row.FindControl("CheckFlag");
Button buttonFlag = (Button)e.Row.FindControl("ButtonFlag");
DataRow row = ((DataRowView)e.Row.DataItem).Row;
bool flagged = row.Field<bool>("Flag1");
buttonFlag.Visible = !flagged;
chkFlag.Visible = flagged;
}
}

Row background color in gridview?

In asp.net application, I am using grid-view control in that I am binding the data to the label which is in grid-view.
If data is empty then the color of the row should be in red
If not I mean if data is there to bind then the row in green.
This is my code:
<asp:TemplateField HeaderText ="Holiday Region">
<ItemTemplate >
<asp:Label ID ="lblholdareg" runat ="server" Text ='<%# Eval("Holidaregion") %>' >
</asp:Label>
</ItemTemplate>
</asp:TemplateField>
You need to handle the RowDataBound event, get into the e.Row item, and assign either a CSS class or directly set the background color. I prefer setting a CSS class so you can change the rendering of it without a recompile later.
<asp:GridView ID="GridView1" runat="server" OnRowDataBound="GridView1_RowDataBound">
<Columns>
<asp:TemplateField HeaderText="Holiday Region">
<ItemTemplate>
<asp:Label ID="lblholdareg" runat="server" Text='<%# Eval("Holidaregion") %>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
And the code-behind, I had to assume you were using a DataTable as your data source, update the code to fit your data structure:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
System.Data.DataRow row = (System.Data.DataRow)e.Row.DataItem;
if (row["Holidaregion"] == null || row["Holidaregion"].ToString().Trim().Length == 0)
{
e.Row.CssClass = "row-empty";
}
else
{
e.Row.CssClass = "row-full";
}
}
You can do it on rowdatabound function of gridview as follows
protected void RowDataBound(Object sender, GridViewRowEventArgs e)
{
if(e.Row.RowType == DataControlRowType.DataRow)
{
//change it according your cell number or find element
if(e.Row.Cells[0].Text != "")
e.Row.BackColor = Color.Green;
else
e.Row.BackColor = Color.Red;
}
}
if(e.Row.RowType == DataControlRowType.DataRow)
{
Control l = e.Row.FindControl("Label1");
((Label)l).BackColor = System.Drawing.Color.Red;
}
Try something like this
<asp:TemplateField HeaderText ="Holiday Region">
<ItemTemplate >
<asp:Label ID ="lblholdareg" runat ="server"
CSSClass='<%# (String.IsNullOrEmply(Eval("Holidaregion")))?"red:green" %>'
Text ='<%# Eval("Holidaregion") %>' >
</asp:Label>
</ItemTemplate>
</asp:TemplateField>
Edit:
Instead of fighting with grid view inline and code behind stuffs, just use a jQuery and achieve the same in client side
try it this code it is every row in color change with the category wise or filters wise
http://devloper4u.blogspot.in/2013/10/how-to-set-color-in-every-row-on.html

dynamically hiding linkbuttons in a gridview

Here is my template field with 2linkbuttons in a gridview.
How do I hide one of the lnkbuttons depending on the user selection
<asp:TemplateField HeaderText="Action">
<ItemTemplate>
<table>
<tr>
<td align="center">
<asp:LinkButton ID="LinkButton1" runat="server" CommandName= "Publish" CommandArgument="<%# Container.DataItemIndex %>"></asp:LinkButton>
</td>
<<td align="center">
<asp:LinkButton ID="LinkButton2" runat="server" CommandName= "Block" HeaderText="Block" CommandArgument="<%# Container.DataItemIndex %>">Block</asp:LinkButton>
</td>
</tr>
</table>
</ItemTemplate>
</asp:TemplateField>
protected void GridView1_OnRowDataBound(object sender, GridViewRowEventArgs e)
{
if(Status=="Posted")
{
LinkButton lbtn2 = (LinkButton)e.Row.FindControl("LinkButton2");
lbtn2.Visible = false;
}
if(Status=="Publish")
{
LinkButton lbtn1 = (LinkButton)e.Row.FindControl("LinkButton1");
lbtn1.Visible = false;
}
}
I am doing this using 2 linkbuttons in the template field.
Here if the Status == Posted, then hide LinkButton2.
If ItemType == Published, then hide LinkButton1.
I don't mind using only Linkbutton and set the commandName and commandArgument in aspx.cs`
I am getting an error at...
LinkButton lbtn2 = (LinkButton)e.Row.FindControl("LinkButton2");
lbtn2.Visible = false;
Error: Object reference not set to an instance pointing to lnbt2.Visible = false;
Any clues,
Thanks
Sun
Check to be sure that the row in question is a data row, not a header, etc. The issue is probably because the current row is a header or footer, so it doesn't contain your controls and throws an exception.
protected void GridView1_OnRowDataBound(object sender, GridViewRowEventArgs e)
{
if(e.Row.RowType == DataControlRowType.DataRow)
{
if(Status=="Posted")
{
LinkButton lbtn2 = (LinkButton)e.Row.FindControl("LinkButton2");
lbtn2.Visible = false;
}
if(Status=="Publish")
{
LinkButton lbtn1 = (LinkButton)e.Row.FindControl("LinkButton1");
lbtn1.Visible = false;
}
}
}
I know this is old but I was looking for the same thing; I was able to do it this way also and posting just for reference:
This is in a listview but should be same for gridview:
<asp:LinkButton ID="btnForms" runat="server" Text='MyButton' CommandName="MyCommandName"
Style='<%#GetHiddenTag(Eval("Status").ToString())%>' CommandArgument='<%#Eval("Status")%>' OnClick="MyOnClickCodeBehind" />
Codebehind:
public string GetHiddenTag(string Status)
{
if (Status.ToLower() == "completed")
return "visibility:hidden;";
return "visibility:visible;";
}

GridView: convert data before bindind to GridView

My GridView using following data binding syntax to bind data.
I hope do data conversion before it is really bound to GridView. For example, if the VendorID int property (I use a List as data source) is 0, I wish showing empty string on that field.
What kind of event I could exploit? And if you could suggest any code sample?
Thanks a lot.
<asp:TemplateField HeaderText="Vendor ID">
<ItemStyle Width="1%" BorderColor="#efefef" BorderWidth="1px" />
<ItemTemplate>
<asp:Label runat="server" ID="lblNo" Text='<%# Bind("VendorID") %>'></asp:Label>
</ItemTemplate>
<HeaderStyle BorderColor="#efefef" />
</asp:TemplateField>
Gridview's RowDataBound Event can be use for this.
void GridView1_RowDataBound(Object sender, GridViewRowEventArgs e)
{
if(e.Row.RowType == DataControlRowType.DataRow)
{
// do wat ever u like to do here.
// formating, checking, changing data, etc....
e.Row.Cells[1].Text = "<i>" + e.Row.Cells[1].Text + "</i>";
}
}
hi you can use following code in RowDataboundEvent
if (DataBinder.Eval(args.Row.DataItem, "VendorID").ToString() == "0")
{
((Label)args.Row.FindControl("lblNo")).Text = "";
}
else
{
((Label)args.Row.FindControl("lblNo")).Text = DataBinder.Eval(args.Row.DataItem, "VendorID").ToString();
}

Resources