How to change "DefaultText" on a row level

2 Answers 88 Views
GridView
Kobus
Top achievements
Rank 1
Iron
Kobus asked on 05 Oct 2022, 04:00 AM

Hi,

 

I want to change the VALUE of the button button per row.

1) Look at this column to determine button value, (3) if tick I want to show "IFC->" and if not ticked I want to show "->IFC" ( For info : IFC stand for Interface Contract)

2) I also tried to add a column in the DATASOURCE. But struggle to make the field a button. This is also option I I can get this to be a button

 

               

    //*
                    //**********************************************************************************
                    //*
                    //*    The IFC Button
                    //*
                    //**********************************************************************************
                    //*
                    GridViewCommandColumn ifc_action = new GridViewCommandColumn();
                    ifc_action.HeaderText = "IFC Action";
                    ifc_action.DataType = typeof(string);
                    ifc_action.DefaultText = "->IFC";
                    ifc_action.UseDefaultText = true;
                    ifc_action.FieldName = "btn_ifc_action";
                    this.grd_db_imported_table.Columns.Add(ifc_action);


2 Answers, 1 is accepted

Sort by
0
Maria
Telerik team
answered on 07 Oct 2022, 01:49 PM

Hello Kobus,

Thank you for the image and the code snippet.

To change the value of the custom column you created you have to subscribe to the CellFormatting event in the RadGridView , which is used to access and change the style of the data cells. In addition, you don't need to add a column in the DataSource. I am sending you a code snippet below:

private void RadGridView1_CellFormatting(object sender, CellFormattingEventArgs e)
{
    if (e.CellElement.ColumnInfo is GridViewCommandColumn)
    {
        // This is how we get the RadButtonElement instance from the cell
        RadButtonElement button = (RadButtonElement)e.CellElement.Children[0];
        if (e.CellElement.RowInfo.Cells["MyBoolean"].Value != null)
        {
            bool isChecked = (bool)e.CellElement.RowInfo.Cells["MyBoolean"].Value;
            if (isChecked)
            {
                button.Text = "IFC->";
            }
            else
            {
                button.Text = "->IFC";
            }
        }
    }
}

You can find more information at this link: Formatting cells

I am sending you an example project as well.

I hope this works for you.

Regards,

Maria
Progress Telerik

Love the Telerik and Kendo UI products and believe more people should try them? Invite a fellow developer to become a Progress customer and each of you can get a $50 Amazon gift voucher.

Kobus
Top achievements
Rank 1
Iron
commented on 07 Oct 2022, 07:41 PM

Hi Maria

Super nice.. This works 99%

The ONLY thing is it override also the 2nd button

I tried

        RadButtonElement button = (RadButtonElement)e.CellElement.Children[0];

        RadButtonElement button02 = (RadButtonElement)e.CellElement.Children[1];

But it gives an error

Kobus
Top achievements
Rank 1
Iron
commented on 09 Oct 2022, 01:06 AM

I had to add a third column to toggle the mode

        private void grd_db_imported_table_CellFormatting(object sender, CellFormattingEventArgs e)
        {
            string m_method = "frm_generator_import_tables.grd_db_imported_table_CellFormatting";
            try
            {
                if (e.CellElement.ColumnInfo is GridViewCommandColumn)
                {
                    // This is how we get the RadButtonElement instance from the cell
                    RadButtonElement button = (RadButtonElement)e.CellElement.Children[0];
                    if (e.CellElement.RowInfo.Cells["table_exist"].Value != null)
                    {
                        bool isChecked = (bool)e.CellElement.RowInfo.Cells["table_exist"].Value;
                        if (isChecked)
                        {
                            button.Text = "IFC->";
                            //button.Text = "->IFC";
                        }
                        else
                        {
                            button.Text = "->IFC";
                            //button.Text = "IFC->";
                        }
                    }
                    RadButtonElement button02 = (RadButtonElement)e.CellElement.Children[1];
                    button02.Text = "Show Fields";
                    RadButtonElement button03 = (RadButtonElement)e.CellElement.Children[2];
                    button03.Text = "Mode";
                }
            }
            catch (Exception ex)
            {

                lb.clsLogger.WriteLog("Error in : " + m_method + " : " + ex.Message);
            }
        }     
Here is the FORMAT ( Buttons at the end)
     private void format_grd_db_imported_table()
        {
            string m_method = "frmDB_db_imported_table.format_grd_db_imported_table";
            try
            {
                if (txt_format_db_imported_table.Text == "0")
                {
                    foreach (GridViewColumn col in this.grd_db_imported_table.Columns)
                    {
                        if (col.Name == "db_imported_schema")
                        {
                            col.IsVisible = false;
                            col.BestFit();
                            col.MaxWidth = 300;
                            col.HeaderText = "DB Imported Schema";
                        }
                        if (col.Name == "db_imported_table_id")
                        {
                            col.IsVisible = false;
                            col.BestFit();
                            col.MaxWidth = 300;
                            col.HeaderText = "Db Imported Table ID";
                        }

                        if (col.Name == "db_imported_schema_id")
                        {
                            col.IsVisible = false;
                            col.BestFit();
                            col.MaxWidth = 300;
                            col.HeaderText = "Db Imported Schema ID";
                        }
                        if (col.Name == "db_table")
                        {
                            col.IsVisible = true;
                            col.BestFit();
                            col.MaxWidth = 300;
                            col.HeaderText = "Table";
                        }
                        if (col.Name == "environment")
                        {
                            col.IsVisible = false;
                            col.BestFit();
                            col.MaxWidth = 300;
                            col.HeaderText = "Environment";
                        }
                        if (col.Name == "table_exist")
                        {
                            col.IsVisible = true;
                            col.BestFit();
                            col.MaxWidth = 300;
                            col.HeaderText = "Table In IFC";
                        }
                    }
                    //*
                    //**********************************************************************************
                    //*
                    //*    The IFC Button
                    //*
                    //**********************************************************************************
                    //*
                    GridViewCommandColumn ifc_action = new GridViewCommandColumn();
                    ifc_action.HeaderText = "IFC Action";
                    ifc_action.DataType = typeof(string);
                    ifc_action.DefaultText = "->IFC";
                    ifc_action.UseDefaultText = true;
                    ifc_action.FieldName = "btn_ifc_action";
                    this.grd_db_imported_table.Columns.Add(ifc_action);
                    //*
                    //**********************************************************************************
                    //*
                    //*    The show_fields Button
                    //*
                    //**********************************************************************************
                    //*
                    GridViewCommandColumn show_fields = new GridViewCommandColumn();
                    show_fields.HeaderText = "Show Fields";
                    show_fields.DataType = typeof(string);
                    show_fields.DefaultText = "show_fields";
                    show_fields.UseDefaultText = true;
                    show_fields.FieldName = "btn_show_fields";
                    this.grd_db_imported_table.Columns.Add(show_fields);
                    //*
                    //**********************************************************************************
                    //*
                    //*    The show_fields Button
                    //*
                    //**********************************************************************************
                    //*
                    GridViewCommandColumn mode = new GridViewCommandColumn();
                    mode.HeaderText = "Mode";
                    mode.DataType = typeof(string);
                    mode.DefaultText = "Mode";
                    mode.UseDefaultText = true;
                    mode.FieldName = "btn_mode";
                    this.grd_db_imported_table.Columns.Add(mode);


                }
                grd_db_imported_table.BestFitColumns();
                txt_format_db_imported_table.Text = "1";
            }
            catch (Exception ex)
            {
                lb.clsLogger.WriteLog("Error in : " + m_method + " : " + ex.Message);
            }
        }
0
Maria
Telerik team
answered on 10 Oct 2022, 11:52 AM

Hello Kobus,

Thank you for the code snippets again!

The CellFormatting event fires multiple times for every cell in the RadGridView. If you want to extract the button in the cell you can try this approach which gives you information for the exact cell from the column and row you want to access. I changed the code snippet a little bit so it doesn't show errors.

private void RadGridView1_CellFormatting(object sender, CellFormattingEventArgs e)
{
    GridCommandCellElement commandCell = e.CellElement as GridCommandCellElement;
    if (commandCell!=null)
    {
        if (e.CellElement.RowInfo.Cells["MyBoolean"].Value != null)
        {
                   
            bool isChecked = (bool)e.CellElement.RowInfo.Cells["MyBoolean"].Value;

            if (e.Column.HeaderText== "IFC Action")
            {
                if (isChecked)
                {
                    commandCell.CommandButton.Text = "IFC->";
                }
                else
                {
                    commandCell.CommandButton.Text = "->IFC";
                }
            }
            else if (e.Column.HeaderText == "Show Fields")
            {
                if (isChecked)
                {
                    commandCell.CommandButton.Text = "IFC->";
                }
                else
                {
                    commandCell.CommandButton.Text = "->IFC";
                }
            }
            else if (e.Column.HeaderText == "Mode")
            {
                if (isChecked)
                {
                    commandCell.CommandButton.Text = "IFC->";
                }
                else
                {
                    commandCell.CommandButton.Text = "->IFC";
                }
            }
        }
    }
}

Please give this approach a try. I hope it works, if not, please let me know.

Regards,
Maria
Progress Telerik

Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.

Tags
GridView
Asked by
Kobus
Top achievements
Rank 1
Iron
Answers by
Maria
Telerik team
Share this question
or