Selecting the multiple rows in GridView and inserting the selected rows to another Gridview & Performing some operations on it

 Dear All,

I have become mad for solving this problem. Please Help me.

 I have a Gridview bound to a datasource. I inserted the CheckBox Control in Templet to select certain rows. Gridview1 contains some products id & name.

I want to insert the selected rows in to second Gridview  and in the second Gridview I need to add two more colums AdvicedQty & AcceptedQty(TextBox Cotrol).

The user will select few products in Gridview1 & clicks add which will be added another gridview then in second Gridview he enters the AdviceQty & AcceptedQty.

After entering the data he clicks SAVE buton. The data entered will be updated to another table called GRNDTL.

 

Please help me to solve this problem.

 

please guide me step by step if possible....  I use C#.

 

Thanks in advance 

 Vinay H N


I Dream it First before I achieve it.

www.vinayhn.com
www.flamboyanz.com
0
truespace4u
11/27/2008 9:43:52 AM
asp.net.presentation-controls 72751 articles. 3 followers. Follow

3 Replies
1854 Views

Similar Articles

[PageSpeed] 51

Hello Vinay,

You will need a way to store the records that have been selected from the first GridView while the user is working with them.  Here is an example from another forum post that describes how to have two DataGrid's, where a user selects records from one and they are transferred to the other DataGrid.  You should be able to adapt the idea to your GridViews:

http://forums.asp.net/p/1297473/2522734.aspx#2522734

Here is another post that talks about copying GridViewRow from one GridView to another:

http://forums.asp.net/p/1169459/1955946.aspx

In order to have the other extra fields, they can be added to the DataTable used in the first link I gave you as extra columns, and TextBox's bound to those columns so that data can be entered into them.  Then in your Save button, you would just traverse the contents of the DataTable that stores the data in the second copied DataGrid (or GridView in your case), and build SQL query to add that data to the database.

Hope this helps!

Cheers,

~ mellamokb

0
mellamokb
11/27/2008 9:13:34 PM

 Hello Mellamokb,

Thanks for the replay.

Say In first Gridview I have checkboxs in ItemTemplate. and the page contains A add button & Gridview2 in the Same page.

Please give me the code if I click on the  Add button I need to traverse through the Gridview1 & I want to insert the selected rows to Gridview2 & also the Gridview should contain 2 more columns for the user to enter some data(TextBoxControl). Also please make sure that everything happens in single Page & upon clicking the ADD button it should reflect in the gridview2.

Please do me the favour by providing me the CODE in C#. I will be very thankful to you.

 With Regards

Vinay H N

www.vinayhn.com


I Dream it First before I achieve it.

www.vinayhn.com
www.flamboyanz.com
0
truespace4u
11/28/2008 11:34:36 AM

Hi Vinay,

I have done my best to adapt the example I gave you to your situation.  You may need to change some field names and stuff to get it to work, but here is what I have: 

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

<!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>
        <asp:SqlDataSource ID="ProductsDataSource" runat="server"
            ConnectionString='<%$ConnectionStrings:TestConnectionString %>'
            SelectCommand="SELECT [ProductID], [ProductName] FROM [Products]">
        </asp:SqlDataSource>
        
	    <asp:gridview id="GridView1" runat="server" autogeneratecolumns="false"
	        datakeynames="ProductID" style="margin-bottom: 20px" DataSourceID="ProductsDataSource">
		    <columns>
			    <asp:BoundField DataField="ProductID" ReadOnly="true"
			        HeaderText="ID" />
			    <asp:BoundField DataField="ProductName" HeaderText="Product Name" />
			    <asp:TemplateField HeaderText="Include">
			        <ItemTemplate>
			            <asp:CheckBox ID="ChkSelected" runat="server" />
			        </ItemTemplate>
			    </asp:TemplateField>
		    </columns>
	    </asp:gridview>
	    <asp:Button ID="Copy" runat="server" Text="Copy Selected Rows" 
	        OnClick="Copy_Click" />
	    <asp:Label ID="Message" runat="server" Text="" Visible="false"
	        ForeColor="Red"></asp:Label>
	    
	    <br /><br />
	    <asp:gridview id="GridView2" runat="server" autogeneratecolumns="false" style="margin-bottom: 20px"
	        DataKeyNames="productID">
		    <columns>
			    <asp:BoundField DataField="ProductID" ReadOnly="true"
			        HeaderText="ID" />
			    <asp:BoundField DataField="ProductName" HeaderText="Product Name" />
			    <asp:TemplateField HeaderText="Advised Quantity">
			        <ItemTemplate>
			            <asp:TextBox ID="AdvisedQty" runat="server"
			                Text='<%#Bind("AdvisedQty") %>'
			                OnTextChanged="AdvisedQty_TextChanged"></asp:TextBox>
			        </ItemTemplate>
			    </asp:TemplateField>
			    <asp:TemplateField HeaderText="Accepted Quantity">
			        <ItemTemplate>
			            <asp:TextBox ID="AcceptedQty" runat="server"
			                Text='<%#Bind("AcceptedQty") %>'
			                OnTextChanged="AcceptedQty_TextChanged"></asp:TextBox>
			        </ItemTemplate>
			    </asp:TemplateField>
		    </columns>
	    </asp:gridview>
	    <asp:Button ID="Save" runat="server" Text="Save"
	        OnClick="Save_Click" Visible="false" />
    </div>
    </form>
</body>
</html>
 And here is the code: 
    protected void AdvisedQty_TextChanged(object sender, EventArgs e)
    {
        TextBox tb = (TextBox)sender;
        GridViewRow gvr = (GridViewRow)tb.Parent.Parent;
        this.CopiedData[gvr.RowIndex].AdvisedQty = Convert.ToInt32(tb.Text);
    }

    protected void AcceptedQty_TextChanged(object sender, EventArgs e)
    {
        TextBox tb = (TextBox)sender;
        GridViewRow gvr = (GridViewRow)tb.Parent.Parent;
        this.CopiedData[gvr.RowIndex].AcceptedQty = Convert.ToInt32(tb.Text);
    }

    private List CopiedData
    {
        get
        {
            if (this.ViewState["CopiedData"] == null)
            {
                this.ViewState["CopiedData"] = new List();
            }

            return this.ViewState["CopiedData"] as List;
        }
    }

    protected void Copy_Click(object sender, EventArgs e)
    {
        foreach (GridViewRow gvr in GridView1.Rows)
        {
            CheckBox cb = (CheckBox)gvr.FindControl("ChkSelected");

            if (cb.Checked)
            {
                //Add items to the new CompositeProductItem Grid
                int productID = Convert.ToInt32(GridView1.DataKeys[gvr.RowIndex]["ProductID"]);
                string productName = gvr.Cells[1].Text;

                Row row = new Row
                {
                    ProductID = productID,
                    ProductName = productName
                };

                if (this.CopiedData.Exists(r => r.ProductID == productID))
                {
                    foreach (GridViewRow dynamicRow in GridView2.Rows)
                        if (Convert.ToInt32(dynamicRow.Cells[0].Text) == row.ProductID)
                        {
                            TextBox tb = (TextBox)dynamicRow.FindControl("AdvisedQty");
                            TextBox tb2 = (TextBox)dynamicRow.FindControl("AcceptedQty");
                            row.AdvisedQty = Convert.ToInt32(tb.Text) + 1;
                            row.AcceptedQty = Convert.ToInt32(tb2.Text);
                        }

                    this.CopiedData.Add(row);
                    int index = this.CopiedData.FindIndex(r => r.ProductID == productID);
                    this.CopiedData.RemoveAt(index);
                    GridView2.DataSource = this.CopiedData;
                    GridView2.DataBind();


                }

                if (!this.CopiedData.Exists(r => r.ProductID == productID))
                {
                    row.AdvisedQty = 1;
                    row.AcceptedQty = 0;
                    this.CopiedData.Add(row);
                    GridView2.DataSource = this.CopiedData;
                    GridView2.DataBind();
                }
            }
        }

        if (GridView2.Rows.Count > 0)
            Save.Visible = true;
    }

    protected void Save_Click(object sender, EventArgs e)
    {
        SqlCommand com = new SqlCommand("INSERT INTO [GRNDTL] (" +
            "[ProductID], [ProductName], [AdvisedQty], [AcceptedQty]) VALUES (" +
            "@ProductID, @ProductName, @AdvisedQty, @AcceptedQty)");
        com.Parameters.Add("@ProductID", SqlDbType.Int);
        com.Parameters.Add("@ProductName", SqlDbType.VarChar, 50);
        com.Parameters.Add("@AdvisedQty", SqlDbType.Int);
        com.Parameters.Add("@AcceptedQty", SqlDbType.Int);

        using (SqlConnection con = new SqlConnection(
            ConfigurationManager.ConnectionStrings["TestConnectionString"].ConnectionString))
        {

            con.Open();
            com.Connection = con;
            
            foreach (Row r in CopiedData)
            {
                com.Parameters["@ProductID"].Value = r.ProductID;
                com.Parameters["@ProductName"].Value = r.ProductName;
                com.Parameters["@AdvisedQty"].Value = r.AdvisedQty;
                com.Parameters["@AcceptedQty"].Value = r.AcceptedQty;
                com.ExecuteNonQuery();
            }

        }

        this.CopiedData.Clear();
        GridView2.DataBind();
        Save.Visible = false;

        Message.Visible = true;
        Message.Text = "Your data was successfully added to the database!";
    }

    [Serializable()]
    private class Row
    {
        public int ProductID { get; set; }
        public string ProductName { get; set; }
        public int AdvisedQty { get; set; }
        public int AcceptedQty { get; set; }
    }

Please let me know how it works for you.  Also, try to figure some things out yourself before posting back.  Thanks!

Regards,

~ mellamokb

0
mellamokb
11/28/2008 3:30:35 PM
Reply:

Similar Artilces:

Selecting a gridview row and keeping the last selected row in a gridview highlighted after mouse moves over a new row.
What is the best way to show a gridview row highlighted once selected and keep it highlighted once the mouse moves over a different row? Thanks, Steve hi,you can use <SelectedRowStyle BackColor="#008A8C" Font-Bold="True" ForeColor="#FFFFFF" /> plz let me know if i misunderstood you Thanks for your help. One problem; however, is that after the row has been selected and I check the "grid.SelectedIndex" it has a value of -1 and not the index of the row that was selected on the grid. What do you recommend? Thanks, Steve hi, sorry i din...

Using a row selection in a gridview to create database rows for another gridview
Hi there, I have 2 gridviews on a form, the first displays some template rows from a database table.  The second displays some user specific rows (with additional data added to the template info) from the same table. They both work fine for displaying the info.  Now what I want to do, is allow users to 'select' a template row on the first gridview, and have that action automatically: create a new user-specific row, with added fields from profile data etc. insert it into the table then refresh the second gridview in order to show the new added row. C...

Gridview paging enabled. Has checkbox to select the row . If user select the checkbox on one page and move to 2nd page to select the rows there also to export the selected rows to excel. But doing so
HiI am using Gridview and that has paging enabled. Using checkbox to select the particular row of the gridview to export the selected rows to the excel . Everything is working fine except for one case.Let user selected the "check" box for, five rows which user would like to export to excel (two of rows on page 1 of results and 3 of them on page 2 of results), however it only exports the 3 row details from page 2, because it seems to de-select the 2 rows from page 1 when I move to page 2 and vice versa. The code i am using is as :protected void BtnExcel_Click(object sender, EventArg...

Insert row above selected row in GridView
Hi, I've been asked to create a list with an insert button at the end of each row that, when clicked, will insert a new line above the selected row and allow the user to enter in information.  It won't be an edit, but a new entry - however it needs to be displayed above the row with the "Insert" button.  I'm not sure if this is possible or not, but any help would be most appreciated. Thanks! Check this articlehttp://fredrik.nsquared2.com/viewpost.aspx?PostID=201Hope my suggestion helps Regards,Vinz"Code, Beer and Music" that's my way of ...

Data Controls insert rows into gridview then insert rows from gridview into SQL Table
Hello there, I'm still new at performing miracles with code . Currently I am having trouble with my latest coding adventure I have a web application which records jobs and labour assigned to jobs. The issue in particular I'm having is the way in which labour is to be assigned. I want to use standard controls (Textbox, Dropdownlist) to record the values into a gridview (ie,put multiple rows into the gridview using the controls). Then, when all the inserting is complete, the user clicks a submit button to insert all the rows from the gridview into a database table. The reason for...

populate a gridview from a selected row in another gridview
can someone give me an overview of how to grab a row from a gridview with a select button that is clicked to populate another gridview   Here is the markup for my gridview<asp:GridView ID="grdContacts" runat="server" BackColor="#DEBA84" BorderColor="#DEBA84" BorderStyle="None" BorderWidth="1px" CellPadding="3" CellSpacing="2" AutoGenerateColumns="False"> <FooterStyle BackColor="#F7DFB5" ForeColor="#8C4510" /> <RowStyle BackColor="#FFF7E7" ForeCol...

select multiple rows with its row highlighted in GridView
Hello everyone, I'm not using any checkbox or select command to select the row in the GridView, I use the "RowDataBound" event in the GridView for the "onmouseover" click and row select.  That works fine for me but I want to have the functionality to select a multiple rows with its row highlighted and have a row selected count. For example, If I select a row, it hightlights that row and if I select another row, it will do the same thing and have the previous row still highlighted.  I have been looking for a way to implement this but can't find a ...

Problem in Using DropDownList Control to select a selected row of SqlDataSource that is under GridView Control
Hi all,In my SQL Server Management Studio Express, I have a database "ChemDatabase" that has a table "LabData"  formatted in the following way: SampleID int PRIMARY KEY, SampleName nvarchar(25), ChemicalName, nvarchar(25), Concentration decimal(6.2) with 5 rows of records in this .dbo table. In my Visual Web Developer Express, I created a website "RPD-TCE" for extracting a row of record from the LabData table by using a DropDownList, a GridView and a SqlDataSource.  I want to click DropDownList to list one number of S...

new row below selected row in gridview to display child gridview
Hello,   I have a gridview that up selected..shows the nested child grivdview. The problem I currently have is that it stuffs all the data into a column whcih blows the whole tbale out of whack. What I need to do is create a new row below the sleected row to house the new child grivdview. I have seen third party controls do this such as teleriks radgrid but I wont to do this with the gridview control.  Any articles on how to do this? Does AJAX support this? Any help is greatly appreciated.    ...

Gridview Hyperlink Select Row & State on Row?
I have a gridview that has a hyperlink in it that I use to create querystring to filter a second gridview on the same page.  Is it possible for the hyperlink to select the row that you click on and then stay on that row when it reloads?  Right now when I click on it it resets the gridview state and of course doesn't select the row. I've tried a few of the hacks but I couldn't get any to work.  Here is the gridview and the code I've triedGridview Source1 protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e) 2 { 3 i...

Select a row in GridView and show dependent Values in Other GridView on the basis of selection...
hi, i've a parent table which has many values in it.. i also have some child tables which contains other information.. these tables are linked through forein keys... now what i want to do is, i want to select a value from main table suppose, ID 10, and i want to display it in other gridview having the children table values of the ID 10. how can i achieve this scenario? Please let me know if i am not clear with my statements. Thank U-Jigzy-Please remember to click “Mark as Answer” on the post that helps you.This can be beneficial to other members reading the thread. You can have y...

Add new controls to a gridview row; when editing that row using the edit row feature in the gridview control.
Hi all  I have a questions that looks hard / complex to me, and i know you can help me in this. Q {  I have row in a Gridview and i have an edit option to it. I press the edit button, to edit the values in that row and update data all the way back to the database. Now when i press the edit button, i want a drop down list to be displayed that contain values for a particular field in my grid view. (for now only plain text boxes appear.) } I hope you understand my question, all i need to do is replace the text box controls with a dropdown list when i am editing a row in ...

Iwant to select a row and column in gridview with select radiobutton. but No checked change event.below put a button .if click that button to seleced row values will select
I want to select a row and column in gridview using radiobutton in gridview . if which radio button is select that row will select and i want that row column values in outside individual textboxes of next page.  but in that only single radiobutton will select and no post back="true" bcz iam not using checked change property. i inserted button below gridview. if i click the that selected row and columns values are stored into textboxes of next page im using this code .but im getting one column in that row. but i want all columns in that selected row so plz repla...

Select multiple rows in GridView without using a Select Command ?
Hello, Does anyone knows a way to select multiple rows in GridView without using a Select command, control button or a checkbox control? Thank you very much. olaamigos:Does anyone knows a way to select multiple rows in GridView without using a Select command, control button or a checkbox control?  I believe that this is what you are looking for:GridView Clickable Rows Regards,Vinz"Code, Beer and Music" that's my way of being a programmer!How to get your Forum Question Answered | Blog | CodeASP.NET Thanks Vinz for replying, The  sample in the "GridView Cl...

Web resources about - Selecting the multiple rows in GridView and inserting the selected rows to another Gridview & Performing some operations on it - asp.net.presentation-controls

University of Music and Performing Arts, Vienna - Wikipedia, the free encyclopedia
The University of Music and Performing Arts, Vienna ( German : Universität für Musik und darstellende Kunst Wien , abbreviated MDW ) is an Austrian ...

Canberra Highland dancer performing at Royal Edinburgh Military Tattoo Melbourne
A Charnwood resident is one of the performers in the upcoming event.

Perth’s top performing suburbs for median real estate price increase
A LITTLE known suburb with a population of less than 1500 is Perth’s top performing suburb for median house price growth.

Property report: WA’s top performing suburbs in last 12 months - PerthNow Search Search
LAND value has pushed Furnissdale, near Mandurah, to become the state’s top performing suburb.

Building High-Performing Organizations Game played at GOAT15
... Agile Tour for yet another year, and Mark debuted a brand new game as part of his GOAT15 presentation. “Beyond Scrum: Building High-Performing ...

Every website can be improved even the best performing ones
Your website content should be dynamic and always changing as your business realigns its goals and audiences to keep pace with a changing landscape. ...

Greek Banks' Non-Performing Loans Hit 43%; Only €14 Billion Recapitalization Needed?
... (undoubtedly stress-free). The ECB's adverse scenario shows Greek banks only underfunded to the tune of €14 billion. " Greek Banks' Non-Performing ...

Mike Birbiglia making new movie w/ Keegan-Michael Key, Chris Gethard & more ++ performing at LGBTQ benefit ...
Mike and the cast of 'Don't Think Twice' Mike Birbiglia wrote, directed and starred in 2012's acclaimed Sleepwalk with Me. He's got a new movie ...

American band Eagles of Death Metal was performing at attacked Paris venue
Management has not heard from the band scheduled to play at the Paris venue where at least 100 people were reportedly killed

Pitbull Talks Performing at Sofia Vergara and Joe Manganiello's Wedding: 'Sofia Knows How to Have a Good ...
Pitbull Talks Performing at Sofia Vergara and Joe Manganiello's Wedding: 'Sofia Knows How to Have a Good Time'

Resources last updated: 12/3/2015 4:39:32 AM