Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

How to Display Selected Row in grdview in another Gridview?

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

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

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …

604 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