determine column of cell

6 posts, 0 answers
  1. Dennis
    Dennis avatar
    13 posts
    Member since:
    Jul 2015

    Posted 03 Aug 2015 Link to this post

    HI 
    I’m trying to determine the column of a cell. 

    I have a _ItemDataBound event in my code where I change the color of the cell based of the value (.text) …no big deal.
    But now I need to determine in which column a cell is. Because I need to call a stored procedure to get some additional values, but I just need to do that for all rows of one column and not for all of them. 
    How can I determine if a cell is in the column where I have to call the procedure? 
    Thanks for any hint

    Best regards
    Dennis 


  2. David
    David avatar
    90 posts
    Member since:
    Jan 2011

    Posted 03 Aug 2015 in reply to Dennis Link to this post

    That would depend greatly upon what you are getting the "ItemDatabound" event from.  I suspect you are referring to a radGrid control however your notes are not very specific.  

     Assuming you are using a RadGrid, the item databound is more of a "row databound" event.  You get a GridDataItem object which points to object getting data bound.  You can cast this several ways one of which provides you easy access to the "Datakeyvalues" collection.  In code I have written, I use a "keyvalue" to provide a database id to lookup values with.  I then perform the related database query, find relevant values, and then adjust things based upon those values.  The  grid data item provides access to the various columns, of the row being data bound, via the unique name of the column.

     

    The documentation is actually helpful along these lines!

  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Dennis
    Dennis avatar
    13 posts
    Member since:
    Jul 2015

    Posted 04 Aug 2015 in reply to David Link to this post

    HI David
    Thanks for your reply.

    Sorry I forgot to write that I’m using a radgrid.

    As I understand it right, in your solution you do the lookup in every cell ?  My idea is to do it just in all cells for a defined column to avoid non necessary operations.

    I used already the key value of a row for other things, but in that case would need the Column as well.

    If there is no option do determine the column of a cell I can return from the DB a concatenated value like “CheckVal:15.4” . This value I can parse and if I get one with “CheckVal” in sting I could do a split on “:” and use the value “15.4”

    But I would prefer a nicer way :-)

    Thanks
    Regards
    Dennis 

  5. David
    David avatar
    90 posts
    Member since:
    Jan 2011

    Posted 04 Aug 2015 in reply to Dennis Link to this post

    You seem to be fixated on the "cell" versus the data row.  The ItemDataBound event, on the RadGrid, is on a per row basis, not a per-cell basis.  Here is a sample of code where I change the tooltips and background of individual cells on the current row based upon business rules.  This is done "on the fly" row by row as data is bound to the grids elements.

     This might give you some ideas.  I remind you that the fields I have identified as "GLAccount", and "ProjectCode" refer to the "UniqueName" values from grids markup.

    protected void RadPCardGridItemDataBound(object sender, GridItemEventArgs e)
    {
        try
        {
            if (e.Item is GridDataItem)
            {
                GridDataItem gdi = e.Item as GridDataItem;
                if (gdi != null)
                {
                    // PcardWFData is a business object holding the rows data.
                    // In the databound event the object is immediately available
                    // I could equally have performed a database action to retreive values
                    // if I had to.
                    PcardWFData wpd = gdi.DataItem as PcardWFData;
                    if (wpd != null)
                    {
                        // Set rows cells GLAccount, ProjectCode, VendorNumber, and InvoiceNumber
                        // backgrounds and tooltips based upon their valid state
                        if (wpd.WFPcardData.GLAccountISValid == false)
                        {
                            gdi["GLAccount"].BackColor = Color.Red;
                            gdi["GLAccount"].ToolTip = wpd.WFPcardData.GLAccountTooltip;
                        }
                        else
                        {
                            gdi["GLAccount"].ToolTip = String.Empty;
                        }
                        if (wpd.WFPcardData.ProjectCodeISValid == false)
                        {
                            gdi["ProjectCode"].BackColor = Color.Red;
                            gdi["ProjectCode"].ToolTip = wpd.WFPcardData.ProjectCodeTooltip;
                        }
                        else
                        {
                            gdi["ProjectCode"].ToolTip = String.Empty;
                        }
                        if (wpd.WFPcardData.VendorNumberISValid == false)
                        {
                            gdi["VendorNumber"].BackColor = Color.Red;
                            gdi["VendorNumber"].ToolTip = wpd.WFPcardData.VendorNumberTooltip;
                        }
                        else
                        {
                            gdi["VendorNumber"].ToolTip = String.Empty;
                        }
                        if (wpd.WFPcardData.InvoiceNumberISValid == false)
                        {
                            gdi["InvoiceNumber"].BackColor = Color.Red;
                            gdi["InvoiceNumber"].ToolTip = wpd.WFPcardData.InvoiceNumberTooltip;
                        }
                        else
                        {
                            gdi["InvoiceNumber"].ToolTip = String.Empty;
                        }
                    }
                }
            }
        }
        catch (Exception ex)
        {
            // document error using private class library
            U.Errors.HandleError("IEP-020003",
                "PCards - RadPCardGridItemDataBound",
                "Internal Error", ex.Message, ex);
        }
    }

  6. Eyup
    Admin
    Eyup avatar
    3006 posts

    Posted 06 Aug 2015 Link to this post

    Hello Guys,

    @David, thanks for the suggestions and for sharing your specific approach.

    @Dennis, you can examine the following article, which demonstrates how you can access an individual cell using the UniqueName of a column:
    http://docs.telerik.com/devtools/aspnet-ajax/controls/grid/rows/accessing-cells-and-rows


    The reverse action, i.e. determining the column name using the cell element, can be achieved as follows:
    TableCell cell = e.Item.Cells[2];
    if (cell is GridTableCell)
    {
        GridTableCell dataCell = cell as GridTableCell;
        string columnName = dataCell.Column.UniqueName;
    }

    But I think there are very limited practical uses of this approach.

    Hope this information helps.

    Regards,
    Eyup
    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
  7. Dennis
    Dennis avatar
    13 posts
    Member since:
    Jul 2015

    Posted 06 Aug 2015 Link to this post

    Hi David, Hi Eyup

     thanks for your help.

    i was thinking the wrong way. it works fine with the GridDataItem for each row.

     thanks a lot

     cheers

    Dennis

Back to Top
UI for ASP.NET Ajax is Ready for VS 2017