3

I want to get gridview datakeys value in JavaScript.

I know that we can get gridview cell value using

 col1 = grid.rows[i].cells[5];

But I use Autogeneratedcolumn = "true"

so it bind with different number of column each time and there is no specific place where my datakey bind for ex-

for 1 scenario gridview can bind like

Code  A1 A2 A3 Tot
as1    1  2  3  6
as2    2  3  4  9

for 2nd scenario gridview can bind like

Code  A1 A2  Tot
as1    1  2   3
as2    2  3   5

Tot is my datakey I want to get this value in JavaScript function

1
  • Can't you just check the length of grid.rows[i] and then return tot1=grid.rows[i].cells[grid.rows[i].length-1]; Commented Feb 22, 2013 at 18:33

2 Answers 2

1

If the Tot column will always be Tot, and no other column would have that name, you could search for the header datakey text and retrieve the row values based on it:

var grid = document.getElementById('<%=grd.ClientID %>');
var header = grid.rows[0];
var dataKeyIndex = -1;
var dataKeyHeaderText = "Tot";

//Find index of the DataKey column
for (var i = 0; i < header.cells.length; i++) {
    var cell = header.cells[i];
    if (cell.innerText == dataKeyHeaderText) {
        dataKeyIndex = i;
        break;
    }
}

if(dataKeyIndex != -1){

    //Loop the rows retrieving the value
    for (var i = 0; i < header.rows.length; i++) {
        var row = header.rows[i];
        var dataKeyValue = row.cells[dataKeyIndex];
    }
}
Sign up to request clarification or add additional context in comments.

Comments

1

Is your Tot column always the last column?

var cells = grid.rows[i].cells
col1 = cells[cells.length-1];

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.