Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 969
  • Last Modified:

Gridview with update button on all rows

Hi I have the following code, and would like to have a edit all rows button and a update all the editited rows button.

Can someone help ?
ASPX code :
--------------------------------------------------------------
<%@ Page Language="C#" AutoEventWireup="true" Inherits="nFopWeb_01._Default" culture="auto" meta:resourcekey="PageResource1" uiculture="auto" CodeFile="LabelsEditQty.aspx.cs" %>
<%@ Register assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" namespace="System.Web.UI.WebControls" tagprefix="asp" %>
<%@ Import Namespace="System.Configuration"%>
<%@ Import Namespace="System.Data"%>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
     <form id="form1" runat="server" >
     <asp:Button ID="GeneratePDF" runat="server" Text="Generate PDF" onclick="GeneratePDF_Click" />
     <%--<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Edit Mode" />--%>
     <%--<asp:Button ID="Button2" runat="server" onclick="Button2_Click" Text="Update" />--%>
<br />
     <div>
         <asp:GridView ID="GridView1" runat="server" 
            AutoGenerateColumns="False" 
            AllowPaging="True"  
            OnPageIndexChanging="GridView1_PageIndexChanging" 
            OnRowCancelingEdit="GridView1_RowCancelingEdit" 
            OnRowDeleting="GridView1_RowDeleting"
            OnRowEditing="GridView1_RowEditing" 
            OnRowUpdating="GridView1_RowUpdating"
            onselectedindexchanged="GridView1_SelectedIndexChanged"
            CellPadding="4" 
            ForeColor="#333333" 
            GridLines="None" HorizontalAlign="Center" Width="700px" PageSize="99999">
            <RowStyle BackColor="#EFF3FB" />
            <Columns>
      
                <asp:BoundField DataField="Style" HeaderText="Style" SortExpression="Style" ReadOnly="true" />
                <asp:BoundField DataField="Navn" HeaderText="Name" SortExpression="Navn" ReadOnly="true" />
                <asp:BoundField DataField="Farvenavn" HeaderText="Color" SortExpression="Farvenavn" ReadOnly="true" />
                <asp:BoundField DataField="Størrelse" HeaderText="Size" SortExpression="Størrelse" ReadOnly="true" />
                <asp:BoundField DataField="Antal" HeaderText="Qty Labels" SortExpression="Antal" />    
                <asp:CommandField ShowEditButton="True" />
<%--    <asp:TemplateField HeaderText="Qty Labels">
    <ItemTemplate>
    <asp:Label ID="lblAntal" Visible='<%# !(bool) IsEditMode %>' runat="server" Text='<%# Eval("Antal") %>' />
    <asp:TextBox ID="txtAntal" Visible='<%# IsEditMode %>' runat="server" Text='<%# Eval("Antal") %>' />  
    </ItemTemplate>   
    </asp:TemplateField>--%>  
    
            </Columns>
            <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
            <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
            <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
            <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" 
                BorderStyle="Outset" HorizontalAlign="Left" />
            <EditRowStyle BackColor="#CCCCCC" />
            <AlternatingRowStyle BackColor="White" />
        </asp:GridView>
        <asp:TextBox ID="txtId" runat="server" Visible="False"></asp:TextBox>
        <asp:TextBox ID="txtName" runat="server" Visible="False"></asp:TextBox>
        <asp:TextBox ID="txtTel" runat="server" Visible="False"></asp:TextBox><br />
        </div>
    </form>
</body>
</html>
 
 
C# CodeBehind 
--------------------------------------------------------------
using System;
using System.Diagnostics;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml;
using System.Text;
using System.IO;
 
namespace nFopWeb_01
{
    public partial class _Default : System.Web.UI.Page
    {
     private bool isEditMode = false;
     protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindGrid();
            }
 
        }
 
        void BindGrid()
        {
            DataSet oDs = new DataSet();
            oDs.ReadXml(Request.QueryString["xmlfile"]);
            GridView1.DataSource = oDs;
            GridView1.DataBind();
        }
 
        protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GridView1.PageIndex = e.NewPageIndex;
            BindGrid();
        }
        protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            BindGrid();
            DataSet oDs = (DataSet)GridView1.DataSource;
            oDs.Tables[0].Rows[GridView1.Rows[e.RowIndex].DataItemIndex].Delete();
            oDs.WriteXml(Request.QueryString["xmlfile"]);
            BindGrid();
        }
 
        protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
        {
            GridView1.EditIndex = e.NewEditIndex;
            BindGrid();
        }
 
        protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            // Get the new values from the GridView controls
            int i = GridView1.Rows[e.RowIndex].DataItemIndex;
            //string strStyle = ((TextBox)GridView1.Rows[e.RowIndex].Cells[2].Controls[0]).Text;
            //7string strName = ((TextBox)GridView1.Rows[e.RowIndex].Cells[3].Controls[0]).Text;
            string strAntal = ((TextBox)GridView1.Rows[e.RowIndex].Cells[4].Controls[0]).Text;
 
            GridView1.EditIndex = -1;
            BindGrid();
 
            // Update the XML file using the new values
            DataSet oDs = (DataSet)GridView1.DataSource;
            //oDs.Tables[0].Rows[i][0] = strStyle;
            //oDs.Tables[0].Rows[i][1] = strName;
            oDs.Tables[0].Rows[i][28] = strAntal;
            oDs.WriteXml(Request.QueryString["xmlfile"]);
            BindGrid();
        }
 
        protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            GridView1.EditIndex = -1;
            BindGrid();
        }
  
         protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
        {
 
        }
 
        protected bool IsEditMode 
        { 
            get { return this.isEditMode; } 
            set { this.isEditMode = value; } 
        }
 
        protected void Button1_Click(object sender, EventArgs e)
        {
            IsEditMode = true;
            BindGrid();
        }
        protected void Button2_Click(object sender, EventArgs e)
        {
            IsEditMode = false;
        }
        protected void GeneratePDF_Click(object sender, EventArgs e)
        {
            Response.Redirect("xml2Pdf.aspx?XmlFileTemp=" + Request.QueryString["xmlfile"]);
        }
}
}

Open in new window

0
conceptdata
Asked:
conceptdata
  • 2
  • 2
1 Solution
 
MogalManicCommented:
0
 
conceptdataAuthor Commented:
Hi
I can see what he is doing, BUT my code (attached) Rowupdating, how do I call it ?

this.GridView1_RowUpdating(xxxx,xxxxx.........)   ????


        protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            // Get the new values from the GridView controls
            int i = GridView1.Rows[e.RowIndex].DataItemIndex;
            //string strStyle = ((TextBox)GridView1.Rows[e.RowIndex].Cells[2].Controls[0]).Text;
            //7string strName = ((TextBox)GridView1.Rows[e.RowIndex].Cells[3].Controls[0]).Text;
            string strAntal = ((TextBox)GridView1.Rows[e.RowIndex].Cells[4].Controls[0]).Text;
 
            GridView1.EditIndex = -1;
            BindGrid();
 
            // Update the XML file using the new values
            DataSet oDs = (DataSet)GridView1.DataSource;
            //oDs.Tables[0].Rows[i][0] = strStyle;
            //oDs.Tables[0].Rows[i][1] = strName;
            oDs.Tables[0].Rows[i][28] = strAntal;
            oDs.WriteXml(Request.QueryString["xmlfile"]);
            BindGrid();
        }

Open in new window

0
 
MogalManicCommented:
I got your sample to work the way you want it with the following changes:
  1) Changed the GridView to have a SaveAll button on the header and converted the "Qty Labels" column to be edit always.  Like this:
         
                <!%--Read Only Fields here--%>
               
                   
                       
                   
               
               
                   
               
             
  2) Changed the BindFunction to reload the data W/O binding like this:
   void BindGrid(Boolean LoadDataOnly)
        {
            DataSet oDs = new DataSet();
            oDs.ReadXml(this.XmlFile);
            GridView1.DataSource = oDs;
            if (!LoadDataOnly)
                GridView1.DataBind();  //Only bind AFTER data is changed
        }      
  3)  Added the HandleSaveAll command:

        protected void HandleSaveAll(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "SaveAll")
            {
                BindGrid(true);
                DataSet oDs = (DataSet)GridView1.DataSource;
                foreach (GridViewRow row in this.GridView1.Rows)
                    if (row.RowType == DataControlRowType.DataRow)
                        UpdateRow(oDs, row.RowIndex, row.DataItemIndex);
 
                oDs.AcceptChanges();
                oDs.WriteXml(this.XmlFile);
                BindGrid(false);
            }
        }
        protected void UpdateRow(DataSet oDs, int rowIndex, int DataRowIndex)
        {
            //Scrape the values of controls in edit mode
            //string strStyle = ((TextBox)GridView1.Rows[rowIndex].Cells[2].Controls[1]).Text;
            //string strName = ((TextBox)GridView1.Rows[rowIndex].Cells[3].Controls[1]).Text;
            string strAntal = ((TextBox)GridView1.Rows[rowIndex].Cells[4].Controls[1]).Text;
 
 
 
            //Update the data
            oDs.Tables[0].Rows[DataRowIndex]["Antal"] = strAntal;
        }

Open in new window

0
 
conceptdataAuthor Commented:
Perfect - And thanks a lot..
Working fine
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now