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

Discussion in 'ASP .Net' started by matthew schouppe, Apr 29, 2004.

  1. 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();
    }
    }
    matthew schouppe, Apr 29, 2004
    #1
    1. Advertising

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

    "matthew schouppe" <> wrote in message
    news:...
    > 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();
    > }
    > }
    Michael Tkachev, Apr 30, 2004
    #2
    1. Advertising

  3. 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" <> wrote in message news:<>...
    > 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.
    matthew schouppe, Apr 30, 2004
    #3
  4. matthew schouppe

    Scott Allen Guest

    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

    On 30 Apr 2004 05:33:05 -0700, (matthew schouppe)
    wrote:

    >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" <> wrote in message news:<>...
    >> 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.
    Scott Allen, Apr 30, 2004
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Clive

    ItemStyle ForeColor on a datagrid

    Clive, Jul 14, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    1,035
    Clive
    Jul 14, 2003
  2. Paul King

    ItemStyle from DataGrid

    Paul King, Jul 21, 2004, in forum: ASP .Net
    Replies:
    4
    Views:
    559
    Paul King
    Jul 21, 2004
  3. Diego
    Replies:
    0
    Views:
    561
    Diego
    Nov 12, 2004
  4. Replies:
    1
    Views:
    1,640
    Scott Allen
    Apr 29, 2005
  5. matthew schouppe

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

    matthew schouppe, Apr 29, 2004, in forum: ASP .Net Datagrid Control
    Replies:
    3
    Views:
    201
    Scott Allen
    Apr 30, 2004
Loading...

Share This Page