Solved

Passing Hidden Boundfield to Nested GridView using DataKeyNames

Posted on 2013-10-22
4
991 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
  • 3
4 Comments
 
LVL 22

Accepted Solution

by:
Mrunal earned 500 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
The reference material had some errors, but ultimately worked after some trial and error.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

772 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

10 Experts available now in Live!

Get 1:1 Help Now