How can I add/delete/modify a row in a specific DataTable in a Dataset in Visual Studio?

303 Views Asked by At

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?

1

There are 1 best solutions below

0
Caius Jard On

Assumptions:

  • You have a strongly typed dataset, the type for which you've called Tablas_calendario
  • Your dataset contains a strongly typed datatable, and the designer chows its name to be DataTable1.
  • You have created an instance of the dataset like this: Tablas_calendario tcDataset = new Tablas_calendario();
  • DataTable1 has two columns: Name (string) and Age (int)

You can create new rows in the DataTable1 table like this:

//add a new row by providing all its values
tcDataset.DataTable1.AddDataTable1Row("Federico", 33);

//create a new row then add it - useful if you don't know all the values first
var newRow =  tcDataset.DataTable1.NewDataTable1Row();
newRow.Name = "Federico";
newRow.Age = 33;
tcDataset.DataTable1.Add(newRow);

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 like Name/string and Age/int with strong types)


One of your essential problems is the name of your dataset type in the designer:

enter image description here

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):

enter image description here

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

ds.Person.Count; //it's the count of rows

ds.Person[0].Name = "Federico";  //it edits the name of the first person 

foreach(var pRo in ds.Person) //it enumerates the rows as PersonRow objects

Avoid using the Rows collection, because again it is the generic base type DataRow:

ds.Person.Rows[0]. // it has no Name property, it is just a DataRow, not a PersonRow
ds.Person.Rows[0]["Name"] = "Federico"; //it would access the name, as an object, but again you lose all the benefits of strong typing! 

Be really clear you understand the difference between a class and an instance:

public class Person{

  public string Name {get; set; }

}

static void Main(string[] arg){

  Person.Name = "Federico"; //"need an instance for the non static class Person" error

} 

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.