Professional Web Applications Themes

DataGrid ItemStyle is a textbox and doesn't update the datagrid datasource - ASP.NET Data Grid Control

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

  1. #1

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

    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 Guest

  2. #2

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

    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" <mschoupexcite.com> wrote in message
    news:be11921e.0404290700.2cb3ea2aposting.google.c om...
    > 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 Guest

  3. #3

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

    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" <m_tkachevhotmail.com> wrote in message news:<OTn3r1nLEHA.3292TK2MSFTNGP11.phx.gbl>...
    > 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 Guest

  4. #4

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


    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
    [url]http://www.OdeToCode.com[/url]

    On 30 Apr 2004 05:33:05 -0700, [email]mschoupexcite.com[/email] (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" <m_tkachevhotmail.com> wrote in message news:<OTn3r1nLEHA.3292TK2MSFTNGP11.phx.gbl>...
    >> 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 Guest

Similar Threads

  1. Replies: 2
    Last Post: September 30th, 06:41 PM
  2. Updating DataGrid while editing, doesn't update
    By Jeff Petter in forum ASP.NET Data Grid Control
    Replies: 0
    Last Post: October 16th, 10:19 PM
  3. Replies: 0
    Last Post: October 8th, 01:19 AM
  4. DataGrid Update Doesn't Show Immediately
    By Wickwack in forum ASP.NET General
    Replies: 0
    Last Post: July 22nd, 10:49 PM
  5. ItemStyle ForeColor on a datagrid
    By Clive in forum ASP.NET General
    Replies: 0
    Last Post: July 14th, 11:28 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139