RadGridView Events

4 posts, 0 answers
  1. Timothy Kelley
    Timothy Kelley avatar
    37 posts
    Member since:
    Oct 2005

    Posted 28 Sep 2011 Link to this post


    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;

     

    }

    }

  2. Emanuel Varga
    Emanuel Varga avatar
    1336 posts
    Member since:
    May 2010

    Posted 30 Sep 2011 Link to this post

    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
  3. UI for WinForms is Visual Studio 2017 Ready
  4. Uday
    Uday avatar
    7 posts
    Member since:
    Sep 2011

    Posted 30 Sep 2011 Link to this post

    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
                }
            }
  5. Ivan Todorov
    Admin
    Ivan Todorov avatar
    688 posts

    Posted 03 Oct 2011 Link to this post

    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 >>

Back to Top