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

RowIndex always -1 using Unbound Grid

3 Answers 47 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Stephen Breytenbach
Top achievements
Rank 1
Stephen Breytenbach asked on 04 May 2011, 01:15 PM
Hi Julian,

as requested, here is the code for setting up my Master and Child and populating, unbound data (please let me know if you need more info)

I Really need to be able to access the RowIndex Value

thanks

Regards

Steve

       void CreateUnboundHierarchy()  
        
  
                this.radGridView.MasterTemplate.Reset(); 
                SetupMasterForUnbound();
                GridViewTemplate ChildTemplate = SetupChildForUnbound();
  
                this.radGridView.Templates.Add(ChildTemplate);
                this.radGridView.CreateCell += new GridViewCreateCellEventHandler(radGridView_CreateCell); 
  
  
  
                //--
                ChildTemplate.HierarchyDataProvider = new GridViewEventDataProvider(ChildTemplate);
                //this.radGridView.RowSourceNeeded += new GridViewRowSourceNeededEventHandler(radGridView_RowSourceNeeded);
                this.radGridView.UserAddingRow += new GridViewRowCancelEventHandler(radGridView_UserAddingRow);
                LoadDataForUnbound();
        }
        void SetupMasterForUnbound() 
        {
            this.radGridView.AllowAutoSizeColumns = true; 
            this.radGridView.MasterTemplate.AllowAddNewRow = false;
            this.radGridView.MasterTemplate.AllowDeleteRow = false;
            //-------------
            //--
            GridViewCheckBoxColumn chk1 = new GridViewCheckBoxColumn("SO");
            chk1.HeaderText = "SO";
            chk1.ReadOnly = true;
            this.radGridView.MasterTemplate.Columns.Add(chk1);
            //-------------
            //--
            GridViewTextBoxColumn textColumn1 = new GridViewTextBoxColumn("ChargeCode");
            textColumn1.HeaderText = "ChargeCode";
            textColumn1.ReadOnly = true;
            this.radGridView.MasterTemplate.Columns.Add(textColumn1);
            //--
            GridViewTextBoxColumn textColumn2 = new GridViewTextBoxColumn("Description");
            textColumn2.HeaderText = "Description";
            textColumn2.ReadOnly = true;
            this.radGridView.MasterTemplate.Columns.Add(textColumn2); 
            //--
            GridViewDecimalColumn decimalColumn = new GridViewDecimalColumn("UnitCost");
            decimalColumn.HeaderText = "UnitCost";
            decimalColumn.FormatString = "{0:0.000}";
            decimalColumn.ReadOnly = true;
            this.radGridView.MasterTemplate.Columns.Add(decimalColumn);
            //--
            GridViewDecimalColumn decimalColumn1 = new GridViewDecimalColumn("Qty");
            decimalColumn1.HeaderText = "Qty";
            decimalColumn1.FormatString = "{0:0.000}";
            decimalColumn1.ReadOnly = true;
            this.radGridView.MasterTemplate.Columns.Add(decimalColumn1);
            //--
            GridViewDecimalColumn decimalColumn3 = new GridViewDecimalColumn("TotalCost");
            decimalColumn3.HeaderText = "TotalCost";
            decimalColumn3.FormatString = "{0:0.000}";
            decimalColumn3.ReadOnly = true;
            this.radGridView.MasterTemplate.Columns.Add(decimalColumn3);
            //--
            //--
            GridViewCheckBoxColumn chk2 = new GridViewCheckBoxColumn("Charge");
            chk2.HeaderText = "Charge";
            chk2.ReadOnly = true;
            this.radGridView.MasterTemplate.Columns.Add(chk2);
            //--
            GridViewCheckBoxColumn chk3 = new GridViewCheckBoxColumn("Checked");
            chk3.HeaderText = "Checked";
            chk3.ReadOnly = true;
            this.radGridView.MasterTemplate.Columns.Add(chk3);
            //--
            this.radGridView.MasterTemplate.AutoSizeColumnsMode = GridViewAutoSizeColumnsMode.Fill;
            //----------------------------
  
  
        }
        private GridViewTemplate SetupChildForUnbound()
        {
            GridViewTemplate template = new GridViewTemplate(); 
            template.AllowAddNewRow = true;
            template.AllowDeleteRow = false;
            //---------------------------------------------------------------
            //---------------------------------------------------------------
            GridViewTextBoxColumn editButtonColumn = new GridViewTextBoxColumn();
            editButtonColumn.Name = "Edit";
            editButtonColumn.FieldName = "Edit";
            editButtonColumn.ReadOnly = true;
            editButtonColumn.HeaderText = "Edit";
            editButtonColumn.Width = 100;
            template.Columns.Add(editButtonColumn); 
            //-----------------------------------------------------------------
//            template.Columns.Add(new GridViewCheckBoxColumn("SO"));
            GridViewDecimalColumn Num = new GridViewDecimalColumn("Billable");
            Num.HeaderText = "Billable";
            Num.ReadOnly = true;
            Num.IsVisible = false;
            template.Columns.Add(Num);
            //--
            GridViewCheckBoxColumn chk1 = new GridViewCheckBoxColumn("SO");
            chk1.HeaderText = "SO";
            chk1.ReadOnly = true;
            template.Columns.Add(chk1);         
            //--
            GridViewTextBoxColumn textColumn1 = new GridViewTextBoxColumn("ChargeCode");
            textColumn1.HeaderText = "ChargeCode";
            textColumn1.ReadOnly = true;
            template.Columns.Add(textColumn1);
            //--
            GridViewTextBoxColumn textColumn2 = new GridViewTextBoxColumn("Description");
            textColumn2.HeaderText = "Description";
            textColumn2.ReadOnly = true;
            template.Columns.Add(textColumn2);
            //--
            GridViewTextBoxColumn textColumn3 = new GridViewTextBoxColumn("Barcode");
            textColumn3.HeaderText = "Barcode";
            textColumn3.ReadOnly = true;
            template.Columns.Add(textColumn3);
            //--
            GridViewTextBoxColumn textColumn4 = new GridViewTextBoxColumn("Dept");
            textColumn4.HeaderText = "Dept";
            textColumn4.ReadOnly = true;
            template.Columns.Add(textColumn4);
            //--
            GridViewDateTimeColumn Date1 = new GridViewDateTimeColumn("Date");
            Date1.HeaderText = "Date";
            Date1.ReadOnly = true;
            Date1.FormatString = "{0:d}";
            template.Columns.Add(Date1);
            //--            
            GridViewTextBoxColumn textColumn5 = new GridViewTextBoxColumn("Site");
            textColumn5.HeaderText = "Site";
            textColumn5.ReadOnly = true;
            template.Columns.Add(textColumn5);
            //--
            GridViewDecimalColumn decimalColumn1 = new GridViewDecimalColumn("Qty");
            decimalColumn1.HeaderText = "Qty";
            decimalColumn1.FormatString = "{0:0.000}";
            decimalColumn1.ReadOnly = true;
            template.Columns.Add(decimalColumn1);
            //--
            GridViewDecimalColumn decimalColumn = new GridViewDecimalColumn("UnitCost");
            decimalColumn.HeaderText = "UnitCost";
            decimalColumn.FormatString = "{0:0.000}";
            decimalColumn.ReadOnly = true;
            template.Columns.Add(decimalColumn);
            //--
            GridViewDecimalColumn decimalColumn3 = new GridViewDecimalColumn("TotalCost");
            decimalColumn3.HeaderText = "TotalCost";
            decimalColumn3.ReadOnly = true;
            decimalColumn3.FormatString = "{0:0.000}";
            template.Columns.Add(decimalColumn3);
            //--
            GridViewCheckBoxColumn chk2 = new GridViewCheckBoxColumn("Charge");
            chk2.HeaderText = "Charge";
            chk2.ReadOnly = true;
            template.Columns.Add(chk2);
            //--
            GridViewCheckBoxColumn chk3 = new GridViewCheckBoxColumn("Checked");
            chk3.HeaderText = "Checked";
            chk3.ReadOnly = true;
            template.Columns.Add(chk3);
            //--
            template.AutoSizeColumnsMode = GridViewAutoSizeColumnsMode.Fill;
            return template;
        }
        void LoadDataForUnbound() 
        {
            using (this.radGridView.DeferRefresh())
            {
                for (int i = 0; i < dts_DWBillDataSet.sps_BillableGrouped.Count; i++)
                {
                    dts_DWBillDataSet.sps_BillableGroupedRow row = dts_DWBillDataSet.sps_BillableGrouped[i];
                    this.radGridView.MasterTemplate.Rows.Add(row.SO, row.ChargeCode, row.Description, row.UnitCost, row.Qty, row.TotalCost,row.Charge,row.Checked);
                }
            }
        }

3 Answers, 1 is accepted

Sort by
0
Stephen Breytenbach
Top achievements
Rank 1
answered on 04 May 2011, 02:12 PM
Hi Julian,

would the correct approach be:

 

 

e.Row.Index

?
thanks
Regards
steve

 

0
Julian Benkov
Telerik team
answered on 09 May 2011, 04:12 PM
Hi Stephen,

I reviewed your code and all your previous posts. Please keep in mind that if you want to use the RowIndex of a row in hierarchy mode, you must get the Rowindex in context of its Parent row. Here is a sample of this:

void radGridView1_CellBeginEdit(object sender, GridViewCellCancelEventArgs e)
{
    int index = -1;
    if (e.Row.Parent != null)
    {
        index = e.Row.Parent.ChildRows.IndexOf(e.Row);
    }
 
    if (index < 0)
    {
        index = e.Row.ViewTemplate.ChildRows.IndexOf(e.Row);
    }
 
    if (index > -1 && e.Column.Name == "Barcode")
    {
        e.Cancel = true;
    }
 
    if (index > -1 && e.Column.Name == "Date")
    {
        e.Cancel = true;
    }
}

If you continue to experience the same issues, please send us a simple project with all API and events used to find the best solution for your scenario.

Thank you for your cooperation.

Best wishes,
Julian Benkov
the Telerik team
Q1’11 SP1 of RadControls for WinForms is available for download; also available is the Q2'11 Roadmap for Telerik Windows Forms controls.
0
Stephen Breytenbach
Top achievements
Rank 1
answered on 09 May 2011, 10:22 PM
Hi Julian,

Thank you for your assistance, it is much appreciated

regards

steve
Tags
GridView
Asked by
Stephen Breytenbach
Top achievements
Rank 1
Answers by
Stephen Breytenbach
Top achievements
Rank 1
Julian Benkov
Telerik team
Share this question
or