Solved

Gridview with update button on all rows

Posted on 2009-05-18
4
956 Views
Last Modified: 2013-11-26
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
Comment
Question by:conceptdata
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 21

Expert Comment

by:MogalManic
ID: 24420562
0
 

Author Comment

by:conceptdata
ID: 24421332
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
 
LVL 21

Accepted Solution

by:
MogalManic earned 250 total points
ID: 24422568
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
 

Author Closing Comment

by:conceptdata
ID: 31582711
Perfect - And thanks a lot..
Working fine
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

623 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question