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

Retrieve column names and values on row selection

3 Answers 416 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Stefan
Top achievements
Rank 1
Stefan asked on 18 May 2009, 01:44 PM
Hi,

I am busy doing R&D to see if the RadGrid will work for us. So far it has been working fine, but I am struggling to retrieve a selected row's column names and values similar to the way you would do that for a DataTable. However, the e.Item.DataSetIndex appears to return the index of the current grid, not  the entire datatable bound to it which is incorrect for m purposes. I am using standard paging of 10 rows per page. I need a way to get hold of one or more column values for the current row selected, either by looking at the databable with the correct row index (as I have tried) or directly in the grid row itself.

The way I am implementing the RadGrid includes the following:

1. Making use of AutoGeneratedColumns
2. Binding DataTable to RadGrid followed by DataBind() event.
3. Subscribed to event handler OnNeedDataSource to rebind grid on postback.
4. Subscribed to event handler OnItemCommand to try and retrieve current row's values as described.

Here's a basic overview of my page class:

        protected void Page_Load(object sender, EventArgs e)
        {
                if (!Page.IsPostBack)
                {
                    BindGrid();
                }
        }

        private void BindGrid()
        {
            //Instantiate Buss Object
            buss_Menus = new CS_Business.CS_Menus(Session.SessionID);

            //Select Menu(s) from DB
            ds = buss_Menus.Select();
            
            Session["dataview_menus_list"] = ds.Tables[0].DefaultView;

            dv = (DataView)Session["dataview_menus_list"];

            rgDataGrid.DataSource = dv;
            rgDataGrid.DataBind();
        }

        protected void rgDataGrid_NeedDataSource(object source, GridNeedDataSourceEventArgs e)
        {
            dv = (DataView)Session["dataview_menus_list"];
            rgDataGrid.DataSource = dv;
        }

        protected void rgDataGrid_ItemCommand(object source, GridCommandEventArgs e)
        {
                DataView dv = (DataView)Session["dataview_menus_list"];
                DataRow dr = dv.Table.Rows[e.Item.DataSetIndex];
                string mnuName = dr["FRONTEND_NAME"].ToString();    //This is where the problem is right now
        }

Any help here would be appreciated.

Stefan Buys

3 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 19 May 2009, 06:25 AM
Hello Stefan,

You can try out the following code to retrieve cell values for the currently selected row in the grid:
aspx:
 <telerik:GridBoundColumn DataField="ProductName" HeaderText="Product Name"  UniqueName="ProductName"
 </telerik:GridBoundColumn> 
 <telerik:GridBoundColumn DataField="ProductID" HeaderText="Product ID"  UniqueName="ProductID"
 </telerik:GridBoundColumn> 
                

c#:
protected void RadGrid1_PreRender(object sender, EventArgs e) 
    { 
        foreach(GridDataItem dataItem in RadGrid1.Items)   
        {             
            if (dataItem.Selected) 
            { 
                string strtxt1 = dataItem["ProductName"].Text; 
                string strtxt2 = dataItem["ProductID"].Text; 
            } 
        } 
    } 

Thanks
Princy.
0
Stefan
Top achievements
Rank 1
answered on 21 May 2009, 12:10 PM
Thank you for the reply Princy,

I did not follow your instructions suggested as I found a way to use the e.Item.DataSetIndex correctly in the ItemCommand event handler. Thank you for the alternative anyway.

My next question relates to selection of grid rows. I saw on your demo site that you can select row (client-side) and make it do a postback to perform an action on the server. It then also preserves that selection on the client-side once the postback is completed. I am happy with that as I need this functionality.

What I did realise is that when I select a row and then sort any column, the state is no longer preserved on the client. I need to preserve the state here as well.

Please advise.
0
Shinu
Top achievements
Rank 2
answered on 22 May 2009, 04:22 AM
Hi Stefan,

Go through the following help articles which explains how to persist the Selected rows on paging/filtering/sorting etc.

Persisting the selected rows server-side on sorting/paging/filtering/grouping
Persisting the selected rows client-side on sorting/paging/filtering/grouping

Regards
Shinu
Tags
Grid
Asked by
Stefan
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Stefan
Top achievements
Rank 1
Shinu
Top achievements
Rank 2
Share this question
or