This is a migrated thread and some comments may be shown as answers.
Conditionally display column
9 Answers 130 Views
This is a migrated thread and some comments may be shown as answers.
Laura
Top achievements
Rank 1
Laura asked on 12 Sep 2008, 02:09 PM
I have the user sending my page a querystring called price. It can be set to
either Yes, No, or Readonly.

How can I use this query string to conditionally show a price column
if the query string is yes, and to make it read only in the edit form if it
is readonly?

Thanks,
Laura

9 Answers, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 1
answered on 15 Sep 2008, 09:13 AM
Hi Laura,

Try the following code snippet to achieve the desired scenario.

CS:
 
    protected void RadGrid1_PreRender(object sender, EventArgs e) 
    { 
        foreach (GridColumn col in RadGrid1.MasterTableView.RenderColumns) 
        { 
            if (col.UniqueName=="FirstName") 
            { 
                GridBoundColumn bndcol = (GridBoundColumn)col; 
                //Check the query string 
                //if(price=="ReadOnly") 
                //{ 
                       bndcol.ReadOnly = true
                //} 
            } 
        } 
 
    } 


Thanks
Shinu.
0
Iana Tsolova
Telerik team
answered on 15 Sep 2008, 09:29 AM
Hello Laura,

You can handle the ColumnCreated server-side event of RadGrid to perform additional settings for each grid column.

Find more about RadGrid server-side API here.

Let us know if you need further directions.

Greetings,
Iana
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
Laura
Top achievements
Rank 1
answered on 22 Sep 2008, 04:29 PM
I am not getting this to work as I want.
If the user chooses readonly, I want the grid to show it, and also for
it to show in the edit form, but be non-editable. At this point,
it is not showing at all.

Also, I have an embedded grid that has prices for each item, and am using
a gridtemplate column for a radnumericTextBox for the user to enter the
price in the grid. When I use the above code, I get an error when trying
to convert from a gridbound column to a gridtemplate column using this statement.

GridBoundColumn bndcol = (GridBoundColumn)col;

0
Laura
Top achievements
Rank 1
answered on 22 Sep 2008, 06:05 PM
I wanted to add something to my question above. I have 2 scenarios where I
am using the querystring to determine if the price should show or not.
One is in my main grid - radgrid1, and the price is inside a gridmaskedcolumn
column so that it can be a properly formatted price.

The second place I am using the price is in an embedded grid, which does
not have an edit or insert command, and I made that price inside
a gridtemplatecolumn with a template item of gridnumeric column
so the user can just type the price in place in the column and it can be updated on
text change. I am trying to use this code, and as you see, I have to comment out
the readonly property becasue the template column can't have a readonly.


RadGrid RadGrid3 = (sender as RadGrid);
foreach (GridColumn col in RadGrid3.MasterTableView.RenderColumns)
        {
            if (col.UniqueName == "Price")
            {
                GridTemplateColumn bndcol = (GridTemplateColumn)col;
          //      GridBoundColumn bndcol = (GridBoundColumn)col;
                //Check the query string
                string showPrice = (Page.Request.QueryString["price"]);
                if (showPrice == "readonly")
                {
                //    bndcol.ReadOnly = true;
                    bndcol.Display = true;
                    bndcol.Visible = true;

                }
                if (showPrice == "no")
                {
            //        bndcol.ReadOnly = true;
                    bndcol.Visible = false;
                    bndcol.Display = false;
                }
                else
                {
                }
            }
0
Iana Tsolova
Telerik team
answered on 23 Sep 2008, 12:43 PM
Hello Laura,

If you want to change the ReadOnly property of the textbox in a certain row when the grid is in edit mode, you can hook up the ItemCreated or the ItemDataBound server event of the grid. There you need to find the particular textbox and set the corresponding properties as desired.

You can find more information how to access grid rows and columns in the following articles:
http://www.telerik.com/help/aspnet-ajax/grdaccessingcellsandrows.html
http://www.telerik.com/help/aspnet-ajax/grdsetfocusontextboxesineditcontrol.html
http://www.telerik.com/help/aspnet-ajax/grdcolumntypes.html
http://www.telerik.com/help/aspnet-ajax/grdusingcolumns.html

Let me know if you need further directions/help.

Kind regards,
Iana
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
Laura
Top achievements
Rank 1
answered on 23 Sep 2008, 03:07 PM
I cannot simply change the readonly property since my column is a template
column. I am accessing the column using the code above, and turn
visible=false, which turns off the browser column but it is still visible in the
edit form. Is there a way to access the edit template under the
template column to set it to readonly?

I was slightly successful in getting rid of the price in the form, but the
header of the form is still there, and I cannot get rid of it.
0
Iana Tsolova
Telerik team
answered on 24 Sep 2008, 07:53 AM
Hello Laura,

Try the following code to disable the textbox in the EditTemplate of GridTempleteColumn:

<telerik:GridTemplateColumn UniqueName="Price" HeaderText="Price">  
   <EditItemTemplate> 
      <telerik:RadNumericTextBox ID="RadNumericTextBox1" runat="server">  
      </telerik:RadNumericTextBox> 
   </EditItemTemplate> 
</telerik:GridTemplateColumn> 
protected void RadGrid1_ItemCreated(object sender, GridItemEventArgs e)  
{  
    if (e.Item is GridEditableItem && e.Item.IsInEditMode)  
    {  
        GridEditableItem item = e.Item as GridEditableItem;  
        RadNumericTextBox txtPrice =   
                  item["Price"].FindControl("RadNumericTextBox1"as RadNumericTextBox;  
        txtPrice.Enabled = false;  
    }  

Let me know if this helps.

Kind regards,
Iana
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
Kiran
Top achievements
Rank 2
answered on 16 Oct 2018, 12:27 PM

Hi,

I have a column named Manifest. If the column  Manifest brings in only one value for the SQL query then hide the column. If there are multiple Manifest values for the query then display the column. Can someone please help me with this scenario?

Thank you.

0
Tsvetomir
Telerik team
answered on 19 Oct 2018, 12:11 PM
Hi Kiran,

In order to understand whether there is only one record, you will have to traverse all items in the specific column of the RadGrid. Depending on the items count (items with values different than "null") hide the column. This could be done in the PreRender() event handler as follows:

protected void RadGrid1_PreRender(object sender, EventArgs e)
{
    var counter = 0; // used to store the items with values different than null
    var masterTable = RadGrid1.MasterTableView; // get the MasterTableView
    var columnToHide = masterTable.Columns.FindByUniqueName("Manifesto"); // Find the specific column by unique name
    var columnIndex = columnToHide.OrderIndex; // get the index of the column
    var rows = masterTable.Items; // get all rows of the MasterTableView
 
    foreach (GridDataItem item in rows)
    {
        var cellText = (item.Cells[columnIndex]).Text; // get the text of the specific column's cell
        if (cellText != null)
        {
            counter++;
        }
    }
 
    if (counter <= 1)
    {
        columnToHide.Visible = false; // hide the column if the data has less than 2 records
    }
}

If your requirements are different, elaborate in further details on your specific scenario. Attached you can find a sample project which demonstrates how to implement the aforementioned approach.

Kind regards,
Tsvetomir
Progress Telerik
Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
Tags
Grid
Asked by
Laura
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 1
Iana Tsolova
Telerik team
Laura
Top achievements
Rank 1
Kiran
Top achievements
Rank 2
Tsvetomir
Telerik team
Share this question
or