views:

39

answers:

2

I'm trying to edit the data in a dataset (change the value in a column on one row), which is not linked to a database. I've been googling for about an hour with no results and no good examples out there. Hopefully someone can help me.

My table (DataTable1) has these columns - ThreadID (string, PK), StatusText (string).

I can select a row in a DataGridView, and get the ThreadID value. No matter how I've tried to edit the row in the associated dataset, either nothing happens or I get an error. Here's what I have now:

string sThreadID = "";
sThreadID = gridThreads.Rows[gridThreads.CurrentRow.Index].Cells["ID"].Value.ToString();  // gives me a good id, which is in the dataset
DataRow drRow = dataThreads.Tables["DataTable1"].Rows.Find(sThreadID);
drRow["StatusText"] = "Test";

The error I get when getting the row (3rd line) is: "Object reference not set to an instance of an object.". I can't create a new DataRow object because there's no public constructor for it (according to my research).

I'm sure I'm missing something basic, but I'm not familiar with working with datasets. What am I doing wrong? Thanks for your help.

A: 

Does it happen that dataThreads or Tables["DataTable1"] is null ?

To create a new row, use the NewRow() method of the data table.

thelost
A: 

How does drRow know what "statusText" is? You may be only getting a shalllow copy, try cloning/copy so that the data types of the row get copied as well. Just an idea.

Induster
Also, try indexing with 0 instead of DataTable1, unless that is truely the name of the table.
Induster
You know what, it was indeed named "DataTable1", but when I switched to the index it worked. Thanks Induster
Jon Slaven