How to get the cell value of a cell that is in the template of the main grid.

1 Answer 77 Views
GridView
Sydney
Top achievements
Rank 1
Sydney asked on 04 May 2023, 08:22 PM

Hi, I am working on a program that uses queries that are hard coded into the program. Within the program, I have pages (radpageview) and then grids on the pages. Then each grid is programmed to load in data from the queries. 

I am running into trouble because my grids are based on the first grid... I have this hard coded into the SQL Query and it will look at a specific cell value to then run the query with that in the where statement. 

I need a cell value that is in the template, but I am having no luck. 

As you can see below, I can get the cells from my main grid... but not from the template.

Example to explain...

I set this: selectedOrder = grdCustPOListing.CurrentRow.Cells["Order Num"].Value.ToString();

So that selectedOrder can now be used in my query: 

"Select RMAH.RMANum as [RMA], RMAH.RMADate as [RMA Date], RMAD.RMALine as [RMA Line], RMAD.RefInvoiceLine as [Invoice], " +
                    "RMAD.OrderNum as [Order], RMAD.OrderLine as [Order Line], RMAD.ReturnReasonCode as [Reason], RMAD.Note, RMAD.PartNum as [Part], RMAD.LineDesc as [Description],  RMAD.ReturnQty as [Quantity] " +
                    "from erp.RMAHead RMAH with (nolock) " +
                    "inner join erp.RMADtl RMAD with(nolock) on RMAH.Company = RMAD.Company and RMAH.RMANum = RMAD.RMANum " +
                    "inner join erp.Customer C with(nolock) on RMAH.Company = C.Company and RMAH.CustNum = C.CustNum " +
                    "where RMAH.OpenRMA = 1 and RMAD.OrderNum like '%" + selectedOrder + "%' ";

1 Answer, 1 is accepted

Sort by
0
Dinko | Tech Support Engineer
Telerik team
answered on 09 May 2023, 08:42 AM

Hello Sydney,

Thank you for the provided details.

In your post, you mentioned that you want to get the rows inside a template. I am assuming that you have a hierarchy in your RadGridView. Generally, speaking the CurrentRow property will return the row no matter in which template is selected MasterTemplate or child template. So I am a bit confused regarding your scenario. May I ask you to elaborate more on how the RadGridView is set-up and from which template you want to get the desired cell value? You can send me images of what you have and mark the needed cell value. 

In general, you can get the child template from the MasterTemplate.Templates collection. Then you can iterate the Rows collection and use the Cells collection to get the cell value.

var templateFirstCellValue = this.radGridView1.MasterTemplate.Templates[0].Rows[0].Cells[0].Value;

The above code snippet will return 001 from the child template.

Regards,
Dinko | Tech Support Engineer
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.

Sydney
Top achievements
Rank 1
commented on 09 May 2023, 12:58 PM

My apologies for not being completely clear. I do have a hierarchy. From the child template, I need the value that is in the cell from the "Part Num" column of the row that is currently selected. See attached. 

Where I have run into problems is that after the templates in the snippet below, there is not a current row. 

var templateFirstCellValue = this.radGridView1.MasterTemplate.Templates[0].

 

Thank you,

Sydney

Dinko | Tech Support Engineer
Telerik team
commented on 10 May 2023, 08:10 AM

You can use the CurrentRow property of the radGridView1. Selecting a row will set the CurrentRow property of the control, no matter where the row is located. In a child template or in the master template. You can check the attached project. When you run the project, expand the first row. Then select the first child row. In the CurrentRowChanged change event, you can observe that the CurrentRow property will be equal to the row in the child template. I also show a RadMessageBox which compares the first cell values. Give this project a try and let me know how it goes.
Sydney
Top achievements
Rank 1
commented on 10 May 2023, 06:15 PM

When I add in a CurrentRowChanged event like the one in the example you sent me, it completely removes my child template... I am not too sure what is going on with that.

Anyway, I am not sure that it will get me what I am wanting to achieve... I see what you are saying about the CurrentRow working for both the master and child template. However, when the current row is in the child template - how do I get a cell value? I cannot use radGridView.MasterTemplate.Templates[0].Rows[0].Cells[0].Value; because it specifies the row. I need it to be current row and then I will specify the cell by column name. 

I am not sure if there is a disconnect/misunderstanding that I am having?

I currently have it so that I check to make sure that data has loaded into the grid (otherwise it will go to look for a value and nothing is there). 

Dinko | Tech Support Engineer
Telerik team
commented on 11 May 2023, 10:08 AM

You should be able to get the desired cell through the CurrentRow property of the RadGridView. You can directly use the Cells collection property of the CurrentRow after selecting the child row. Here is the code snippet from the project.

private void RadGridView1_CurrentRowChanged(object sender, CurrentRowChangedEventArgs e)
{
    var firstCellValue = this.radGridView1.CurrentRow.Cells[0].Value;
    if(this.radGridView1.MasterTemplate.Templates[0].Rows.Count > 0)
    {
        var templateFirstCellValue = this.radGridView1.MasterTemplate.Templates[0].Rows[0].Cells[0].Value;
        if (firstCellValue == templateFirstCellValue)
        {
            RadMessageBox.Show(firstCellValue + " / " + templateFirstCellValue);
        }
    }            
}

If this approach is not working, may I ask you to share some more details on why is not working? You can try checking which row the this.radGridView1.CurrentRow is pointing to.

 

Sydney
Top achievements
Rank 1
commented on 11 May 2023, 02:50 PM

I have added this event into my program. The error I get is in the attachment. From using break points, I have found that the child template has not loaded yet. Therefore, it throws that error that it is out of range. 

When I add in an if clause to see if the gridview has loaded (radgridview1.IsLoaded == true) , it only looks at the parent/main grid and I will still get the error because the main grid has loaded but the child has not yet. For this to not occur, I need a way to check if the child template has loaded...

Sydney
Top achievements
Rank 1
commented on 11 May 2023, 02:56 PM

I am not sure if I was completely clear, and I am new to programming so I want to try to help you help me as best I can. It looks like my program creates the parent/main template and then creates the child template. However, each time it goes through it is changing rows. So, it changes rows when the parent/main template is created, then it changes rows again when the child template is created, and then it changes again to have the current row go back to the parent/main template.

So when it creates the parent/main template it triggers the CurrentRowChanged event, but the child template has not been created and it cannot check to see if the row count is greater than 0, so it throws the error.

That is why I believe that I would need a way of checking that both the parent/main template and the child template have both been created/loaded. So that when it runs through the CurrentRowChanged event it will check and if they are not both there it will not continue. 
Sydney
Top achievements
Rank 1
commented on 11 May 2023, 03:32 PM

It seems that I have figured out how to get this to work...

I have the following if statement: 

if (grdCustPOListing.CurrentRow.HierarchyLevel == 1) 
            {
                selectedPart = grdCustPOListing.CurrentRow.Cells["Part Num"].Value.ToString();

            }

Dinko | Tech Support Engineer
Telerik team
commented on 15 May 2023, 12:45 PM

I am happy to hear that you have found a solution for your case and thank you for sharing it. It seems that in your case, the child template is still not loaded when trying to access the cells. Checking the HierarchyLevel is a good way to check which level is the current row.
Tags
GridView
Asked by
Sydney
Top achievements
Rank 1
Answers by
Dinko | Tech Support Engineer
Telerik team
Share this question
or