Click here to Skip to main content
15,891,431 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am displaying different gridview based on selected row in gridview.


C#
Protected void Bindgrid1()
 {
        DataTable dt = new DataTable();
        dt.Columns.AddRange(new DataColumn[5] { new DataColumn("Date"), new DataColumn("Location"), new DataColumn("TimeIn"), new DataColumn("TimeOut"), new DataColumn("Hours") });
        dt.Rows.Add("1-01-2018", "Chennai", "9.30", "6.30", "9");
        dt.Rows.Add("2-01-2018", "Chennai", "9.30", "7.30", "10");
        dt.Rows.Add("3-01-2018", "Chennai", "9.30", "5.30", "8");
        dt.Rows.Add("4-01-2018", "Chennai", "9.30", "8.00", "10.30");
        dt.Rows.Add("5-01-2018", "Chennai", "9.30", "7.00", "9.30");
        GridView1.DataSource = dt;
        GridView1.DataBind();
 }

  Protected void Bindgrid2()
    {
        DataTable dt = new DataTable();
        dt.Columns.AddRange(new DataColumn[5] { new DataColumn("Date"), new 
        DataColumn("Location"), new DataColumn("TimeIn"), new DataColumn("TimeOut"), 
        new DataColumn("Hours") });

        dt.Rows.Add("8-01-2018", "Chennai", "9.30", "7.00", "9.30");
        dt.Rows.Add("9-01-2018", "Chennai ", "10.30", "7.30", "9");
        dt.Rows.Add("10-01-2018", "Chennai", "11.30", "5.30", "6");
        dt.Rows.Add("11-01-2018", "Chennai", "10.00", "8.00", "10.00");
        dt.Rows.Add("12-01-2018", "Chennai", "9.00", "7.00", "10.00");

        GridView2.DataSource = dt;
        GridView2.DataBind();
 }

In run mode as follows

Date      Location   Timein   TimeOut   Workinghours   ViewAttendance
 
         1/1/2018   Chennai    9.30      6.30          9          Clickhere
         2//12018   Chennai    9.30      7.30          10         Clickhere
         3/1/2018   Chennai    9.30      5.30          8          Clickhere
         4/1/2018   Chennai    9.30      8.00          10.30      Clickhere
         5/1/2018   Chennai    9.30      7.00          9.30       Clickhere


in the run mode when i click the first row in gridview i want to visible the Gridview1.


Simliarily when i click the second row in gridview i want to visble the Gridview2.

for selecting gridview based on each row i written a code as follows

C#
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
     {

      //in gridview First row selected show the gridview1.    Bindgrid1 method

  if (e.Row.RowType == DataControlRowType.DataRow)
         {
             string username = Convert.ToString(DataBinder.Eval(e.Row.DataItem, "Date"));
             LinkButton lnkbtnresult = (LinkButton)e.Row.FindControl("lnkdelete");
             BindGrid1();
             Gridview1.visible = true;
         }

        //in gridview second row selected show the gridview2.      Bindgrid2 method

         if (e.Row.RowType == DataControlRowType.DataRow)
         {
             string username = Convert.ToString(DataBinder.Eval(e.Row.DataItem, "Location"));
             LinkButton lnkbtnresult = (LinkButton)e.Row.FindControl("lnkdelete");
             BindGrid2();
             Gridview2.visible = true;
         }
     }


But when i click the second row in gridview, it shows the gridview1 only, i want to visible the gridview2.

from my above code what is the mistake.

What I have tried:

See above
Posted
Updated 1-Feb-18 21:49pm
v2
Comments
Ziee-M 1-Feb-18 11:00am    
Hi sudhakarthikeyan, you are using the wrong events to achive your goal.
Here is how you should do it :
1-Page load event : Bind your GridView1
2-GridView1SelectedIndexChange event:
You get the information you need from the Grid1, and use it to bind Grid2.
Thats it.
You should however study a little more the events flow/order for Asp.net to get a clearer idea of Asp.net life cycle.
[no name] 2-Feb-18 0:58am    
ok what changes i have to made in my above code please tell me

1 solution

Hi Sudhakarthikeyan,

The GridView1_RowDataBound event is useless in your case, you need the selectedIndexChange event instead.
Here is how you rcode should look like :
C#
protected void Page_Load(object sender, EventArgs e)
{
  if (!PostBack)
{ 
Bindgrid1(); //when the page is first run, bind the GridView1
}
}

//execute whenever the user select a row from gridview1
void GridView1_SelectedIndexChanging(Object sender, GridViewSelectEventArgs e)
  { 
    GridViewRow row = GridView1.Rows[e.NewSelectedIndex];

     //add your logic to filter data
BindGrid2(); //bind grid 2
  }

That should do it. however, you are using a Object Oriented Programming, things like this line should be refactored :
dt.Rows.Add("5-01-2018", "Chennai", "9.30", "7.00", "9.30");

You should create a class instead of filling the data manually.

Hope it helps.
 
Share this answer
 
Comments
[no name] 2-Feb-18 6:14am    
it is not working. when i click the second row in gridview click here . it shows the gridview 1 in popup only.

MY gridview html source code as follows

Gridview1 html source

<asp:GridView ID="GridView1" runat="server" AllowPaging="True" AutoGenerateColumns="False" CellPadding="10"
CellSpacing="0" OnRowDataBound="GridView1_RowDataBound" />
<columns>
<asp:BoundField DataField="Date" HeaderText="Date" />
<asp:BoundField DataField="Location" HeaderText="Location" />
<asp:BoundField DataField="TimeIn" HeaderText="TimeIn" />
<asp:BoundField DataField="TimeOut" HeaderText="TimeOut" />
<asp:BoundField DataField="WorkingHours" HeaderText="WorkingHours" />
<asp:BoundField DataField="id" HeaderText="id" />

<asp:TemplateField HeaderText="ViewAttendance">
<itemtemplate>
<asp:LinkButton ID="lnkdelete" href="#myModal" data-toggle="modal" runat="server">Click Here







Gridview 2 Html source





×



<asp:Label ID="lblmessage" runat="server" ClientIDMode="Static">

<asp:GridView ID="gridview2" runat="server" CellPadding="6" CellSpacing="3" ForeColor="#333333" GridLines="None" Width="570px">
<AlternatingRowStyle BackColor="White" />
<EditRowStyle BackColor="#2461BF" />
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" CssClass="gridHeaderAlignRight" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#EFF3FB" />
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#F5F7FB" />
<SortedAscendingHeaderStyle BackColor="#6D95E1" />
<SortedDescendingCellStyle BackColor="#E9EBEF" />
<SortedDescendingHeaderStyle BackColor="#4870BE" />





Close
Ziee-M 2-Feb-18 7:08am    
In the asp:gridview tag add the OnSelectedIndexChanged= "GridView1_SelectedIndexChanging" to subscrive to the event.
<asp:GridView ID="GridView1" runat="server" AllowPaging="True" AutoGenerateColumns="False" CellPadding="10"
CellSpacing="0" OnRowDataBound="GridView1_RowDataBound" OnSelectedIndexChanged= "GridView1_SelectedIndexChanging"/>
Remove your RowDatabound code, and add your logic in the OnSelectedIndexChanged event

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900