How to programmatically insert a row in a grid?


Userlevel 2
Hi Friends.I have the following screen. I need that when the screen loads (if it is a new record) the grid Alternates have a one row by default.

 

This is the way I have been working but it doesn't work.
using System;
using PX.Data;
using PX.Data.BQL.Fluent;
using System.Linq;

namespace Estimating
{
public class EstimateEntry : PXGraph<EstimateEntry, CEEstimate >
{

protected void CEEstimateAlternate_RowSelected(PXCache cache, PXRowSelectedEventArgs e)
{

var row = (CEEstimateAlternate)e.Row;

if (row == null)
return;

CEEstimateAlternate line = new CEEstimateAlternate();
line.Number=1;
line.Description = "Base Bid";
line.Type = "INCLUDED";
this.viewCEEstimateAlternates.Insert(line);

}
}
}

Aditional.

  • The header view is: viewCEEstimate.
  • The DAC header is: CEEstimate.
  • The grid view (Alternate) is: viewCEEstimateAlternates.
  • The DAC grid (Alternate) is: CEEstimateAlternate.
  • The structure of alternates is simple: Number (int), Description (Text), Type (Text-Combobox)
  • The screen is a customization project.

I only need when de screen loads in the grid alternates insert a row automatically.

 

Can you help me please.


12 replies

Userlevel 7
Badge +17

Hi @eddiedaco It is NOT recommended to write a insert statements in RowSelected event.
Since you want to insert the records into the grid, while page is loading you need to write a logic in viewDelagete() method.

 

Please find the sample example for view delegate.

public PXSelect<TempDAC> InsertRecords;  // View 

protected virtual IEnumerable insertRecords() //viewDelegate
{
TempDAC record = new TempDAC()
{
EmpName = "Test",
Address = "Address Details",
};
yield return InsertRecords.Insert(record);
}

Userlevel 2

@Naveen B   Thank you very much for the advice. I really appreciate your support.

 

I will test the code and write if I can replicate it successfully.

 

Thanks!!!

Userlevel 2

hi @Naveen B  i tested the code but the row not appears in the alternates grid. i paste my code. 

I have the question of where do I send to call the method (delegate) so that it works? (sorry, I have not worked with delegates).

 

using System;
using PX.Data;
using PX.Data.BQL.Fluent; //se agrega esta linea
using System.Linq;
using System.Collections;
using System.Collections.Generic;

namespace Estimating
{
public class EstimateEntry : PXGraph<EstimateEntry, CEEstimate >
{

public SelectFrom<CEEstimate>.View viewCEEstimate; //view and DAC header

public SelectFrom<CEEstimateAlternate>. Where<CEEstimateAlternate.estimateNbr.IsEqual<CEEstimate.estimateNbr.FromCurrent>>.OrderBy<CEEstimateAlternate.description.Asc>.View viewCEEstimateAlternates; //view and DAC Grid alternates


protected virtual IEnumerable insertRecords() //viewDelegate
{
CEEstimateAlternate record = new CEEstimateAlternate()
{
Number=1,
Description = "Base Bid",
Type = "INCLUDED"
};
yield return viewCEEstimateAlternates.Insert(record);
}
}
}

 

Userlevel 7
Badge +17

@eddiedaco  viewDelegate is wrong. I have modified your code, please check with this.

 

using System;
using PX.Data;
using PX.Data.BQL.Fluent; //se agrega esta linea
using System.Linq;
using System.Collections;
using System.Collections.Generic;

namespace Estimating
{
public class EstimateEntry : PXGraph<EstimateEntry, CEEstimate >
{

public SelectFrom<CEEstimate>.View viewCEEstimate; //view and DAC header

public SelectFrom<CEEstimateAlternate>. Where<CEEstimateAlternate.estimateNbr.IsEqual<CEEstimate.estimateNbr.FromCurrent>>.OrderBy<CEEstimateAlternate.description.Asc>.View ViewCEEstimateAlternates; //view and DAC Grid alternates


protected virtual IEnumerable viewCEEstimateAlternates() //viewDelegate
{
CEEstimateAlternate record = new CEEstimateAlternate()
{
Number=1,
Description = "Base Bid",
Type = "INCLUDED"
};
yield return ViewCEEstimateAlternates.Insert(record);
}
}
}

 

 

 

 

 

 

Userlevel 2

hI, @Naveen B  thanks for the answer. 

i make the changes and i have the following observations.

A) if i use:   protected virtual IEnumerable CEEstimateAlternate() //the DAC Class . I dont have error but in the screen the row in the grid alternates doesnt appears.

using System;
using PX.Data;
using PX.Data.BQL.Fluent; //se agrega esta linea
using System.Linq;
using System.Collections;
using System.Collections.Generic;

namespace Estimating
{
public class EstimateEntry : PXGraph<EstimateEntry, CEEstimate >
{

protected virtual IEnumerable CEEstimateAlternate() //viewDelegate
{
CEEstimateAlternate record = new CEEstimateAlternate()
{
Number=1,
Description = "Base Bid",
Type = "INCLUDED"
};
yield return viewCEEstimateAlternates.Insert(record);
}

 

B) if i use: protected virtual IEnumerable viewCEEstimateAlternates() // the view of Dac. I have this error in the line of mi declaration view.

Error: The type 'EstimateEntry' already contains a definition for 'viewCEEstimateAlternates'

 

the line where the error is.

    public SelectFrom<CEEstimateAlternate>.   Where<CEEstimateAlternate.estimateNbr.IsEqual<CEEstimate.estimateNbr.FromCurrent>>.OrderBy<CEEstimateAlternate.description.Asc>.View viewCEEstimateAlternates;

using System;
using PX.Data;
using PX.Data.BQL.Fluent; //se agrega esta linea
using System.Linq;
using System.Collections;
using System.Collections.Generic;

namespace Estimating
{
public class EstimateEntry : PXGraph<EstimateEntry, CEEstimate >
{

protected virtual IEnumerable viewCEEstimateAlternates() //viewDelegate
{
CEEstimateAlternate record = new CEEstimateAlternate()
{
Number=1,
Description = "Base Bid",
Type = "INCLUDED"
};
yield return viewCEEstimateAlternates.Insert(record);
}

 

thanks, for your support. 

 

 

Userlevel 7
Badge +17

Hi @eddiedaco  You have taken complete code from above which I have modified.

The view name starting letter I have given in Capital letter

public SelectFrom<CEEstimateAlternate>.   Where<CEEstimateAlternate.estimateNbr.IsEqual<CEEstimate.estimateNbr.FromCurrent>>.OrderBy<CEEstimateAlternate.description.Asc>.View ViewCEEstimateAlternates;

 

then viewDelegate should be start with small letter, just like below.

 protected virtual IEnumerable viewCEEstimateAlternates()   //viewDelegate
{
CEEstimateAlternate record = new CEEstimateAlternate()
{
Number=1,
Description = "Base Bid",
Type = "INCLUDED"
};
yield return viewCEEstimateAlternates.Insert(record);
}

 

Since you are using the both are in small letters, it is treating as a duplicate view hence you are getting that issue.

Userlevel 2

hi @Naveen B  thanks for the observation.

I made some changes but the initial row still does not appear in the alternating grid.

A) I change my view with the initial letter capital. And test de code that you wrote. And i got this error.

'EstimateEntry.viewCEEstimateAlternates()' is a method, which is not valid in the given context
using System;
using PX.Data;
using PX.Data.BQL.Fluent; //se agrega esta linea
using System.Linq;
using System.Collections;
using System.Collections.Generic;

namespace Estimating
{
public class EstimateEntry : PXGraph<EstimateEntry, CEEstimate >
{

public SelectFrom<CEEstimateAlternate>.
Where<CEEstimateAlternate.estimateNbr.IsEqual<CEEstimate.estimateNbr.FromCurrent>>.OrderBy<CEEstimateAlternate.description.Asc>.View ViewCEEstimateAlternates;

protected virtual IEnumerable viewCEEstimateAlternates() //viewDelegate
{
CEEstimateAlternate record = new CEEstimateAlternate()
{
Number=1,
Description = "Base Bid",
Type = "INCLUDED"
};
yield return viewCEEstimateAlternates.Insert(record); //line 21 error
}

 

B) If I change the method and return my view with a capital v, I have no errors but something strange happens on my screen.

 

using System;
using PX.Data;
using PX.Data.BQL.Fluent; //se agrega esta linea
using System.Linq;
using System.Collections;
using System.Collections.Generic;

namespace Estimating
{
public class EstimateEntry : PXGraph<EstimateEntry, CEEstimate >
{

public SelectFrom<CEEstimateAlternate>. Where<CEEstimateAlternate.estimateNbr.IsEqual<CEEstimate.estimateNbr.FromCurrent>>.OrderBy<CEEstimateAlternate.description.Asc>.View ViewCEEstimateAlternates;

protected virtual IEnumerable viewCEEstimateAlternates() //viewDelegate
{
CEEstimateAlternate record = new CEEstimateAlternate()
{
Number=1,
Description = "Base Bid",
Type = "INCLUDED"
};
yield return ViewCEEstimateAlternates.Insert(record);
}
Do you know what could be happening?

a lot of thanks for your help, 

Userlevel 7
Badge +17

Hi @eddiedaco  When you are inserting into cache, we need to use VIEW but NOT the VIEW DELEGATE

In the line no 21 you need to use capital V like below, if you small v you will get an error.

         yield return ViewCEEstimateAlternates.Insert(record);

 

Initial row blank: This will occurs only when we create a DAC without proper keys. Please provide Primary Key for the proper field and verify.

Userlevel 2

A lot of thanks @Naveen B  . I change the code and not have errors.

using System;
using PX.Data;
using PX.Data.BQL.Fluent; //se agrega esta linea
using System.Linq;
using System.Collections;
using System.Collections.Generic;

namespace Estimating
{
public class EstimateEntry : PXGraph<EstimateEntry, CEEstimate >
{


public SelectFrom<CEEstimateAlternate>. Where<CEEstimateAlternate.estimateNbr.IsEqual<CEEstimate.estimateNbr.FromCurrent>>.OrderBy<CEEstimateAlternate.description.Asc>.View ViewCEEstimateAlternates;

protected virtual IEnumerable viewCEEstimateAlternates()
{
CEEstimateAlternate record = new CEEstimateAlternate()
{
Number=1,
Description = "Base Bid",
Type = "INCLUDED"
};
yield return ViewCEEstimateAlternates.Insert(record);
}

I share with you mi DAC of CEEstimateAlternate. In this DAC i have 2 keys Estimate nbr and Id (Identity). I dont know what fields put in the delegate to show a “default” record in the grid Alternates.

using System;
using PX.Data;
using PX.Data.BQL.Fluent; //se agrega esta linea

namespace Estimating
{
[Serializable]
[PXCacheName("CEEstimateAlternate")]
public class CEEstimateAlternate : IBqlTable
{
#region EstimateNbr
[PXDBString(10, IsKey = true, IsUnicode = true, InputMask = "")]
[PXUIField(DisplayName = "Estimate Nbr")]
[PXDBDefault(typeof(CEEstimate.estimateNbr))]
[PXParent(typeof(SelectFrom<CEEstimate>.
Where<CEEstimate.estimateNbr.IsEqual<CEEstimateAlternate.estimateNbr.FromCurrent>>))]
public virtual string EstimateNbr { get; set; }
public abstract class estimateNbr : PX.Data.BQL.BqlString.Field<estimateNbr> { }
#endregion

#region Id
[PXDBIdentity(IsKey = true)]
public virtual int? Id { get; set; }
public abstract class id : PX.Data.BQL.BqlInt.Field<id> { }
#endregion

#region Number
[PXDBInt()]
[PXUIField(DisplayName = "Number")]
public virtual int? Number { get; set; }
public abstract class number : PX.Data.BQL.BqlInt.Field<number> { }
#endregion

#region Description
[PXDBString(50, IsUnicode = true, InputMask = "")]
[PXUIField(DisplayName = "Description")]
public virtual string Description { get; set; }
public abstract class description : PX.Data.BQL.BqlString.Field<description> { }
#endregion

#region Type
[PXDBString(10, IsUnicode = true, InputMask = "")]
[PXUIField(DisplayName = "Type")]
[PXStringList(
new string[]
{
"INCLUDED","EXCLUDED"
},
new string[]
{
"INCLUDED","EXCLUDED"
})]
public virtual string Type { get; set; }
public abstract class type : PX.Data.BQL.BqlString.Field<type> { }
#endregion
}
}

 

Userlevel 7
Badge +17

@eddiedaco  Since you are using the PXDBIdentity, and identity will NOT generate till the SAVE action performed.

Please perform below steps and verify.

  • Don’t change anything at database level.
  • Please take the below DAC and verify.
using System;
using PX.Data;
using PX.Data.BQL.Fluent; //se agrega esta linea

namespace Estimating
{
[Serializable]
[PXCacheName("CEEstimateAlternate")]
public class CEEstimateAlternate : IBqlTable
{
#region EstimateNbr
[PXDBString(10, IsKey = true, IsUnicode = true, InputMask = "")]
[PXUIField(DisplayName = "Estimate Nbr")]
[PXDBDefault(typeof(CEEstimate.estimateNbr))]
[PXParent(typeof(SelectFrom<CEEstimate>.
Where<CEEstimate.estimateNbr.IsEqual<CEEstimateAlternate.estimateNbr.FromCurrent>>))]
public virtual string EstimateNbr { get; set; }
public abstract class estimateNbr : PX.Data.BQL.BqlString.Field<estimateNbr> { }
#endregion

#region Id
[PXDBIdentity()]
public virtual int? Id { get; set; }
public abstract class id : PX.Data.BQL.BqlInt.Field<id> { }
#endregion

#region Number
[PXDBInt(IsKey = true)]
[PXUIField(DisplayName = "Number")]
public virtual int? Number { get; set; }
public abstract class number : PX.Data.BQL.BqlInt.Field<number> { }
#endregion

#region Description
[PXDBString(50, IsUnicode = true, InputMask = "")]
[PXUIField(DisplayName = "Description")]
public virtual string Description { get; set; }
public abstract class description : PX.Data.BQL.BqlString.Field<description> { }
#endregion

#region Type
[PXDBString(10,IsKey = true, IsUnicode = true, InputMask = "")]
[PXUIField(DisplayName = "Type")]
[PXStringList(
new string[]
{
"INCLUDED","EXCLUDED"
},
new string[]
{
"INCLUDED","EXCLUDED"
})]
public virtual string Type { get; set; }
public abstract class type : PX.Data.BQL.BqlString.Field<type> { }
#endregion
}
}

Userlevel 2

Hi @Naveen B  

thanks, i check it these days!
Userlevel 7
Badge +17

Hi @eddiedaco Sure, let me know if you get any issues.

Reply


About Acumatica ERP system
Acumatica Cloud ERP provides the best business management solution for transforming your company to thrive in the new digital economy. Built on a future-proof platform with open architecture for rapid integrations, scalability, and ease of use, Acumatica delivers unparalleled value to small and midmarket organizations. Connected Business. Delivered.
© 2008 — 2024  Acumatica, Inc. All rights reserved