This is a migrated thread and some comments may be shown as answers.

OnChildGridLoaded

4 Answers 35 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Orit
Top achievements
Rank 1
Orit asked on 07 Sep 2009, 11:21 AM
Hello

I want to build a hierarchy with a ComboBoxColumn
both the rows of the child grid
and the items of the ComboBoxColumn
need to be filtered
this is the way I work
and now OnChildGridLoaded I get a mistake 
for this line
var dataRowView = (DataRowView)((RadGridView)sender).ParentRow.DataContext;
Unable to cast object of type 'System.Data.DataRow' to type 'System.Data.DataRowView'.

 private void BindDataMainGrid()  
        {  
            try  
            {  
                GridViewTableDefinition BudgetTableDefinition = new GridViewTableDefinition();  
                BudgetTableDefinition.Relation = new PropertyRelation("Budget");  
                  
                GridViewTableDefinition SubBudgetTableDefinition = new GridViewTableDefinition();  
                SubBudgetTableDefinition.Relation = new PropertyRelation("SubBudget");  
                  
                 
                this.gvAccount.ItemsSource = this.GetData();              
            }  
            catch{}  
        }  
 
private void OnChildGridLoaded(object sender, RoutedEventArgs e)  
        {  
                try  
                {  
                    var dataRowView = (DataRowView)((RadGridView)sender).ParentRow.DataContext;  
                    var ds = dataRowView.DataView.Table.DataSet;  
                 
                    var childData = dataRowView.CreateChildView(ds.Relations["BudgetRelation"]);  
                    //סינון הערכים בגריד לפי סוג פעילות   
                    if (((ComboBoxValues.ComboValues)cmbActivityType.SelectedValue).Id != 0)  
                    {  
                        childData.RowFilter = "ActivityTypeID=" + ((ComboBoxValues.ComboValues)cmbActivityType.SelectedValue).Id;  
                        ((RadGridView)sender).ItemsSource = childData;  
                    }  
                    else  
                    {  
                        ((RadGridView)sender).ItemsSource = null;  
                        ((RadGridView)sender).ItemsSource = childData;  
                    }  
                        
                  
                    //סינון הערכים בתיבת הבחירה בעמודה של סוג פעילות  
                    if (Convert.ToInt32(dataRowView.Row.ItemArray[5]) == 1)  
                    {  
                        ((GridViewComboBoxColumn)((RadGridView)sender).Columns["ActivityTypeID"]).ItemsSource = ComboBoxValues.GetComboValues(dtActivityType.Select("ID < 2000"));  
                    }  
                    if (Convert.ToInt32(dataRowView.Row.ItemArray[5]) == 2)  
                    {  
                        ((GridViewComboBoxColumn)((RadGridView)sender).Columns["ActivityTypeID"]).ItemsSource = ComboBoxValues.GetComboValues(dtActivityType.Select("ID > 2000"));  
                    }  
                }  
                catch { }                        
         }  
 
        private void OnSubChildGridLoaded(object sender, RoutedEventArgs e)  
        {  
            try  
            {  
                var dataRowView = (DataRowView)((RadGridView)sender).ParentRow.DataContext;  
                var ds = dataRowView.DataView.Table.DataSet;  
                var childData = dataRowView.CreateChildView(ds.Relations["SubBudgetRelation"]);                     
                ((RadGridView)sender).ItemsSource = childData;  
                //סינון הערכים בגריד לפי סוג פעילות   
                if (((ComboBoxValues.ComboValues)cmbActivityType.SelectedValue).Id != 0)  
                {  
                    childData.RowFilter="ActivityTypeID=" + ((ComboBoxValues.ComboValues)cmbActivityType.SelectedValue).Id;  
                    ((RadGridView)sender).ItemsSource = childData;  
                }  
                else  
                {  
                    ((RadGridView)sender).ItemsSource = null;  
                    ((RadGridView)sender).ItemsSource = childData;  
                }  
                //סינון הערכים בתיבת הבחירה של סוג פעילות  
                if (Convert.ToInt32(dataRowView.Row.ItemArray[5]) == 1)  
                {  
                    ((GridViewComboBoxColumn)((RadGridView)sender).Columns["ActivityTypeID"]).ItemsSource = ComboBoxValues.GetComboValues(dtActivityType.Select("ID < 2000"));  
                }  
                if (Convert.ToInt32(dataRowView.Row.ItemArray[5]) == 2)  
                {  
                    ((GridViewComboBoxColumn)((RadGridView)sender).Columns["ActivityTypeID"]).ItemsSource = ComboBoxValues.GetComboValues(dtActivityType.Select("ID > 2000"));                    
                }  
            }  
            catch { }  
        }  
 
 

4 Answers, 1 is accepted

Sort by
0
Vlad
Telerik team
answered on 07 Sep 2009, 11:34 AM
Hi Orit,

It is not clear what is the return type of GetData() method - is it DataTable or DataView?

Kind regards,
Vlad
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
0
Orit
Top achievements
Rank 1
answered on 07 Sep 2009, 11:37 AM
GetDate returns DataSet
 private DataSet GetData()  
        {  
            try  
            {  
                dsb = ServiceData.GetBudgetsByOBS(iOBSID);  
                if (dsb != null)  
                {  
                    dsb.Tables["Budget"].Columns.Add("SumBudgetAllocation", typeof(float));  
                    dsb.Tables["Budget"].Columns.Add("SumBudgetRequest", typeof(float));  
                    dsb.Tables["Budget"].Columns.Add("SumOrderAllocation", typeof(float));  
                    dsb.Tables["Budget"].Columns.Add("SumOrderPaid", typeof(float));  
                   
                    foreach (DataRow dr in dsb.Tables["Budget"].Rows)  
                    {  
                        dr["RowStatus"] = 0;  
                        if (dsb.Tables["BudgetAllocation"].Select("id=" + Convert.ToInt32(dr["ID"])).GetLength(0) > 0)  
                        {  
                            dr["SumBudgetAllocation"] = Convert.ToDecimal(((LM_Model.dsBudget.BudgetAllocationRow)dsb.Tables["BudgetAllocation"].Select("id=" + Convert.ToInt32(dr["ID"]))[0]).ItemArray[1]);  
                        }  
                        if (dsb.Tables["BudgetRequest"].Select("BudgetId=" + Convert.ToInt32(dr["ID"])).GetLength(0) > 0)  
                        {  
                            dr["SumBudgetRequest"] = Convert.ToDecimal(((LM_Model.dsBudget.BudgetRequestRow)dsb.Tables["BudgetRequest"].Select("BudgetId=" + Convert.ToInt32(dr["ID"]))[0]).ItemArray[1]);  
                        }  
                        if (dsb.Tables["BudgetOrder"].Select("BudgetId=" + Convert.ToInt32(dr["ID"])).GetLength(0) > 0)  
                        {  
                            dr["SumOrderAllocation"] = Convert.ToDecimal(((LM_Model.dsBudget.BudgetOrderRow)dsb.Tables["BudgetOrder"].Select("BudgetId=" + Convert.ToInt32(dr["ID"]))[0]).ItemArray[1]);  
                        }  
                        if (dsb.Tables["BudgetOrderPaid"].Select("Id=" + Convert.ToInt32(dr["ID"])).GetLength(0) > 0)  
                        {  
                            dr["SumOrderPaid"] = Convert.ToDecimal(((LM_Model.dsBudget.BudgetOrderPaidRow)dsb.Tables["BudgetOrderPaid"].Select("Id=" + Convert.ToInt32(dr["ID"]))[0]).ItemArray[1]);  
                        }  
                    }  
                     
                    dsb.Tables["Budget"].Columns.Add("BalanceBudgetAllocation", typeof(float), "TotalBudget-SumBudgetAllocation");  
                    dsb.Tables["Budget"].Columns.Add("BalanceToPay", typeof(float), "SumBudgetRequest-SumOrderPaid");  
                      
                    //Build dsAcoount  
                    dsAcoount = new DataSet();  
                    DataView dv = new DataView(dsb.Tables["Budget"], "Type=1", "", DataViewRowState.CurrentRows);  
                    dsAcoount.Tables.Add((DataTable)dv.ToTable("Account"));  
                    dv = new DataView(dsb.Tables["Budget"], "Type=2", "", DataViewRowState.CurrentRows);  
                    dsAcoount.Tables.Add((DataTable)dv.ToTable("Budget"));  
                    dv = new DataView(dsb.Tables["Budget"], "Type=3", "", DataViewRowState.CurrentRows);  
                    dsAcoount.Tables.Add((DataTable)dv.ToTable("SubBudget"));  
                                    
                    LM_Model.clsEnums.BudgetType eVal = clsEnums.BudgetType.Investment;  
                    decimal dCurrentInvestment = Convert.ToDecimal(dsAcoount.Tables["Account"].Compute("SUM(TotalBudget)", "BudgetType=" + Convert.ToInt32(eVal)));  
                    txtbInvestmentSum.Text = dCurrentInvestment.ToString("#,##0.00");  
                    eVal = clsEnums.BudgetType.Continuous;  
                    decimal dCurrentContinues = Convert.ToDecimal(dsAcoount.Tables["Account"].Compute("SUM(TotalBudget)", "BudgetType=" + Convert.ToInt32(eVal)));  
                    txtbContinuesSum.Text = dCurrentContinues.ToString("#,##0.00");  
 
                    decimal dInvestmentBaseline = Convert.ToDecimal(dsb.Tables["BaselineBudget"].Rows[0]["InvestmentBaseline"]);  
                    decimal dContinuesBaseline = Convert.ToDecimal(dsb.Tables["BaselineBudget"].Rows[0]["ContinuesBaseline"]);  
 
                    decimal dI = (dCurrentInvestment / dInvestmentBaseline - 1) * 100;  
                    decimal dC = (dCurrentContinues / dContinuesBaseline - 1) * 100;  
 
                    txtInv.Text = dI.ToString("#.00");  
                    txtCon.Text = dC.ToString("#.00");  
 
                    DateTime dDate = (DateTime)dsb.Tables["BaselineBudget"].Rows[0]["Date"];  
                    txtBLDate.Text = dDate.ToString("dd/MM/yyyy");  
 
                    //Add Relations  
                    dsAcoount.Relations.Add("BudgetRelation",  
                    dsAcoount.Tables["Account"].Columns["Number"],  
                    dsAcoount.Tables["Budget"].Columns["ParentID"]);  
                    dsAcoount.Relations.Add("SubBudgetRelation",  
                    dsAcoount.Tables["Budget"].Columns["Number"],  
                    dsAcoount.Tables["SubBudget"].Columns["ParentID"]);  
                    return dsAcoount;  
                }  
            }  
            catch { }  
            return null;  
        } 
Thanks
0
Vlad
Telerik team
answered on 07 Sep 2009, 11:53 AM
Hi Orit,

In this case you need to cast to DataRow - DataRowView will work only if you bind the grid to DataView.

Best wishes,
Vlad
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
0
Orit
Top achievements
Rank 1
answered on 07 Sep 2009, 11:59 AM
But if I use DataRow I cant use the next rows

 

var

 

dataRowView = (DataRow)((RadGridView)sender).ParentRow.DataContext;

 

var

 

ds = dataRowView.DataView.Table.DataSet;

 

 

var childData = dataRowView.CreateChildView(ds.Relations["BudgetRelation"]);

 

Tags
GridView
Asked by
Orit
Top achievements
Rank 1
Answers by
Vlad
Telerik team
Orit
Top achievements
Rank 1
Share this question
or