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

[Solved] Access Selected Row's Columns in JavaScript

3 Answers 368 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Jeremy Yoder
Top achievements
Rank 1
Jeremy Yoder asked on 21 Dec 2009, 10:54 PM

I have a grid with client-side row selection turned on. After selecting a row, I want to click a button to access the row and the various individual fields in that row. How can do I do this in JavaScript? Basically, I want something like the following where I can access a column by name or index, but I don't have the syntax figured out, so it crashes...


                var grid = $find("<%= RadGrid1.ClientID %>");  
                var masterTable = grid.get_masterTableView();  
                var row = masterTable.SelectedRow;
                var colid = row.Column["SupplierID"];  // access by name
                alert(colid);  
                var col3 = row.Column[2];  // access by index
                alert(col3);  

3 Answers, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 22 Dec 2009, 04:25 AM
Hi Jeremy,

You can try the following code in order to get the selected value from client side.

javascript:
 
<script type="text/javascript"
    function selectedValue() { 
        var masterTable = $find("<%=RadGrid1.ClientID%>").get_masterTableView(); 
        for (var i = 0; i < masterTable.get_selectedItems().length; i++) { 
            var row = masterTable.get_selectedItems()[i]; 
            var cell = masterTable.getCellByColumnUniqueName(row, "ContactName"); 
            alert(cell.innerHTML); //here cell.innerHTML holds the value of the cell 
        }         
    } 
</script> 

Also go through the following documentation:
Getting cell values for selected rows client side

Regards,
Shinu.
0
Jeremy Yoder
Top achievements
Rank 1
answered on 22 Dec 2009, 02:04 PM

Thanks! You'd have thought an hour of Googling would have found that.

In combing through code yesterday, I kept running into the getDataKeyValue. As far as I can tell, it's used to create a quick reference to key columns in your grid. But is it simply a "shortcut"? (After all, you can access any row and column with the above code.) Or does getDataKeyValue do something more that I'm not following?

If I should post this as a separate thread, I can certainly do that.
0
Princy
Top achievements
Rank 2
answered on 23 Dec 2009, 06:44 AM
Hello Jeremy,

Basically, the getDataKeyValue() and the getCellByColumnUniqueName() client side methods are used to fetch cell values in the grid. DataKeyNames/ClientDataKeyNames are used when you want to hide certain fields in the grid but still be able to access the hidden values(for instance, primary key fields in the database). The getCellByColumnUniqueName() method is used to access cell values when the row element and the uniqueName for the column is known.
 
Also, the datakey names/values are stored in the ViewState so they are available at any moment after the grid has been data-bound, after postbacks, etc. This collection is used when editing data, and for automatic relations when binding a hierarchical grid

Hope this helps..
Princy.
Tags
Grid
Asked by
Jeremy Yoder
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
Jeremy Yoder
Top achievements
Rank 1
Princy
Top achievements
Rank 2
Share this question
or