7 Answers, 1 is accepted
In order to achieve this try the following code snippet in ItemDataBound event.
C#:
protected
void
RadGrid1_ItemDataBound(
object
sender, GridItemEventArgs e)
{
if
(e.Item
is
GridDataItem)
{
GridDataItem item = (GridDataItem)e.Item;
if
(item.GetDataKeyValue(
"EmployeeID"
).ToString() ==
"4"
)
//set your condition for hiding the row
{
item.Display =
false
;
//hide the row
}
}
}
Thanks,
Princy.
Hi there. How do you hide the rows by setting conditions to a non-data key value columns? For example if I set condition to data key value "CustomerID", it works but If I set data key value to "CompanyName", I can't hide the row. I would like to hide the row based on the value of "CompanyName" instead of "CustomerId".
This works:
protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
{
if (e.Item is GridDataItem && e.Item.OwnerTableView.Name == "Master")
{
GridDataItem item = (GridDataItem)e.Item;
if (item.GetDataKeyValue("CustomerID").ToString().Substring(0, 1) == "A")
{
item.Display =
false; //hide the row
}
}
}
This does not work..
protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
{
if (e.Item is GridDataItem && e.Item.OwnerTableView.Name == "Master")
{
GridDataItem item = (GridDataItem)e.Item;
if (item.GetDataKeyValue("CompanyName").ToString().Substring(0, 1) == "A")
{
item.Display =
false; //hide the row
}
}
}
Thanks
GC_0620
Please disregard my earlier thread. It is solved. Correct syntex should be below because CompanyName is not a GetDataKeyValue.
Thanks Princy and others.
GC_0620
if (e.Item is GridDataItem && e.Item.OwnerTableView.Name == "Master")
{
GridDataItem item = (GridDataItem)e.Item;
if (item["CompanyName"].Text.ToString().Substring(0, 1) == "A")
{
item.Display = false; //hide the row
}
}
One option is you can access the cell value by using ColumnUniqueName'. Please refer the following documentation
Accessing cells and rows
Another option to get the data for the column 'CompanyName' is by adding the field name to the DataKeyNames property.
ASPX:
<
MasterTableView
DataKeyNames
=
"CompanyName"
. . . >
Thanks,
Princy.
I am doing my program in silverlight 5. Unfortunately, i cant seem to find the namespace GridItemEventArgs, or ItemDataBound event, or event the GridDataItem.
I'm currently using Telerik.Windows.Controls.GridView.RowLoadedEventArgs e, however, when i used
e.Row.Visibility = System.Windows.Visibility.Collapsed;
the row is not visible but it leaves a row of blank space.How can i hide it without leaving the white blank space?
Thanks..
The mentioned namespace and events are available only for the RadGrid control for ASP .NET Ajax. Note that in case you have any issues with the RadGridView control for Winforms you should post you question in the Winforms forums.
Regards,
Maria Ilieva
Telerik
Hi Princy,
I've tried everything but cannot get my row to hide. The database table is named Archived and I want to be able to hide it from view if it is Archived. Please see code below. Thank you.
public partial class ProjectGridView : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
bindParentData();
}
private void bindParentData()
{
rgResults.DataSource = Status.GetStatesByType(StatusType.Project);
rgResults.DataBind();
}
//public void BindData(int id)
//{
//}
protected void rgResults_ItemDataBound(object sender, GridItemEventArgs e)
{
//throw new NotImplementedException();
if ((e.Item is GridDataItem) && (e.Item.OwnerTableView.Name == ""))
{
var item = (GridDataItem)e.Item;
var lbl = (Label)item.FindControl("lblStatus");
var szStatus = item.GetDataKeyValue("Value").ToString();
var status = (StatusValue)Enum.Parse(typeof(StatusValue), szStatus);
var cnt = Project.GetProjectsByUser(Page.User.Identity.Name, null, status, (string.IsNullOrEmpty(txtProjectFilter.Text) ? null : txtProjectFilter.Text) ).Count;
lbl.Text = szStatus + " (" + cnt.ToString() + ")";
}
if ((e.Item is GridDataItem) && (e.Item.OwnerTableView.Name == "ctrlProject"))
{
GridDataItem item = (GridDataItem)e.Item;
var hf = (ProjectStatusControl)item["Proj"].FindControl("ctrlProjectStatus");
hf.BindData(Convert.ToInt32(item.OwnerTableView.ParentItem.GetDataKeyValue("ProjectID").ToString()));
}
if ((e.Item is GridDataItem) && (e.Item.OwnerTableView.Name == "ctrlProject"))
{
GridEditableItem item = (GridEditableItem)e.Item;
// GridDataItem item = (GridDataItem)e.Item;
//var archive = (ProjectStatusControl)item["Proj"].FindControl("ctrlProjectStatus");
//archive.BindData(Convert.ToInt32(item.OwnerTableView.ParentItem.GetDataKeyValue("Archive").ToString()));
if (item["Archive"].FindControl("ctrlProjectStatus").ToString() == "1") //set your condition for hiding the row
{
item.Display = false; //hide the row
}
}
if ((e.Item is GridDataItem) && (e.Item.OwnerTableView.Name == "ctrlTable"))
{
GridDataItem item = (GridDataItem)e.Item;
Project prj = item.DataItem as Project;
var hlSRF = (HyperLink)item.FindControl("hlSRF");
hlSRF.NavigateUrl = "~/ServiceRequest.aspx?pid=" + item.GetDataKeyValue("ProjectID").ToString();
hlSRF.Text = string.IsNullOrEmpty(prj.Title) ? "No Title Specified" : prj.Title;
var lbl = (Label)item.FindControl("lblSafetyOfficerApprovedDate");
var approvedDate = prj.SafetyOfficerApprovedDate;
if (prj.SampleData != null && prj.SampleData.IncludesRadMaterial)
lbl.Text = approvedDate == null ? "Pending" : String.Format("{0:d}", approvedDate);
else
lbl.Text = @"N/A";
lbl = (Label)item.FindControl("lblLabLeadApproval");
approvedDate = prj.LabLeadApprovedDate;
lbl.Text = approvedDate == null ? "Pending" : String.Format("{0:d}", approvedDate);
}
}
protected void rgResults_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
var dr = e.Row.DataItem as DataRowView;
if ((dr["Archived"]).ToString() == "1")
e.Row.Enabled = false;
}
}
protected void rgResults_DetailTableDataBind(object sender, GridDetailTableDataBindEventArgs e)
{
//throw new NotImplementedException();
GridDataItem item = e.DetailTableView.ParentItem;
switch (e.DetailTableView.Name)
{
case "ctrlTable":
var status = (StatusValue)Enum.Parse(typeof(StatusValue),item.GetDataKeyValue("Value").ToString());
var parentNodes = Project.GetProjectsByUser(Page.User.Identity.Name, null, status, (string.IsNullOrEmpty(txtProjectFilter.Text) ? null : txtProjectFilter.Text));
if (!string.IsNullOrEmpty(txtProjectFilter.Text))
parentNodes = parentNodes.Where(x => x.Title.ToLower().Contains(txtProjectFilter.Text.ToLower())).ToList();
e.DetailTableView.DataSource = parentNodes;
break;
case "ctrlProject":
var dtFool = new DataTable();
dtFool.Columns.Add("Fool");
dtFool.Rows.Add("");
e.DetailTableView.DataSource = dtFool;
break;
}
}