Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Proper way to bind GridView to class object?? 1

Status
Not open for further replies.

randysmid

Programmer
Dec 12, 2001
801
0
0
US
Hi All,
I have a class object that I'm trying to bind to a GridView, but it simply doesn't work. It gives me this error message:
A field or property with the name "Id" was not found in the selected data source.

So......
Where is the proper place to bind the Gridview columns to the class object? By the way, I tried doing this in the form html because I need to add 5 CheckBoxFields, as well as an inputtable text box.

Any help is greatly appreciated.

TIA, Randy
 
A field or property with the name "Id" was not found in the selected data source
Are you saying there is a field or property named "ID" in the datasource and it isn't being found?


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
Yes,
Here's the error message I'm getting:

Server Error in '/Web' Application.
--------------------------------------------------------------------------------

A field or property with the name 'mrt.Id' was not found on the selected data source.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Web.HttpException: A field or property with the name 'mrt.Id' was not found on the selected data source.

Source Error:


Line 22: List<MaterialRateTable> mrt = mrdm.FetchAllRates();
Line 23: gvMaterialRates.DataSource = mrt; //???????
Line 24: gvMaterialRates.DataBind();
Line 25:
Line 26: }


 
OK, so if you debug the project in VS, hover over the "mrt" variable once it's been assigned (i.e. in line 23). What does it show? Can you paste the details of the variable here?


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
Hi ca8msm,
"mrt" is a list of rows from the class object. So, I am wondering if I need to loop through the list of items (which I can do), but how do I bind these items to the GridView?

There are many examples of how to bind regular SQL and Access tables to a GridView, but I have yet to find a sample of how to bind an OOP object to a GridView that works.
 
Can you provide an example of the object?


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
Hi,
here is the class for the MaterialRateTable object:

public class MaterialRateTable
{
#region fields
private MaterialRateDataMapper materialRateDM;//see property

public int Id;
public string Description;
public double CostPerContainer;
public double QtyPerContainer;
public double PerUnitCost; //hidden, updated on change to Cost & Qty per Container
public string MaterialRateNotes;
#endregion fields

#region properties
public MaterialRateDataMapper MaterialRateDM
{
get
{
if (materialRateDM == null)
this.materialRateDM = GWO.ApplicationManager.MaterialRateDM;
return materialRateDM;
}
set
{
this.materialRateDM = value;
}
}
#endregion properties

#region methods
public void Save()
{
MaterialRateDM.InsertMaterialRate(this);

}
public void Update()
{
MaterialRateDM.UpdateMaterialRate(this);
}
public void Delete()//may never be called
{
materialRateDM.DeleteMaterialRateTable(this);
}
#endregion methods
}
 
OK, I thought maybe I could use your class to demonstrate an example but it's not as simple as I thought it might have been (for example, I don't know what your "MaterialRateDataMapper" class holds).

Instead, it's probably easier if you read up on the ObjectDataSource control. Then, you can follow this walkthrough on how to bind your object to a GridView using this control:



____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.
 
Hi ca8msm,
Thanks for the links. Lately, I've been having real trouble finding anything useful on the MSDN site. Here's a star for all of your help!!!
Randy
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top