Solved

Passing Hidden Boundfield to Nested GridView using DataKeyNames

Posted on 2013-10-22
4
1,014 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
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

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…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

839 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