?
Solved

Passing Hidden Boundfield to Nested GridView using DataKeyNames

Posted on 2013-10-22
4
Medium Priority
?
1,065 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 1500 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

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
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!
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…

649 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