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 TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Adding a row to an empty dataset

Status
Not open for further replies.

ac11nyc

Programmer
Oct 1, 2003
94
US
I have a datagrid that is bound to an empty dataset but it has a row that allows you to enter data and an option to Insert the row into the grid with information they filled out. The idea behind this is that i want the user to be able to enter multiple records and when they finally click save, it inserts everything at once in a batch. My question is how do i insert the row into the empty dataset and then continue addind to the dataset so that i may allow the grid to continue to grow until they are ready to save and upload all the records at once.

Any help would be greatly appreciated. Thanks
Alex
 
You can't add a row to a DataSet, can you? I mainly use DataReaders with ASP.NET for performance reasons, so I might be off here.

DataSets contain DataTables, which contain Columns and Rows.

In fact, often you don't even need a DataSet, if you're only binding a single table. In that case, skip the DataSet and just use a DataTable.

To add Rows to a DataTable:

Code:
// create DataTable objects to bind to datagrid:
myPrinters = new DataTable("myPrinters");
DataColumn workCol = myPrinters.Columns.Add("DeviceNo", typeof(string));

DataRow row = myPrinters.NewRow();
row["DeviceNo"] = "HP Laser";
myPrinters.Rows.Add(row);

row = myPrinters.NewRow();
row["DeviceNo"] = "Xerox 2045";
myPrinters.Rows.Add(row);

row = myPrinters.NewRow();
row["DeviceNo"] = "Color Laser";
myPrinters.Rows.Add(row);

datagrid1.DataSource = myPrinters;
datagrid1.DataBind();

That is of course server-side code. If you want to do this client-side, recognize that DataGrids are rendered as tables, and you can add rows and cells to a table via JavaScript.



Thomas D. Greer

Providing PostScript & PDF
Training, Development & Consulting
 
You may want to use VS.NET's built-in ability to create strongly-typed DataSets. All of the code needed to create the table schema will be generated for you so you can just call NewRow(), plus you get the benefits of early-binding (with some slight modifications to the generated code).
 
I think you need to store the dataset in the Cache so that it is instantiated between postbacks. Then you can add to it when the user for example clicks a button and then when the "Update" button is pressed then you write the dataset to the datastore.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top