How to get Column Value for a given Row within GridView using javascript in asp.net?

37,021

Solution 1

Check this out...

 var tbl = document.getElementById('Gridview1');

 var tbl_row = tbl.rows[parseInt(RowIndex) + 1];

 var tbl_Cell = tbl_row.cells[no of the cell];

 var value= tbl_Cell.innerHTML.toString();

Here no of the cell indicates the column number.

If it is a template field,you can try this..

var value=document.getElementById('GridViewId_ColumnID_' + RowIndex).value;

Solution 2

A simple example.

 function myfunc   {
         var b=document.getElementById("GridView1");
         var c=document.getElementById("TextBox1");
         var d=document.getElementById("TextBox2");
         dd=dd+1;
         c.value=document.getElementById("GridView1").rows[dd].cells[2].innerHTML;
         d.value=document.getElementById("GridView1").rows[dd].cells[3].innerHTML;
    }

Get the id of the grid. Then you play around it by using rows[] and cells[]

Share:
37,021
user3264676
Author by

user3264676

Updated on July 28, 2020

Comments

  • user3264676
    user3264676 almost 4 years

    I have a GridView, I have a row index in a javascript function. Now I want to get the value of a Column for that index in a GridView using javascript. Please tell me how can I do this?

    • Adil
      Adil about 10 years
      Please show us the generated html in relation to you question.
  • user3264676
    user3264676 about 10 years
    Its working fine. But I want to get the value at this variable value in your code. How to do this.
  • Jameem
    Jameem about 10 years
    The variable 'value' has the value of the paricular cell..Is it a template feild?
  • user3264676
    user3264676 about 10 years
    yes template field. alert(value); is coming. <span id="GridViewId_ColumnID_0">Pending</span> I want to get the value at this id , in my case its Pending.
  • user3264676
    user3264676 about 10 years
    alert(value); is coming undefined :(
  • Jameem
    Jameem about 10 years
    I have edited the answer..var value=document.getElementById('GridViewId_ColumnID_' + RowIndex).value;
  • user3264676
    user3264676 about 10 years
    yes, I have tried that too. var value = document.getElementById('GridViewMultiplePOSAssociationId_La‌​belStatusPendingPOSI‌​d_0').value; alert(value);
  • Jameem
    Jameem about 10 years
    Is it a label..?var value = document.getElementById('GridViewMultiplePOSAssociationId_La‌​belStatusPendingPOSI‌​‌​d_0').innerHTML; alert(value);