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

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();
}
}
 
M

Michael Tkachev

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.
 
M

matthew schouppe

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
 
S

Scott Allen

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,812
Latest member
GracielaWa

Latest Threads

Top