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

RadGridView Events

3 Answers 311 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Timothy Kelley
Top achievements
Rank 1
Timothy Kelley asked on 28 Sep 2011, 07:19 PM

I'm using the standard RadGridVeiw events to set various cell states such as enabled.  Everything works fine as long as the grid doesn't have a scroll bar.  Here is the code.

I'm finding that on this line

 

 

if (row.Cells[e.ColumnIndex].CellElement != null)  CellElement is null and is that last row in view when the grid loads.  So my code below only works for rows in view.

Let me state exactly what I'm trying to do. If nothing  else please help me find the correct solution.
I have a Winforms RadGridView table with GridViewCheckBoxes in a column.  When a user selects a checkbox in a row i want all the rest of the checkboxes in the other rows to be disabled.  When the user unchecks the checkboxed row I want all checkboxes in other rows to be enabled.
  It's simple, only one checkbox in a row can be selected.

 



 

 

public bool HandleCellValueChanged(RadGridView matrixRadGrid, GridViewCellEventArgs e, MatrixPickerPresenter matrixPresenter, RadGridView measureGrid)

 

{
 

bool IsValidSelection = false;

 

 

 

if (reportType == CustomReportTypes.Matrix || reportType == CustomReportTypes.MatrixRowWithHistory || reportType == CustomReportTypes.MatrixWithHistory)

 

{

 

 

if (e.Column.HeaderText == "Selected")

 

{

measureGrid.DataSource =

 

null;

 

 

 

if (matrixPresenter.Measures.Count > 0)

 

{

measureGrid.Enabled =

 

true;

 

measureGrid.DataSource = matrixPresenter.Measures;

 

 

try

 

{

 

 

var displayRow =

 

measureGrid.Rows.Where(row => ((

 

ReportMeasure)row.DataBoundItem).IsDisplayMeasure).FirstOrDefault();

 

 

 

if (displayRow != null)

 

{

 

 

// The Display Measure must be in view for the RowFormatting event to fire and the user needs to know what the Display Measure is at the start.

 

measureGrid.GridElement.ScrollToRow(displayRow);

}

}

 

 

catch

 

{

 

 

// Do nothing: No need to end the world just because the Display Measure isn't in view.

 

;

}

 

}

 

 

else

 

{

measureGrid.Enabled =

 

false;

 

}

IsValidSelection = measureGrid.Enabled;

 

 

var selectedReportItem = e.Row.DataBoundItem as ReportMatrix;

 

 

 

if (selectedReportItem != null)

 

{

 

 

bool enableOtherSelectedCheckboxes = true;

 

 

 

if (selectedReportItem.Selected)

 

{

enableOtherSelectedCheckboxes =

 

false;

 

IsValidSelection =

 

true;

 

}

 

 

 

for (int index = 0; index < matrixRadGrid.Rows.Count; index++)

 

{

 

 

var row = matrixRadGrid.Rows[index];

 

 

 

 

if (e.RowIndex == index)

 

{

 

 

if (!selectedReportItem.Selected)

 

{

selectedReportItem.MatrixParameter = (

 

int)MatrixParameterTypes.None;

 

e.Row.Cells[e.ColumnIndex + 1].Value = (

 

int)MatrixParameterTypes.None;

 

}

 

 

 

continue;

 

}

matrixRadGrid.GridElement.ScrollToRow(row);

 

 

if (row.Cells[e.ColumnIndex].CellElement != null)

 

{

row.Cells[e.ColumnIndex].CellElement.Enabled = enableOtherSelectedCheckboxes;

}

}

}

}

 

 

else if (e.Column.HeaderText == "Runtime Parameter")

 

{

 

 

bool setEnabled = true;

 

 

 

var reportMatrix = e.Row.DataBoundItem as ReportMatrix;

 

 

 

if (reportMatrix != null && reportMatrix.Selected)

 

{

setEnabled =

 

false;

 

IsValidSelection =

 

true;

 

measureGrid.Enabled =

 

true;

 

}

 

 

for (int index = 0; index < matrixRadGrid.Rows.Count; index++)

 

{

 

 

var row = matrixRadGrid.Rows[index];

 

 

 

if (e.RowIndex == index)

 

{

 

 

continue;

 

}

row.Cells[e.ColumnIndex].CellElement.Enabled = setEnabled;

}

}

}

 

 

return IsValidSelection;

 

}

}

3 Answers, 1 is accepted

Sort by
0
Emanuel Varga
Top achievements
Rank 1
answered on 30 Sep 2011, 05:44 AM
Hello,

Could you please post a screenshot of your grid with maybe some indications on what you are trying to achieve, because i could not really understand your specs...

Best Regards,
Emanuel Varga

Telerik WinForms MVP
0
Uday
Top achievements
Rank 1
answered on 30 Sep 2011, 11:19 AM
Hi Timothy,

You can try this code..

private void radGridView1_ValueChanged(object sender, EventArgs e)
        {           
            RadCheckBoxEditor check_editor = sender as RadCheckBoxEditor;
 
            if (check_editor == null)           
                return;
 
            if (check_editor.Value == "On")
            {               
                // You can make all other check boxes disabled here
            }
        }
0
Ivan Todorov
Telerik team
answered on 03 Oct 2011, 09:43 AM
Hi all,

Thank you for following up on this thread.

I think Timothy's problem is that he cannot access all the cell elements due to RadGridView's virtualization mechanism. In order to increase performance, only the data cells that are visible on the user area of the control have an associated CellElement. Therefore, instead of trying to access the CellElement directly, you could use the Tag property of the cells  to set a value that indicates whether it should be disabled. After this, you should subscribe to the CellFormatting event and enable or disable the cell depending on the value in the Tag property. The CellFormatting event is fired when a cell needs to be formatted before displaying, so it is guaranteed that the CellElement won't be null. The following code snippet demonstrates this:
// ...  your own logic
  
//instead of row.Cells[e.ColumnIndex].CellElement.Enabled = enableOtherSelectedCheckboxes;
row.Cells[e.ColumnIndex].Tag = enableOtherSelectedCheckboxes;
  
// ... your own logic
  
//on the cell formatting event we read the value in the Tag property and set the Enabled property of the CellElement being formatted
void radGridView1_CellFormatting(object sender, CellFormattingEventArgs e)
{
    if (e.Column is GridViewCheckBoxColumn && e.Row.Cells[e.ColumnIndex].Tag == false)
    {
        e.CellElement.Enabled = false;
    }
    else
    {
        e.CellElement.Enabled = true;
    }
}

I hope you find this useful.

Best wishes,
Ivan Todorov
the Telerik team

Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

Tags
GridView
Asked by
Timothy Kelley
Top achievements
Rank 1
Answers by
Emanuel Varga
Top achievements
Rank 1
Uday
Top achievements
Rank 1
Ivan Todorov
Telerik team
Share this question
or