Solved

How to Display Selected Row in grdview in another Gridview?

Posted on 2014-01-20
4
547 Views
Last Modified: 2014-01-21
Hi,

Can u plz help me to display grid view when row selected from another gridview
0
Comment
Question by:mannevenu26
[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
4 Comments
 
LVL 35

Assisted Solution

by:Robert Schutt
Robert Schutt earned 500 total points
ID: 39794199
You could do it like this for example:

ShowGrid.aspx:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="ShowGrid.aspx.cs" Inherits="EE_Q_28342971_WEB.ShowGrid" %>

<!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> EE Q_28342971 </title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:GridView ID="GridView1" runat="server" AutoGenerateSelectButton="true" OnSelectedIndexChanged="SetGridView2">
        </asp:GridView>
        <hr />
        <asp:GridView ID="GridView2" runat="server">
        </asp:GridView>
    </div>
    </form>
</body>
</html>

Open in new window

ShowGrid.aspx.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace EE_Q_28342971_WEB {
    public partial class ShowGrid : System.Web.UI.Page {

        DataTable dt = new DataTable();

        protected void Page_Load(object sender, EventArgs e) {
            dt.Columns.AddRange(new DataColumn[] { new DataColumn("id", typeof(int)), new DataColumn("name", typeof(string)) });
            dt.Rows.Add(new object[] { 1, "Peter" });
            dt.Rows.Add(new object[] { 2, "Paul" });
            dt.Rows.Add(new object[] { 3, "Mary" });
            GridView1.DataSource = dt;
            GridView1.DataKeyNames = new string[] {"id"};
            GridView1.EnablePersistedSelection = true;
            GridView1.DataBind();
        }

        protected void SetGridView2(object sender, EventArgs e) {
            DataView dv = new DataView(dt, "id = " + GridView1.SelectedPersistedDataKey["id"], "", DataViewRowState.CurrentRows);
            GridView2.DataSource = dv;
            GridView2.DataBind();
        }
    }
}

Open in new window

Note: just grab the code you need, ignore the namespace stuff and test data.
0
 

Author Comment

by:mannevenu26
ID: 39796103
can u plz give example for

how to select particular value in a grid view and assign to local variable(local varible is integer).
0
 
LVL 12

Expert Comment

by:deanvanrooyen
ID: 39796246
0
 
LVL 35

Accepted Solution

by:
Robert Schutt earned 500 total points
ID: 39796287
Did I misunderstand or is that another question?

If I understand correctly you could do that as follows; I added a button and a label and on button click it shows the name of the selected person (in this case the second cell of the first row in GridView2).

ShowGrid.aspx:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="ShowGrid.aspx.cs" Inherits="EE_Q_28342971_WEB.ShowGrid" %>

<!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> EE Q_28342971 </title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:GridView ID="GridView1" runat="server" AutoGenerateSelectButton="true" OnSelectedIndexChanged="SetGridView2">
        </asp:GridView>
        <hr />
        <asp:GridView ID="GridView2" runat="server">
        </asp:GridView>
        <hr />
        <asp:Button ID="Button1" runat="server" Text="Button" OnClick="ShowName" /> - <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
    </div>
    </form>
</body>
</html>

Open in new window

ShowGrid.aspx.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace EE_Q_28342971_WEB {
    public partial class ShowGrid : System.Web.UI.Page {

        DataTable dt = new DataTable();

        protected void Page_Load(object sender, EventArgs e) {
            dt.Columns.AddRange(new DataColumn[] { new DataColumn("id", typeof(int)), new DataColumn("name", typeof(string)) });
            dt.Rows.Add(new object[] { 1, "Peter" });
            dt.Rows.Add(new object[] { 2, "Paul" });
            dt.Rows.Add(new object[] { 3, "Mary" });
            GridView1.DataSource = dt;
            GridView1.DataKeyNames = new string[] {"id"};
            GridView1.EnablePersistedSelection = true;
            GridView1.DataBind();
        }

        protected void SetGridView2(object sender, EventArgs e) {
            DataView dv = new DataView(dt, "id = " + GridView1.SelectedPersistedDataKey["id"], "", DataViewRowState.CurrentRows);
            GridView2.DataSource = dv;
            GridView2.DataBind();
        }

        protected void ShowName(object sender, EventArgs e) {
            Label1.Text = GridView2.Rows[0].Cells[1].Text;
        }
    }
}

Open in new window

Here is how it looks for me:
capture of output
0

Featured Post

More Than Just A Video Library

Train for your certification. Learn the latest DevOps tools. Grow your skillset to do better work.

At Linux Academy, we release new training modules every week so you'll always be up to date on the latest tech.

Question has a verified solution.

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

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

695 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