Solved

How to Display Selected Row in grdview in another Gridview?

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
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…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

758 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now