Edit in the gridview

  • (2 Pages)
  • +
  • 1
  • 2

17 Replies - 650 Views - Last Post: 24 April 2014 - 01:12 AM Rate Topic: -----

#1 sangeetha_s  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 94
  • Joined: 15-November 12

Edit in the gridview

Posted 21 April 2014 - 01:11 AM

I am having gridview with chemicalname, grade, price, quantity and total when i click on the edit i need to change the quantity and then it has to caluculate the total(price*quantity)

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="testpage.aspx.cs" Inherits="testpage" %>

<!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">
    <div>
        &nbsp;<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" onrowcancelingedit="GridView1_RowCancelingEdit" 
   onrowediting="GridView1_RowEditing" onrowupdating="GridView1_RowUpdating"  Height="305px" Width="785px" CellPadding="4" ForeColor="#333333" GridLines="None">
            <Columns>
                <asp:CommandField ButtonType="Button" ShowEditButton="True" />
                <asp:BoundField DataField="Name of the Chemical " HeaderText="Chemical Name" ReadOnly="True" />
                <asp:BoundField DataField="Grade" HeaderText="Grade" ReadOnly="True" />
                <asp:TemplateField HeaderText="Quantity">
                <ItemTemplate>
            <%# Eval("Quantity")%>
          </ItemTemplate>
          <EditItemTemplate>
            <asp:TextBox runat="server" ID="txtQuantity" Text='<%# Eval("Quantity")%>' />
          </EditItemTemplate>
              </asp:TemplateField>
               <asp:BoundField DataField="Price in"  HeaderText="Price" ReadOnly="True" />
               <asp:BoundField DataField="Total" HeaderText="Total" ReadOnly="True" />
                <asp:CheckBoxField HeaderText="Check" />

            </Columns>
            <RowStyle BackColor="#FFFBD6" ForeColor="#333333" />
            <FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
            <PagerStyle BackColor="#FFCC66" ForeColor="#333333" HorizontalAlign="Center" />
            <SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="Navy" />
            <HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
            <AlternatingRowStyle BackColor="White" />
        </asp:GridView>
        <br />
        <br />
        <asp:Button ID="Button1" runat="server" Text="VIEW" onclick="Button1_Click" /><br />
        &nbsp;
        <asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="False" CellPadding="4" ForeColor="#333333" GridLines="None" Height="177px" Width="594px">
        <Columns>
               
                <asp:BoundField DataField="Name of the Chemical " HeaderText="Chemical Name" ReadOnly="True" />
                <asp:BoundField DataField="Grade" HeaderText="Grade" ReadOnly="True" />
               <asp:BoundField DataField="Quantity" HeaderText="Quantity" ReadOnly="True" />
               <asp:BoundField DataField="Price in"  HeaderText="Price" ReadOnly="True" />
               <asp:BoundField DataField="Total" HeaderText="Total" ReadOnly="True" />

            </Columns>
            <RowStyle BackColor="#FFFBD6" ForeColor="#333333" />
            <FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
            <PagerStyle BackColor="#FFCC66" ForeColor="#333333" HorizontalAlign="Center" />
            <SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="Navy" />
            <HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
            <AlternatingRowStyle BackColor="White" />
        </asp:GridView>
    
    </div>
    </form>
</body>
</html>




And here is the codebehind


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 System.Data.OleDb;

public partial class testpage : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            BindData();
        }
        
    }

    private void BindData()
    {
        OleDbConnection con = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;
                Data Source= D:\chemicalbilling.xls ; Extended Properties='Excel 12.0;'");
        OleDbDataAdapter da = new OleDbDataAdapter("select * from [Sheet1$] ", con);
        DataSet ds = new DataSet();
        da.Fill(ds);
        GridView1.DataSource = ds.Tables[0];
        GridView1.DataBind();
    }
    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {

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

        //for (int i = 0; i < GridView1.Rows.Count; i++)
        //{

        //    TextBox txtQuantity = (TextBox)GridView1.Rows[i].FindControl("txtQuantity");
        //    TextBox txtTotal = (TextBox)GridView1.Rows[i].FindControl("txtTotal");

            //GridView1.Rows[i]["Quantity"] = txtQuantity.Text;
            //GridView1.Rows[i]["Total"] = Convert.ToInt32(txtQuantity.Text) * Convert.ToInt32(GridView1.Rows[i]["Price in"]);
        
       
    }
    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
       // GridViewRow row = GridView1.Rows[e.RowIndex];
        //GridViewRow row = GridView1.Rows[e.RowIndex];

        //TextBox txtQuantity = (TextBox)row.FindControl("txtQuantity");
        //TextBox txtTotal = (TextBox)row.FindControl("txtTotal");
        //TextBox txtUnitPrice = (TextBox)row.FindControl("txtUnitPrice");


        //int productID = Int32.Parse(GridView1.DataKeys[e.RowIndex].Value.ToString());

        //string productName = txtQuantity.Text;
        //decimal unitPrice = Decimal.Parse(txtUnitPrice.Text);


       // UpdateProduct(productID, productName);
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        OleDbConnection con = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;
                Data Source= D:\chemicalbilling.xls ; Extended Properties='Excel 12.0;'");
        OleDbDataAdapter da = new OleDbDataAdapter("select * from [Sheet1$] ", con);
        DataSet ds = new DataSet();
        da.Fill(ds);
        GridView2.DataSource = ds.Tables[0];
        GridView2.DataBind();
    }
}





Is This A Good Question/Topic? 0
  • +

Replies To: Edit in the gridview

#2 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3622
  • View blog
  • Posts: 11,290
  • Joined: 05-May 12

Re: Edit in the gridview

Posted 21 April 2014 - 05:35 AM

So what problem are you encountering?
Was This Post Helpful? 0
  • +
  • -

#3 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5582
  • View blog
  • Posts: 11,941
  • Joined: 02-June 10

Re: Edit in the gridview

Posted 21 April 2014 - 06:38 AM

Yeah, same here. I read "I want" like a kid at the store. Not "I've tried these four ways shown but not quite getting it." We won't give you answers where you don't show an effort to resolve your issue. It's double hard when you don't ever ask a question.
Was This Post Helpful? 0
  • +
  • -

#4 sangeetha_s  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 94
  • Joined: 15-November 12

Re: Edit in the gridview

Posted 22 April 2014 - 10:56 PM


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 System.Data.OleDb;

public partial class testpage : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            BindData();
        }
        
    }

    public void BindData()
    {
        OleDbConnection con = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;
                Data Source= D:\chemicalbilling.xls ; Extended Properties='Excel 12.0;'");
        OleDbDataAdapter da = new OleDbDataAdapter("select * from [Sheet1$] ", con);
        DataSet ds = new DataSet();
        da.Fill(ds);
        GridView1.DataSource = ds.Tables[0];
        GridView1.DataBind();
    }
    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {

    }
    
    protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        e.Cancel = true;
        GridView1.EditIndex = -1;
        BindData();
       
    }
    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        GridView1.EditIndex = e.NewEditIndex;
        //BindData();

        //for (int i = 0; i < GridView1.Rows.Count; i++)
        //{

        //    TextBox txtQuantity = (TextBox)GridView1.Rows[i].FindControl("txtQuantity");
        //    TextBox txtTotal = (TextBox)GridView1.Rows[i].FindControl("txtTotal");

            //GridView1.Rows[i]["Quantity"] = txtQuantity.Text;
            //GridView1.Rows[i]["Total"] = Convert.ToInt32(txtQuantity.Text) * Convert.ToInt32(GridView1.Rows[i]["Price in"]);
        
       
    }
    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
       // GridViewRow row = GridView1.Rows[e.RowIndex];
        //GridViewRow row = GridView1.Rows[e.RowIndex];

        //TextBox txtQuantity = (TextBox)row.FindControl("txtQuantity");
        //TextBox txtTotal = (TextBox)row.FindControl("txtTotal");
        //TextBox txtUnitPrice = (TextBox)row.FindControl("txtUnitPrice");


        //int productID = Int32.Parse(GridView1.DataKeys[e.RowIndex].Value.ToString());

        //string productName = txtQuantity.Text;
        //decimal unitPrice = Decimal.Parse(txtUnitPrice.Text);


       // UpdateProduct(productID, productName);
    }

    protected void chkboxSelectAll_CheckedChanged(object sender, EventArgs e)
    {

        //CheckBox ChkBoxHeader = (CheckBox)GridView2.HeaderRow.FindControl("chkboxSelectAll");
        //foreach (GridViewRow row in GridView2.Rows)
        //{
        //    CheckBox ChkBoxRows = (CheckBox)row.FindControl("chkSelect");
        //    if (ChkBoxHeader.Checked == true)
        //    {
        //        ChkBoxRows.Checked = true;
        //    }
        //    else
        //    {
        //        ChkBoxRows.Checked = false;
        //    }
        //}
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
    
        DataTable dt = new DataTable();
        dt.Columns.AddRange(new DataColumn[5] { new DataColumn("Name of the Chemical "), new DataColumn("Grade"), new DataColumn("Quantity"), new DataColumn("Price in"), new DataColumn("Total") });
        foreach (GridViewRow row in GridView1.Rows)
        {
            if (row.RowType == DataControlRowType.DataRow)
            {
                CheckBox chkSelect = (row.Cells[0].FindControl("chkSelect") as CheckBox);
                if (chkSelect.Checked)
                {
                    string name = row.Cells[1].Text;
                    string grade = row.Cells[2].Text;
                    
                    decimal price = Convert.ToDecimal(row.Cells[3]);
                  int quantity = Convert.ToInt32((row.Cells[4].FindControl("Quantity") as TextBox));
                   decimal  total =Convert.ToDecimal( row.Cells[5]);
                    //string country = (row.Cells[2].FindControl("lblCountry") as Label).Text;
                    dt.Rows.Add(name, grade, price,quantity , total);
                }
            }
        }
        GridView2.DataSource = dt;
        GridView2.DataBind();
        //gvSelected.DataSource = dt;
        //gvSelected.DataBind();
    }
    
}




I am getting the error in the button1_click inside the if loop i am able to display only name and grade
Unable to cast object of type 'System.Web.UI.WebControls.DataControlFieldCell' to type 'System.IConvertible'. This is the error i am getting
Was This Post Helpful? 0
  • +
  • -

#5 Momerath  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1012
  • View blog
  • Posts: 2,444
  • Joined: 04-October 09

Re: Edit in the gridview

Posted 23 April 2014 - 04:05 AM

Line 114 you can't convert a textbox to an Int32 no matter how hard you try. You might be able to convert the .Text property of a Textbox to an Int32, but that depends on the contents.

And next time, instead of "it's in the if loop" (and an if ISN"T a loop) how about you give the exact line number and copy/paste the error message?

This post has been edited by Momerath: 23 April 2014 - 04:06 AM

Was This Post Helpful? 0
  • +
  • -

#6 sangeetha_s  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 94
  • Joined: 15-November 12

Re: Edit in the gridview

Posted 23 April 2014 - 04:23 AM

then how can i display selected rows from one gridview to another when the checkbox is checked i am getting only two fields
Was This Post Helpful? 0
  • +
  • -

#7 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3622
  • View blog
  • Posts: 11,290
  • Joined: 05-May 12

Re: Edit in the gridview

Posted 23 April 2014 - 05:47 AM

Is TryParse() on the .Text property of the cells that you are interested in.
Was This Post Helpful? 0
  • +
  • -

#8 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5582
  • View blog
  • Posts: 11,941
  • Joined: 02-June 10

Re: Edit in the gridview

Posted 23 April 2014 - 05:05 PM

View Postsangeetha_s, on 23 April 2014 - 05:23 AM, said:

then how can i display selected rows from one gridview to another when the checkbox is checked i am getting only two fields


Set the DataSource on each grid to the same collection.
On the second create a Style that only displays a row where the property backing the checkbox is set to true.
Was This Post Helpful? 0
  • +
  • -

#9 sangeetha_s  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 94
  • Joined: 15-November 12

Re: Edit in the gridview

Posted 23 April 2014 - 10:29 PM

 protected void txtQuantity_TextChanged(object sender, EventArgs e)
    {
        TextBox txtQuantity = (TextBox)sender;
        GridViewRow row = (GridViewRow)txtQuantity.NamingContainer;
        TextBox txtTotal = (TextBox)row.FindControl("txtTotal");
        decimal price = Convert.ToDecimal(row.Cells[1].Text.Replace("$", String.Empty).Replace(",", String.Empty));
        decimal total = price * Convert.ToInt32(txtQuantity.Text);


        txtTotal.Text = string.Format("{0:C}", total);

    }




Ontextchanged i am trying to multiply the quantity with price and display the result in total field but i am getting the error in decimal price = Convert.ToDecimal(row.Cells[1].Text.Replace("$", String.Empty).Replace(",", String.Empty));
Input string is not in a correct format
Was This Post Helpful? 0
  • +
  • -

#10 Momerath  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1012
  • View blog
  • Posts: 2,444
  • Joined: 04-October 09

Re: Edit in the gridview

Posted 23 April 2014 - 10:54 PM

Before you do the replace, what's in the cell?
Was This Post Helpful? 0
  • +
  • -

#11 sangeetha_s  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 94
  • Joined: 15-November 12

Re: Edit in the gridview

Posted 23 April 2014 - 10:59 PM

In the quantity field if i enter 2 and the price 300-00 which is coming from excel sheet in the total field i want it to display 500-00
Was This Post Helpful? 0
  • +
  • -

#12 sangeetha_s  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 94
  • Joined: 15-November 12

Re: Edit in the gridview

Posted 23 April 2014 - 11:55 PM

display 600-00
Was This Post Helpful? 0
  • +
  • -

#13 Momerath  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1012
  • View blog
  • Posts: 2,444
  • Joined: 04-October 09

Re: Edit in the gridview

Posted 23 April 2014 - 11:55 PM

Well, - isn't a valid character for a decimal number, thus you get the error.
Was This Post Helpful? 0
  • +
  • -

#14 sangeetha_s  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 94
  • Joined: 15-November 12

Re: Edit in the gridview

Posted 24 April 2014 - 12:04 AM

is it you are asking the value from the excel i.e 300-00
Was This Post Helpful? 0
  • +
  • -

#15 sangeetha_s  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 94
  • Joined: 15-November 12

Re: Edit in the gridview

Posted 24 April 2014 - 12:14 AM

Thank you its solved now
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2