Link to home
Start Free TrialLog in
Avatar of bertmn
bertmn

asked on

Passing Hidden Boundfield to Nested GridView using DataKeyNames

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
ASKER CERTIFIED SOLUTION
Avatar of Mrunal
Mrunal
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of bertmn
bertmn

ASKER

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
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of bertmn

ASKER

The reference material had some errors, but ultimately worked after some trial and error.