Conditionally display column

10 posts, 0 answers
  1. Laura
    Laura avatar
    131 posts
    Member since:
    Aug 2008

    Posted 12 Sep 2008 Link to this post

    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
  2. Shinu
    Shinu avatar
    17764 posts
    Member since:
    Mar 2007

    Posted 15 Sep 2008 Link to this post

    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.
  3. Iana Tsolova
    Admin
    Iana Tsolova avatar
    3388 posts

    Posted 15 Sep 2008 Link to this post

    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.
  4. Laura
    Laura avatar
    131 posts
    Member since:
    Aug 2008

    Posted 22 Sep 2008 Link to this post

    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;

  5. Laura
    Laura avatar
    131 posts
    Member since:
    Aug 2008

    Posted 22 Sep 2008 Link to this post

    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
                    {
                    }
                }
  6. Iana Tsolova
    Admin
    Iana Tsolova avatar
    3388 posts

    Posted 23 Sep 2008 Link to this post

    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.
  7. Laura
    Laura avatar
    131 posts
    Member since:
    Aug 2008

    Posted 23 Sep 2008 Link to this post

    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.
  8. Iana Tsolova
    Admin
    Iana Tsolova avatar
    3388 posts

    Posted 24 Sep 2008 Link to this post

    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.
  9. Kiran
    Kiran  avatar
    40 posts
    Member since:
    Oct 2018

    Posted 16 Oct 2018 Link to this post

    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.

  10. Tsvetomir
    Admin
    Tsvetomir avatar
    259 posts

    Posted 19 Oct 2018 Link to this post

    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.
Back to Top