views:

35

answers:

1

I have a dataset. I added a column "Tags". I need to run a function to update the value, for each row one at a time, for the "Tags" column. Here's what I've tried so far, tell me what I'm missing.

public System.Data.DataSet PopulateDataGrid(int nViewMode)
{
  System.Data.DataSet ds = _data.GetGridView(nViewMode)
  ds.Tables[0].Columns.Add("Tags", typeof(string));

  foreach (System.Data.DataRow dr in ds.Tables[0].Rows)
  {
    dr.BeginEdit();
    dr.ItemArray.SetValue(_dataFactory.GetPartTags(_dataFactory.ExecuteScalar("SELECT POSITION FROM PARTS WHERE PART_ID = " + dr.ItemArray.GetValue(0)).ToString()), dr.ItemArray.Length - 2);
    dr.SetModified();
    dr.EndEdit();
    dr.AcceptChanges();
  }

  ds.AcceptChanges();
  return ds;
}

Yeah, I know I could put using System.Data and wouldn't have to put it in my code, but this is the only function out of many using that, so I didn't want to do that (if there's no damage to loading time, then I may). I wish I just needed to run a query and that's it, but this needs to be filled in code. If someone could help, that'd be great. Thanks.

Edit: And I've also verified that _dataFactory.GetPartTags(_dataFactory.ExecuteScalar("SELECT POSITION FROM PARTS WHERE PART_ID = " + dr.ItemArray.GetValue(0)).ToString()) returns the value I want. It just shows up blank in the DataGridView. The column is there, but nothing is populated.

+2  A: 

You should be able to just use:

dr["Tags"] = _dataFactory.GetPartTags(_dataFactory.ExecuteScalar("SELECT POSITION FROM PARTS WHERE PART_ID = " + dr.ItemArray.GetValue(0)).ToString())

You should take note that the SetModified() method marks the row as modified, and AcceptChanges() clears the Modified/New/Deleted flags. Unless you have underlying code to store that data somewhere or do something more with it automatically, it's a little redundant. Same thing with the ds.AcceptChanges(). And if you try to use an adapter later on modified/new rows to push them to the database after you've done an AcceptChanges() on the rows/dataset, they won't be found as modified/new/deleted, and the changes won't be pushed to the database.

md5sum
lol. It says you answered 20 seconds ago. That's about the same time I did that. :) Thanks.
XstreamINsanity
Awesome, did it fix it?
md5sum
Yep. I don't know why I was trying to do it so difficult. I was looking at other code, though "Hmmm, I get information from a dataReader like `dataReader["columnName"]`, why not try that here. And boom, it worked. Preciate it.
XstreamINsanity