?
Solved

How to Display Selected Row in grdview in another Gridview?

Posted on 2014-01-20
4
Medium Priority
?
569 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 2000 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 2000 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
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…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Suggested Courses

764 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