DataGrid ItemStyle is a textbox and doesn't update the datagrid datasource

  • Thread starter Thread starter matthew schouppe
  • Start date Start date
M

matthew schouppe

I have a datagrid with two columns, the first a normal bound column,
the second is a template column created from a bound column. For the
ItemTemplate of the Template Column, I removed the label and replaced
it with a textbox. Displaying the data from the datasource is not a
problem, however when I change the data in the textbox and click on an
update button (the thought being that multiple rows will have been
changed and I can simply use the update command of the DataAdapter),
the underlying datasource (a DataSet) has HasChanges()=false. What am
I not doing to cause the DataSet to be updated when the user changes
the text in the textbox?

Thanks.

private DataSet ds = new DataSet();
private SqlDataAdapter sda = new SqlDataAdapter("SELECT orderid,
shipname FROM orders", new
SqlConnection("Server=(local)\\NetSDK;Integrated Security=true;Initial
Catalog=Northwind"));

private void Page_Load(object sender, System.EventArgs e)
{
if (!IsPostBack)
{
sda.Fill(ds, "orders");
DataGrid1.DataSource = ds.Tables["orders"];
DataGrid1.DataBind();
}
}

Private void Button1_Click(object sender, System.EventArgs e)
{
if (ds.HasChanges()) <- ALWAYS FALSE
{
ds.AcceptChanges();
sda.Update(ds);
sda.Fill(ds, "orders");
DataGrid1.DataSource = ds;
DataGrid1.DataBind();
}
}
 
Private void Button1_Click(object sender, System.EventArgs e)
{
foreach(DataGridItem i in DataGrid1.Items)
{
TextBox tb = ((i.Cells[1].Controls[1]));
... Your code;
}
}


Michael.
 
I don't understand the reference to '...Your code;'. How is the
ds.HasChanges() affected by the foreach statement?

I thought that at some point I saw an article that had an example of
this where the entire grid was saved using sqlDataAdapter.Update(ds),
I just don't know how to get my textbox to register a change to the
dataset.

Matthew
 
Matthew:

The data binding in ASP.NET is "one way". In order to get values from
the controls on the page back into your data source, you need to write
code to move the values. Michael was demonstrating some code to
iterate through the grid to pull out all the values in the TextBox
controls. Using this loop you could then put the text into your
populated DataSet, than use the Update method to push the values into
the database.

HTH,

--
Scott
http://www.OdeToCode.com

I don't understand the reference to '...Your code;'. How is the
ds.HasChanges() affected by the foreach statement?

I thought that at some point I saw an article that had an example of
this where the entire grid was saved using sqlDataAdapter.Update(ds),
I just don't know how to get my textbox to register a change to the
dataset.

Matthew

Michael Tkachev said:
Private void Button1_Click(object sender, System.EventArgs e)
{
foreach(DataGridItem i in DataGrid1.Items)
{
TextBox tb = ((i.Cells[1].Controls[1]));
... Your code;
}
}


Michael.
 
Back
Top