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
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];
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With