Solved

How to Display Selected Row in grdview in another Gridview?

Posted on 2014-01-20
4
521 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

Independent Software Vendors: 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

Suggested Solutions

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

749 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