ASP.NET 2 GridView.

Discussion in 'ASP .Net' started by realgeek@gmail.com, Sep 8, 2005.

  1. Guest

    I got to playing with ASP.NET 2.0. So I have the following code:
    CODE
    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
    DataKeyNames="QuestionID"
    OnRowCancelingEdit="GridView1_RowCancelingEdit"
    OnRowEditing="GridView1_RowEditing"
    OnRowUpdating="GridView1_RowUpdating">
    <Columns>
    <asp:BoundField DataField="Description"
    HeaderText="??????"></asp:BoundField>
    <asp:CheckBoxField DataField="IsActive" HeaderText="???." />
    <asp:CommandField ButtonType="Image"
    CancelImageUrl="img/button_cancel.png" EditImageUrl="img/edit.png"
    ShowEditButton="True" UpdateImageUrl="img/button_ok.png" />
    </Columns>
    </asp:GridView>


    In .cs file:
    CODE
    SqlDataSource ds;

    protected void BindData()
    {
    ds = new
    SqlDataSource(WebConfigurationManager.ConnectionStrings["AnketaDBConnectionString"].ToString(),
    "SELECT QuestionID,Description,IsActive FROm Question");
    GridView1.DataSource = ds;
    GridView1.DataBind();

    }

    protected void Page_Load(object sender, EventArgs e)
    {
    if (!IsPostBack)
    {
    BindData();
    }
    }

    protected void GridView1_RowUpdating(object sender,
    GridViewUpdateEventArgs e)
    {
    Response.Write(e.Keys.Count.ToString());
    Response.Write(e.NewValues.Count.ToString());
    }

    protected void GridView1_RowEditing(object sender,
    GridViewEditEventArgs e)
    {
    GridView1.EditIndex = e.NewEditIndex;
    BindData();
    }

    protected void GridView1_RowCancelingEdit(object sender,
    GridViewCancelEditEventArgs e)
    {
    GridView1.EditIndex = -1;
    BindData();
    }

    You see that it outputs number of keys and new values on update.
    Problem is, both numbers are zero when I press edit, preform editing
    and press ok, I cannot update anything. What's wrong with that?
     
    , Sep 8, 2005
    #1
    1. Advertising

  2. Brock Allen Guest

    If you want to the grid to do automatic updates back to the database you
    need to use the new declarative databinding model. SO, IOW, you need a <asp:SqlDataSource>
    and to set the DataSourceID on your GridView.

    http://beta.asp.net/QUICKSTART/aspnet/doc/data/default.aspx

    -Brock
    DevelopMentor
    http://staff.develop.com/ballen



    > I got to playing with ASP.NET 2.0. So I have the following code:
    > CODE
    > <asp:GridView ID="GridView1" runat="server"
    > AutoGenerateColumns="False"
    > DataKeyNames="QuestionID"
    > OnRowCancelingEdit="GridView1_RowCancelingEdit"
    > OnRowEditing="GridView1_RowEditing"
    > OnRowUpdating="GridView1_RowUpdating">
    > <Columns>
    > <asp:BoundField DataField="Description"
    > HeaderText="??????"></asp:BoundField>
    > <asp:CheckBoxField DataField="IsActive" HeaderText="???." />
    > <asp:CommandField ButtonType="Image"
    > CancelImageUrl="img/button_cancel.png" EditImageUrl="img/edit.png"
    > ShowEditButton="True" UpdateImageUrl="img/button_ok.png" />
    > </Columns>
    > </asp:GridView>
    > In .cs file:
    > CODE
    > SqlDataSource ds;
    > protected void BindData()
    > {
    > ds = new
    > SqlDataSource(WebConfigurationManager.ConnectionStrings["AnketaDBConne
    > ctionString"].ToString(),
    > "SELECT QuestionID,Description,IsActive FROm Question");
    > GridView1.DataSource = ds;
    > GridView1.DataBind();
    > }
    >
    > protected void Page_Load(object sender, EventArgs e)
    > {
    > if (!IsPostBack)
    > {
    > BindData();
    > }
    > }
    > protected void GridView1_RowUpdating(object sender,
    > GridViewUpdateEventArgs e)
    > {
    > Response.Write(e.Keys.Count.ToString());
    > Response.Write(e.NewValues.Count.ToString());
    > }
    > protected void GridView1_RowEditing(object sender,
    > GridViewEditEventArgs e)
    > {
    > GridView1.EditIndex = e.NewEditIndex;
    > BindData();
    > }
    > protected void GridView1_RowCancelingEdit(object sender,
    > GridViewCancelEditEventArgs e)
    > {
    > GridView1.EditIndex = -1;
    > BindData();
    > }
    > You see that it outputs number of keys and new values on update.
    > Problem is, both numbers are zero when I press edit, preform editing
    > and press ok, I cannot update anything. What's wrong with that?
    >
     
    Brock Allen, Sep 8, 2005
    #2
    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. Ben
    Replies:
    3
    Views:
    1,233
  2. Kivak Wolf
    Replies:
    2
    Views:
    10,118
    Kivak Wolf
    Jun 28, 2005
  3. Andrea Raimondi
    Replies:
    1
    Views:
    551
  4. =?Utf-8?B?bWdvbnphbGVzMw==?=

    GridView Hierarchical View - Gridview in Gridview

    =?Utf-8?B?bWdvbnphbGVzMw==?=, May 9, 2006, in forum: ASP .Net
    Replies:
    1
    Views:
    18,046
    =?Utf-8?B?UGhpbGxpcCBXaWxsaWFtcw==?=
    May 9, 2006
  5. Amelyan
    Replies:
    2
    Views:
    209
    Amelyan
    Apr 25, 2005
Loading...

Share This Page