In a Visual Studio Project I have a Dataset, called Tablas_calendario. I want that if I click a button, I can add (and delete and modify, for other buttons) a row to a specific Table in Tablas_calendario.
Say I have a DataTable called DataTable1 and I want to add a row
If I do any of the following:
DataRow newRow = Tablas_calendario.DataTable1DataTable.NewRow();
DataRow newRow = Tablas_calendario.DataTable1.NewRow();
DataRow newRow = Tablas_calendario.Tables["DataTable1"];
I get errors, all say that you need a non-static reference for Tablas_calendario.Tables, Tablas_calendario.DataTable and etc...
I've looking the internet for solutions, but they are all variations of the above.
Can anyone point me in the right direction?
Assumptions:
Tablas_calendarioTablas_calendario tcDataset = new Tablas_calendario();Name (string)andAge (int)You can create new rows in the DataTable1 table like this:
Don't use
NewRow- it returns a base type DataRow, stripping away all the advantages of having strongly typed stuff in the first place (named properties likeName/stringandAge/intwith strong types)One of your essential problems is the name of your dataset type in the designer:
You've probably named the type what you should have named the instance variable. consider calling it something that includes the word "DataSet" to keep the naming convention that the dataset designer makes for everything else: Your table called "DataTable1" on the design surface is actually of type "DataTable1DataTable" and it has "DataTable1Row" rows. Call it something better than "DataTable1" too. For example, call your dataset "CalendariosDataSet" and the table inside it, call it Person if it related to a person (my example above):
Your code then looks like:
CalendariosDataSet ds = new CalendariosDataSet(); ds.Person //it's a property of the dataset object, that returns the PersonDataTable instance held by the dataset
Avoid using the Rows collection, because again it is the generic base type DataRow:
Be really clear you understand the difference between a class and an instance:
This is entirely the problem you have right now; you think you have an instance of the dataset but you don't - you're referring to the type as if it were an instance.