This is a migrated thread and some comments may be shown as answers.

row formatting

2 Answers 255 Views
GridView
This is a migrated thread and some comments may be shown as answers.
steve
Top achievements
Rank 1
steve asked on 08 Apr 2010, 07:22 PM
I am creating a gridview, columns and rows dynamically using .Rows.AddNew();.  My problem is when I try to set a row enable to false I get a null ref when trying to set the .VisualElement.Enabled = false;.  I think the property is null because the grid does not created the visualelement until run time (I think i read that somewhere).  However, that seems to be the only way to set the enable property.  If this is true, how can I set enable false for a row before the grid is rendered? I have tried using the RowFormatting event but it fires everytime the row has focus. The user needs to be able to change the stated of the property once the grid is rendered, but that calls the RowFormatting event and set it back to false.  I only want to set the enable property when loading the grid or by firing event to enable it later.

Stephen.

2 Answers, 1 is accepted

Sort by
0
Accepted
Jack
Telerik team
answered on 12 Apr 2010, 02:39 PM
Hi steve,

You are correct in your assumption. You can't access the VisaualElement because it is not yet created. However, you have to use the RowFormatting event because RadGridView reuses its visual elements and only the rows and cells that are currently visible have corresponding visual elements. You can use a hidden column to indicate whether the row is enabled or not. Please consider the sample below:

this.radGridView1.RowFormatting += new RowFormattingEventHandler(radGridView1_RowFormatting);
GridViewCheckBoxColumn hiddenColumn = new GridViewCheckBoxColumn("Enabled", "");
hiddenColumn.IsVisible = false;
hiddenColumn.VisibleInColumnChooser = false;
this.radGridView1.Columns.Add(hiddenColumn);
this.radGridView1.DataSource = table;
 
this.radGridView1.GridElement.BeginUpdate();
foreach (GridViewRowInfo rowInfo in this.radGridView1.Rows)
{
    rowInfo.Cells["Enabled"].Value = true;
}
this.radGridView1.GridElement.EndUpdate();
 
this.radGridView1.Rows[2].Cells["Enabled"].Value = false;
 
void radGridView1_RowFormatting(object sender, RowFormattingEventArgs e)
{
    if (e.RowElement.RowInfo.Cells["Enabled"].Value is bool)
    {
        e.RowElement.Enabled = (bool)e.RowElement.RowInfo.Cells["Enabled"].Value;
    }
    else
    {
        e.RowElement.Enabled = true;
    }
}

I hope this helps.

Regards,
Jack
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
0
steve
Top achievements
Rank 1
answered on 26 Apr 2010, 07:16 PM
That was it.  Works like a charm now.  Thank you so much!
Tags
GridView
Asked by
steve
Top achievements
Rank 1
Answers by
Jack
Telerik team
steve
Top achievements
Rank 1
Share this question
or