This is a migrated thread and some comments may be shown as answers.

[Solved] Pass ItemID to Grid on User Control Form

3 Answers 236 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Brett
Top achievements
Rank 1
Brett asked on 09 Apr 2008, 08:59 AM

I have a RadGrid on my main page and I created a user control form to edit the data in the Grid. In that user control form I put another RadGrid the form with a GridDropDownColumn.

When you click "Edit" on the main grid, it'll bring up the user control form displaying the second RadGrid with all the items associated with current item that they're editing. Then when user adds an item to the second grid, they can select the item from the dropdown (there are over 60 different choices, and of those 60, you could have 60 items associated with that one item, hence why I went with another RadGrid in the user control form to add/delete).

On my User control form I put another SqlDataSource with a Select * From table Where ItemID = @ItemID

What I'm wondering is, is there a way to return the ItemID from the main Grid to the Grid on the user control form?

So that when you click "Edit" on the main page, it'll pass the ItemID to the user control from the main form and the select statement will only return all the items associated with that ID for the Grid on the user control form.

Thanks,

Brett

3 Answers, 1 is accepted

Sort by
0
Iana Tsolova
Telerik team
answered on 11 Apr 2008, 02:52 PM
Hi Brett,

You can use the DataItem property defined in your UserControl as described in this online help topic. The object it sets is a DataRowView which contains information about for all the fields of your edited row. This way, you can find the ItemID in that collection.

Sincerely yours,
Iana
the Telerik team

Instantly find answers to your questions at the new Telerik Support Center
0
Brett
Top achievements
Rank 1
answered on 13 Apr 2008, 08:22 AM

Iana,

Thank you for your response, I'm able to populate my RadGrid with the the item that I'm editing from the RadGrid on the main page. It populates the RadGrid on the user control fine perfectly. Yay!

I do get an error when trying to add a record to the RadGrid on the user control form, which is probably due to lack of experience. I might be completely going about this the wrong way too.

When I go to add a record I get: System.NullReferenceException: Object reference not set to an instance of an object. I'm pretty sure that I'm getting this error because my dataItem is null after the page has been loaded and I'm guessing that when I go to add a record in the RadGid it must do a refresh on the page or something, and sets it to null.

To populate the RadGrid on my user control form I put a SqlDataSource and created my select statement. To pass the ItemID I created an event on my SqlDataSource like this:

protected void ServiceState_Selecting(object sender, SqlDataSourceSelectingEventArgs e)  
    {  
            e.Command.Parameters["@ItemID"].Value = (DataBinder.Eval(DataItem, "ItemID").ToString());  
    } 

When I click add record on the RadGrid I get the System.NullreferenceException on the dataBinder.Eval(DataItem, "ItemID").ToString()).

If there is a better way of doing this or if you have any nudges in the right direction, I'd appreciate it.

Thanks,

Brett

0
Iana Tsolova
Telerik team
answered on 16 Apr 2008, 12:15 PM
Hello Brett,

The DataItem object is only available when the grid is bound (inside the ItemDataBound handler). And when you try to add new record from the inner grid, after the postback, it is already null. 

That's why I suggest you to add your ItemID to the DataKeyNames collection of the main grid which is always available. Thus you can bind the grid in the user control as shown below:

protected void AccessDataSource1_Selecting(object sender, SqlDataSourceSelectingEventArgs e)  
{  
    e.Command.Parameters["ItemID"].Value =   
        (this.Parent.NamingContainer as GridEditFormItem).ParentItem.GetDataKeyValue("ItemID");  

Let us know if this helps.
 
Best regards,
Iana
the Telerik team

Instantly find answers to your questions at the new Telerik Support Center
Tags
Grid
Asked by
Brett
Top achievements
Rank 1
Answers by
Iana Tsolova
Telerik team
Brett
Top achievements
Rank 1
Share this question
or