views:

1654

answers:

3

I am continuing from this post.

After much Googling, I have come up with this code to edit cells programmatically:

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using Ice_Web_Portal.BO;

namespace GridView___Test
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            GridView1.DataSource = Course.GetCourses();
            GridView1.DataBind();
        }

        protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
        {
            GridViewRow row = GridView1.Rows[e.NewEditIndex];

            GridView1.EditIndex = e.NewEditIndex;

            GridView1.DataSource = Course.GetCourses();
            GridView1.DataBind();
        }

        protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            TextBox txtID = (TextBox)GridView1.Rows[e.RowIndex].Cells[1].Controls[0];
            TextBox txtCourseCode = (TextBox)GridView1.Rows[e.RowIndex].Cells[2].Controls[0];
            TextBox txtCourseName = (TextBox)GridView1.Rows[e.RowIndex].Cells[3].Controls[0];
            TextBox txtCourseTextBookCode = (TextBox)GridView1.Rows[e.RowIndex].Cells[4].Controls[0];

            Course item = new Course();
            item.ID = Convert.ToInt32(txtID.Text);
            item.CourseCode = txtCourseCode.Text;
            item.CourseName = txtCourseName.Text;
            item.TextBookCode = txtCourseTextBookCode.Text;

            bool success = Course.Update(item);

            labMessage.Text = success.ToString();

            GridView1.EditIndex = -1;
            GridView1.DataSource = Course.GetCourses();
            GridView1.DataBind();
        }
    }
}

But 2 problems are happening.

(1) I need to press command buttons twice to Edit/Update.

(2) Changes in the cell values are not updated in the database. I.e. edited cell values are not committing.

Can anyone give me a solution?

UPDATE: My solution was like this:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="GridView___Test._Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>

<body>
    <form id="form1" runat="server">
    <div>

        <asp:GridView ID="GridView1" runat="server" Font-Names="Verdana" Font-Size="Small" AutoGenerateColumns="False" OnRowCancelingEdit="GridView1_RowCancelingEdit" OnRowCommand="GridView1_RowCommand" OnRowEditing="GridView1_RowEditing" OnRowUpdating="GridView1_RowUpdating">            
        </asp:GridView>

    </div>
    </form>
</body>

</html>

namespace GridView___Test
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //if (!Page.IsPostBack)
            {
                CreateGridView();
            }
        }

        private void CreateGridView()
        {
            GridView1.Columns.Clear();

            DataTable dataTable = Book.GetBooksDataSet().Tables[0];

            CommandField cf = new CommandField();
            cf.ShowEditButton = true;

            GridView1.Columns.Add(cf);

            int colCount = 1;
            foreach (DataColumn c in dataTable.Columns)
            {
                BoundField boundField = new BoundField();

                boundField.DataField = c.ColumnName;
                boundField.HeaderText = c.ColumnName;
                //boundField.FooterText = "---";

                if (colCount == 3 || colCount == 5)
                {
                    boundField.ReadOnly = true;
                }

                GridView1.Columns.Add(boundField);
                colCount++;
            }

            GridView1.ShowFooter = true;

            GridView1.DataSource = dataTable;
            GridView1.DataBind();

            GridViewRow footerRow = GridView1.FooterRow;
            Button b = new Button();
            b.Text = "Add New";
            int i = 0;
            footerRow.Cells[i].Controls.Add(b);
            foreach (DataColumn c in dataTable.Columns)
            {
                ++i;
                TextBox tb = new TextBox();
                footerRow.Cells[i].Controls.Add(tb);
            }
        }

        protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            int index = Convert.ToInt32(e.CommandArgument);

            if (e.CommandName == "Edit")
            {
                //Takes the GridView to Edit mode.
                GridView1.EditIndex = index;

                GridViewRow selectedRow = ((GridView)e.CommandSource).Rows[index];

                //We can get cell data like this
                string id = selectedRow.Cells[1].Text;
                string isbn = selectedRow.Cells[2].Text;

                //This is necessary to GridView to be showed up.
                CreateGridView();
            }
            else if (e.CommandName == "Update")
            {
                LinkButton updateButton = (LinkButton)e.CommandSource;

                DataControlFieldCell dcfc = (DataControlFieldCell)updateButton.Parent;

                GridViewRow gvr = (GridViewRow)dcfc.Parent;

                //The update...................
                //Update grid-data to database
                UpdateDataInTheDatabase(gvr.Cells[1].Controls);                

                //Grid goes back to normal
                GridView1.EditIndex = -1;

                //This is necessary to GridView to be showed up.
                CreateGridView();
            }
        }

        private void UpdateDataInTheDatabase(ControlCollection cc)
        {
            TextBox tb = (TextBox)cc[0];
            //...
            //...
            //...

            //Call the update persistance code here...
        }

        #region Application Satisfactory Event Handlers
        protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
        {
        }

        protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
        }

        protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
        }
        #endregion
    }
}
+2  A: 

for problem #1,try

    protected void Page_Load(object sender, EventArgs e)
    {
        if(!Page.IsPostBack)
        {
        GridView1.DataSource = Course.GetCourses();
        GridView1.DataBind();
        }
    }

for problem #2, we need to check your method Course.Update(item) .

kurast
My Update method has no problem I think, coz when i am casting cells to TextBoxes, they are showing me old values always. I.e. edited cell values are not being committed.
JMSA
I have set break points and examined it.
JMSA
If Update method is not working, may be problem is in your sql procedures or transaction is left open and any sql statement does not execute.Make sure of that please.
Myra
I have checked it multiple times. Can you understand what I said? When I am typing a new value in the cell using my keyboard, I am not finding that value. I am finding the old value instead. ANd I am finding it before the Update. If the new value is not entering the newly created Course object, how can it be Updated in the DB?
JMSA
A: 

I have similar problem, I cant read new edited values from the cells into my code.

Nagarjuna
A: 

I have this problem too.. how did you solve it?

I have something like this:

((TextBox)(row.Cells[0].Controls[0])).Text;

And it gives me the old value. I can't find a way to get what the user has entered. The datasource for my grid is a list.

gsxrlady
Plz see the update.
JMSA