Changing cell type?

4 posts, 0 answers
  1. Randy Hompesch
    Randy Hompesch avatar
    44 posts
    Member since:
    Nov 2008

    Posted 15 Jan Link to this post

    I'm sure this question has been asked a 1000 times, but this will make 1001. I have a RadGridView and for the purposes of this discussion let make it a single column grid. I set the columntype to GridViewCommandColumn. Let's pretend I have 5 rows of data. On even numbered rows I DON'T want a button or any other editor/control. I just want a readonly bit of text to show (or no text at all for that matter). Basically, I want to change the even numbered rows/cells to a different cell type. In this case I want a readonly textbox or something similar. More generically, I want to know how to change individual cells at both design and runtime. Is this doable? If so, I could sure use some guidance.

    Thanks ... Ed

     

     

  2. Dess
    Admin
    Dess avatar
    1601 posts

    Posted 18 Jan Link to this post

    Hello Ed,

    Thank you for writing.

    You can use the CellFormatting event and hide the GridCommandCellElement.CommandButton for the desired rows and display the cell value as text:
    public Form1()
    {
        InitializeComponent();
     
        GridViewCommandColumn commandColumn = new GridViewCommandColumn("CommandColumn");
        commandColumn.UseDefaultText = false;
        radGridView1.MasterTemplate.Columns.Add(commandColumn);
        this.radGridView1.AutoSizeColumnsMode = GridViewAutoSizeColumnsMode.Fill;
        for (int i = 0; i < 10; i++)
        {
            this.radGridView1.Rows.Add("Data" + i);
        }
    }
     
    private void radGridView1_CellFormatting(object sender, CellFormattingEventArgs e)
    {
        if (e.Row is GridViewDataRowInfo)
        {
            GridCommandCellElement commandCell = e.CellElement as GridCommandCellElement;
            if (commandCell != null)
            {
                if (e.RowIndex % 2 == 0)
                {
                    e.CellElement.Text = commandCell.CommandButton.Text;
                    commandCell.CommandButton.Visibility = Telerik.WinControls.ElementVisibility.Collapsed;
                }
                else
                {
                    e.CellElement.Text = string.Empty;
                    commandCell.CommandButton.Visibility = Telerik.WinControls.ElementVisibility.Visible;
                }
            }
        }
    }

    If you will have a single column grid only, it is recommended to use RadDataEntry or RadDataLayout control. Thus, you can obtain different editors for each row.

    I hope this information helps. Should you have further questions I would be glad to help.
     
    Regards,
    Dess
    Telerik
    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 Feedback Portal and vote to affect the priority of the items
  3. UI for WinForms is Visual Studio 2017 Ready
  4. Randy Hompesch
    Randy Hompesch avatar
    44 posts
    Member since:
    Nov 2008

    Posted 18 Jan in reply to Dess Link to this post

    Hi,

    I'm getting closer to what I need with your help! Thanks. In the CellFormatting event there is a CellFormattingEventArgs e. e has a CellElement property. How can I get the CellElement for grd.Rows[e.RowIndex].Cells["MyFavoriteCell"]? What I am trying to do, is to look at the value of a cell and if it has a value that I am looking for, I want to hide all the other cells in the row. Can you get me going in the right direction?

    Thanks ... Ed

     

  5. Dess
    Admin
    Dess avatar
    1601 posts

    Posted 19 Jan Link to this post

    Hello Ed,

    Thank you for writing back. 

    You can get the value by using the CellFormattingEventArgs.CellElement.Value property. Thus, you can determine how to customize the cell. Note that RadGridView uses visual cell elements to display only the currently visible data and these visual cell elements are being reused when scrolling for example. In order to indicate that a certain element in the cell should not be visible, you can use the GridViewCellInfo.Tag property to store any useful information that can be used later in the CellFormatting event.
     
    I hope this information helps. If you have any additional questions, please let me know.

    Regards,
    Dess
    Telerik
    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 Feedback Portal and vote to affect the priority of the items
Back to Top