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

Problem accessing the SelectedValue of a programatically created RadGrid

2 Answers 113 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Jonathan
Top achievements
Rank 1
Jonathan asked on 01 Dec 2011, 07:14 PM
Hello, 

I am having difficulty with the problem of accessing the Selected value of a programatically created RadGrid.  The purpose of this app is to allow a user to select an order from a grid and open a new page with a new grid populated based on the selection of the previous grid.  I can't seem to find an example like this in the forums.  Most example assume the grid is bound directly, however I am pulling from a programatically created grid.  I am new to Telerik controls.  I tried using the format code block but it made my code unreadable.

Here is how I create the grid:

    <telerik:RadGrid ID="OrdersGrid" runat="server" Skin="WebBlue" CellSpacing="0" GridLines="None"
        ClientSettings-Selecting-AllowRowSelect="True">
        <ClientSettings>
            <Selecting AllowRowSelect="True" />
            <Scrolling AllowScroll="True" UseStaticHeaders="True" />
        </ClientSettings>
        <MasterTableView DataKeyNames="OrderID" ClientDataKeyNames="OrderID">
            <CommandItemSettings ExportToPdfText="Export to PDF" />
            <RowIndicatorColumn FilterControlAltText="Filter RowIndicator column">
                <HeaderStyle Width="20px" />
            </RowIndicatorColumn>
            <ExpandCollapseColumn FilterControlAltText="Filter ExpandColumn column">
                <HeaderStyle Width="20px" />
            </ExpandCollapseColumn>
            <EditFormSettings>
                <EditColumn FilterControlAltText="Filter EditCommandColumn column">
                </EditColumn>
            </EditFormSettings>
        </MasterTableView>
        <FilterMenu EnableImageSprites="False">
        </FilterMenu>
        <HeaderContextMenu CssClass="GridContextMenu GridContextMenu_WebBlue">
        </HeaderContextMenu>
    </telerik:RadGrid>    <telerik:RadGrid ID="OrdersGrid" runat="server" Skin="WebBlue" CellSpacing="0" GridLines="None"
        ClientSettings-Selecting-AllowRowSelect="True">
        <ClientSettings>
            <Selecting AllowRowSelect="True" />
            <Scrolling AllowScroll="True" UseStaticHeaders="True" />
        </ClientSettings>
        <MasterTableView DataKeyNames="OrderID" ClientDataKeyNames="OrderID">
            <CommandItemSettings ExportToPdfText="Export to PDF" />
            <RowIndicatorColumn FilterControlAltText="Filter RowIndicator column">
                <HeaderStyle Width="20px" />
            </RowIndicatorColumn>
            <ExpandCollapseColumn FilterControlAltText="Filter ExpandColumn column">
                <HeaderStyle Width="20px" />
            </ExpandCollapseColumn>
            <EditFormSettings>
                <EditColumn FilterControlAltText="Filter EditCommandColumn column">
                </EditColumn>
            </EditFormSettings>
        </MasterTableView>
        <FilterMenu EnableImageSprites="False">
        </FilterMenu>
        <HeaderContextMenu CssClass="GridContextMenu GridContextMenu_WebBlue">
        </HeaderContextMenu>
    </telerik:RadGrid>

Here is how I bind the data to the new grid:

protected void Page_Load(object sender, EventArgs e)
        {
            using (Dbcontext)
            {
                companyID = Convert.ToInt32(Request.QueryString["CompanyID"]);
                OrdersGrid.DataSource = Dbcontext.usp_SelectOrders(null,companyID);
                OrdersGrid.DataBind();
                OrdersGrid.MasterTableView.DataKeyNames = new String[] { "OrderID" };
             }
        }

Here is how I am attempting to access the SelectedValue of the OrdersGrid:

protected void OrderDetailsButton_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/Pages/OrderDetails.aspx?OrderID=" + OrdersGrid.SelectedValue.ToString());
        }

When the button is clicked I get an error stating that the Selected value has not been initialized.  When I hover over the SelectedValue in Debugging mode it says it is "null."

Any thoughts?

Thanks in advance,
Jonathan

2 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 02 Dec 2011, 05:39 AM
Hello Jonathan,

I tried the same scenario which worked as expected in my end. Also try binding the grid with Advanced databinding or in the PageInit method.
C#:
protected void Button3_Click(object sender, EventArgs e)
{
   string val=RadGrid1.SelectedValue.ToString();
}

-Princy.
0
Jonathan
Top achievements
Rank 1
answered on 07 Dec 2011, 04:09 PM
Thank you for your response.  The issue was a session state issue.  I've resolved the problem.  

Thanks again for the help,
Jonathan
Tags
Grid
Asked by
Jonathan
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Jonathan
Top achievements
Rank 1
Share this question
or