Arun
Arun

Reputation: 738

How to get the column index value in gridview

My gridview has autogeneratecolumns = true and I want to get the specific column index value of where the user click on the gridview cell. If the user clicks on the cell of third row, fifth column, then it will show the Header text of Third Row & Fifth Column. How can I get the specific column index value. Please help me out.

protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
   Label1.Text = "RowHeader - " + GridView1.SelectedRow.Cells[0].Text + " and ColumnHeader - " + GridView1.HeaderRow.Cells[0].Text;
}

Upvotes: 2

Views: 5817

Answers (2)

Arul Dinesh
Arul Dinesh

Reputation: 550

General solution is:

gridView.Rows['Index of selected row']['Index or column name of cell you want to get value from'] that in concrete: gridView.Rows[gridView.SelectedRowIndex]["ID"]

As often values in cells are of type Object, so it needed to convert them after getting. In some implementations it may be: gridView.Rows[gridView.SelectedRowIndex].Cells["ID"]

But i think the main idea you have got. Rows - are collection of rows of Table and Cells (or columns) - are collection of cells of Row

Reference from here

Upvotes: 0

Arun
Arun

Reputation: 738

Finally I got it thru the following jQuery...

<script type="text/javascript">
   $(document).ready(function () {
       $('#GridView1>tbody>tr>td').click(function () {
           var col = $(this).parent().children().index($(this));
           var title = $(this).closest("table").find("th").eq(col).text();
           $('p').html("Cell Clicked Text ---> " + $(this).text() + "<br/>" + "Column Name  ---> " + title + "<br/>" + " Column Index ---> " + col + "<br/>");
       });
   });
</script>  

Upvotes: 1

Related Questions