Solved

Passing Hidden Boundfield to Nested GridView using DataKeyNames

Posted on 2013-10-22
4
1,029 Views
Last Modified: 2013-10-30
Hi,
I have a hidden boundfield that is in a parent GridView

<asp:BoundField DataField="Name" HeaderText="Name" ReadOnly="true"
                    SortExpression="Name" Visible="false" />

I would like to use DataKeyNames

DataKeyNames="Name"

On the script side I am using this code example to pass what I need from GridView1 to GridView2

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            SqlDataSource s = (SqlDataSource)e.Row.FindControl("GridView2");
            s.SelectParameters[0].DefaultValue = e.Row.Cells[0].Text;
        }
    }


It works fine if I make the "Name" value visible but as soon as I hide, it doesnt work.
I understand that that the script code (example from MSDN site) will not pull from the DataKeyName but pulls from the first boundfield. How can I use the DataKeyName in the same way to get this to work? Also from looking at other questions I can't have the Name value converted to an int as it errors.

TIA
0
Comment
Question by:bertmn
[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
  • 3
4 Comments
 
LVL 22

Accepted Solution

by:
Mrunal earned 500 total points
ID: 39593847
You can assign DataKey to parent gridview for "Name".

After that read this datakey like:

http://www.aspdotnet-suresh.com/2012/06/get-multiple-datakeyname-values-from.html

and pass to child gridview.

More reference:

stackoverflow.com/questions/2818203/get-datakey-values-in-gridview-rowcommand

http://www.codeproject.com/Articles/23833/DataKeyNames
0
 

Author Comment

by:bertmn
ID: 39595238
Hi,
Using the reference you posted I was able to sort of get things working.
If I use this code

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        foreach (GridViewRow gvrow in GridView1.Rows)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                SqlDataSource s = (SqlDataSource)e.Row.FindControl("GridView2");
                s.SelectParameters[0].DefaultValue = GridView1.DataKeys[gvrow.RowIndex].Values[0].ToString();
            }
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                SqlDataSource s = (SqlDataSource)e.Row.FindControl("GridView2");
                s.SelectParameters[1].DefaultValue = GridView1.DataKeys[gvrow.RowIndex].Values[1].ToString();
            }
        }
    }

Open in new window


It works but it offsets the data by 1. So parent row 1 gets no child data while parent row 2 gets the child data that should go into parent row 1 and so on.

If I use this code

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
	 GridViewRow gvrow;
	 string value1 = GridView1.DataKeys[gvrow.RowIndex].Values[0].ToString();
	 string value2 = GridView1.DataKeys[gvrow.RowIndex].Values[1].ToString();
            
	    if (e.Row.RowType == DataControlRowType.DataRow)
            {
                SqlDataSource s = (SqlDataSource)e.Row.FindControl("GridView2");
                s.SelectParameters[0].DefaultValue = value1;
            }
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                SqlDataSource s = (SqlDataSource)e.Row.FindControl("GridView2");
                s.SelectParameters[1].DefaultValue = value2;
            }
    }

Open in new window


I get the error: CS0165: Use of unassigned local variable 'gvrow'

I used the reference material from this link you posted.
http://www.aspdotnet-suresh.com/2012/06/get-multiple-datakeyname-values-from.html

It had the best reference.

Edit. I am using two DataKeys my original question I had only posted for one DataKey.

TIA
0
 

Assisted Solution

by:bertmn
bertmn earned 0 total points
ID: 39601677
Ok I got it to work.

This was the final code that worked.
 
Default.aspx
DataKeyNames="Name, Name2"

Open in new window


Default.aspx.cs
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                SqlDataSource s = (SqlDataSource)e.Row.FindControl("GridView2");
                s.SelectParameters[0].DefaultValue = GridView1.DataKeys[e.Row.RowIndex].Values["Name"].ToString();
            }
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                SqlDataSource s = (SqlDataSource)e.Row.FindControl("GridView2");
                s.SelectParameters[1].DefaultValue = GridView1.DataKeys[e.Row.RowIndex].Values["Name2"].ToString();
            }
    }

Open in new window


For anyone using this info, The original question was for one datakeyname but that was just to simplify the question. I was really using two datakeynames. The code in this comment works.
0
 

Author Closing Comment

by:bertmn
ID: 39611051
The reference material had some errors, but ultimately worked after some trial and error.
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

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.…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
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…

688 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