This is a migrated thread and some comments may be shown as answers.
Changing cell type?
3 Answers 37 Views
This is a migrated thread and some comments may be shown as answers.
Randy Hompesch
Top achievements
Rank 1
Randy Hompesch asked on 15 Jan 2016, 06:33 PM

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

 

 

3 Answers, 1 is accepted

Sort by
0
Dess | Tech Support Engineer, Sr.
Telerik team
answered on 18 Jan 2016, 12:49 PM
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
0
Randy Hompesch
Top achievements
Rank 1
answered on 18 Jan 2016, 04:14 PM

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

 

0
Dess | Tech Support Engineer, Sr.
Telerik team
answered on 19 Jan 2016, 10:03 AM
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
Asked by
Randy Hompesch
Top achievements
Rank 1
Answers by
Dess | Tech Support Engineer, Sr.
Telerik team
Randy Hompesch
Top achievements
Rank 1
Share this question
or