Pete
Pete

Reputation: 713

Changing dataset data for datagrid

I'm trying to update an attribute inside a dataset, which is then displayed in a datagrid. However, when I attempt to assign the date to my row.ItemArray[0] it doesn't appear to be updating. Not in debug nor in the datagrid.

DataSet ds = new DataSet();
connection.Open();
adapter.Fill(ds);

foreach (DataTable table in ds.Tables)
{
    foreach (DataRow row in table.Rows)
    {
        string date = row.ItemArray[0].ToString();
        date = date.Remove(date.Length - 12);
        row.ItemArray[0] = date;

        foreach (object item in row.ItemArray)
        {
            // read item
            //Console.WriteLine(item.ToString());
        }
    }
}
ds.AcceptChanges();
invoicesDataGrid.AutoGenerateColumns = true;
invoicesDataGrid.ItemsSource = ds.Tables[0].DefaultView;

Upvotes: 0

Views: 55

Answers (2)

Rita Chavda
Rita Chavda

Reputation: 191

make your select query like below

select cast(yourdatecolumnName as date) As date from yourtableName

Upvotes: 1

MethodMan
MethodMan

Reputation: 18863

change your code to the following below. you do not need that many nested foreach loops

DataSet ds = new DataSet();
connection.Open();
adapter.Fill(ds);
DataTable table = ds.Tables[0];

foreach (DataRow row in table.Rows)
{
    for (int i = 0; i < row.ItemArray.Length; i++) 
    {
       string date = row.ItemArray[0].ToString();
       date = date.Remove(date.Length - 12);
       string val = row[i].ToString();
       row[i] = date;
       Debug.WriteLine("oldValue: {0}, newValue: {1}", val, row[i].ToString());
    }
}
invoicesDataGrid.AutoGenerateColumns = true;
invoicesDataGrid.ItemsSource = table.DefaultView;

Upvotes: 0

Related Questions