Click here to Skip to main content
15,890,845 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I've been working in a project for some time now, and I'm having some hard times trying to figure out how to copy data from one gridview to another.

What I want to do is this: My web aplication will list all content the user have in a gridview (dynamically binded to a database), and it will have a button "+" that when clicked, it will copy that 2 columns of that row to another gridview.

I've tried some times and it works copy the very first row (no matter what row is that), but then it do not work anymore.

Here is the asp.net code:

C#
<div id="Musicas">
    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="Name" DataSourceID="ObjectDataSource1" OnRowCommand="GridView1_RowCommand">
        <Columns>
            <asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
            <asp:BoundField DataField="Gender" HeaderText="Gender" SortExpression="Gender" />
            <asp:BoundField DataField="Album" HeaderText="Album" SortExpression="Album" />
            <asp:BoundField DataField="ReleaseYear" HeaderText="ReleaseYear" SortExpression="ReleaseYear" />
            <asp:BoundField DataField="Composer" HeaderText="Composer" SortExpression="Composer" />
            <asp:BoundField DataField="Artist" HeaderText="Artist" SortExpression="Artist" />
            <asp:ButtonField ButtonType="Button" CommandName="Select" HeaderText="AddPlayList" ShowHeader="True" Text="+" />
        </Columns>
    </asp:GridView>
    <asp:ObjectDataSource ID="ObjectDataSource1" runat="server" OnSelecting="ObjectDataSource1_Selecting" SelectMethod="GetMusic" TypeName="BLL.GetUserInformation">
        <SelectParameters>
            <asp:Parameter Name="id" Type="String" />
        </SelectParameters>
    </asp:ObjectDataSource>

    <asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="False">
        <Columns>
        <asp:BoundField DataField="Name" HeaderText="Name" />
    </Columns>
    </asp:GridView>
</div>



And here is the code-behind:

C#
private List<Row> CopiedData
    {
        get
        {
            if (this.ViewState["CopiedData"] == null)
            {
                this.ViewState["CopiedData"] = new List<Row>();
            }

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

    protected void Page_Load(object sender, EventArgs e)
    {

    }


    protected void ObjectDataSource1_Selecting(object sender, ObjectDataSourceSelectingEventArgs e)
    {
        e.InputParameters[0] = User.Identity.Name;
    }

    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName.Equals("Select"))
        {
            GridViewRow gvr = GridView1.Rows[Convert.ToInt32(e.CommandArgument)];
            int id = Convert.ToInt32(GridView1.DataKeys[gvr.RowIndex]["Name"]);
            string character = gvr.Cells[0].Text;
            Row row = new Row { Id = id, Name = character };

            if (!this.CopiedData.Exists(r => r.Id == id))
            {
                this.CopiedData.Add(row);

                GridView2.DataSource = this.CopiedData;
                GridView2.DataBind();
            }
        }
    }


    [Serializable()]
    private class Row
    {
        public int Id { get; set; }
        public string Name { get; set; }
    }


The issue I'm having is in this line of code:

int id = Convert.ToInt32(GridView1.DataKeys[gvr.RowIndex]["Name"]);
for it always retuns 0 to id, making the if (!this.CopiedData.Exists(r => r.Id == id)) always return false, so it do not update the code after the first line was selected.

Can anyone help me with this?

Thanks in advance and sorry about my poor english, its not my main language.
Posted
Comments
Mahesh Bailwal 5-Jul-13 12:10pm    
What values you have in Name Column? Give some example.
Macézar Alves 5-Jul-13 14:55pm    
Music names.
ZurdoDev 5-Jul-13 13:02pm    
Is there a reason you don't handle this in SQL?
Macézar Alves 5-Jul-13 14:56pm    
The first gridview will get data from sql server, what I want to do is select then some of the music Ids from one grindview, and pass to another, and then, I'll save the the data from gridview2 in sql-server again.

1 solution

I think first you take you grid view data to data table or data set then remove column which you don't want then bind that datatable to your second column.

Refer below link for how to do that:

http://stackoverflow.com/questions/785799/how-can-i-export-a-gridview-datasource-to-a-datatable-or-dataset[^]

how to get gridview data in dataset in asp.net[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900