Check this
http://social.msdn.microsoft.com/Forums/en/wpf/thread/74332b78-6bfd-4ac9-af85-dfd9bec87a29
http://wpfadventures.wordpress.com/2008/12/02/wpf-datagrid-detecting-clicked-cell-and-row/
http://stackoverflow.com/q/2148978/217880
script language="javascript" type="text/javascript"> var gridViewCtlId = '<%=ctlGridView.ClientID%>'; var gridViewCtl = null; var curSelRow = null; var curRowIdx = -1; function getGridViewControl() { if (null == gridViewCtl) { gridViewCtl = document.getElementById(gridViewCtlId); } } function onGridViewRowSelected(rowIdx) { var selRow = getSelectedRow(rowIdx); if (null != selRow) { curSelRow = selRow; var cellValue = getCellValue(rowIdx, 0); alert(cellValue); } } function getSelectedRow(rowIdx) { return getGridRow(rowIdx); } function getGridRow(rowIdx) { getGridViewControl(); if (null != gridViewCtl) { return gridViewCtl.rows[rowIdx]; } return null; } function getGridColumn(rowIdx, colIdx) { var gridRow = getGridRow(rowIdx); if (null != gridRow) { return gridRow.cells[colIdx]; } return null; } function getCellValue(rowIdx, colIdx) { var gridCell = getGridColumn(rowIdx, colIdx); if (null != gridCell) { return gridCell.innerText; } return null; } </script>
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head runat="server"> <title></title> <script src="Scripts/jquery-1.3.2.js" type="text/javascript"></script> <script type="text/javascript"> $(document).ready(function() { var list = ""; $("#btnGet").click(function() { $("#<%=GridView1.ClientID %> tr").each(function() { //Skip first(header) row if (!this.rowIndex) return; var age = $(this).find("td:last").html(); list += age + "</br>"; }); $("#listAge").html(list) }); }); </script></head><body> <form id="form1" runat="server"> <div> </div> <asp:GridView ID="GridView1" runat="server"> </asp:GridView> <input type="button" id="btnGet" value="Get Cell Value" /> <div id="listAge"> </div> </form></body></html>
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { //Create Object of person class Person personObject = new Person(); //Assign Person list to GridView GridView1.DataSource = personObject.GetPersonList(); //Call Bindmethod of GridView GridView1.DataBind(); } } public class Person { public int ID { get; set; } public string Name { get; set; } public int Age { get; set; } public List<Person> GetPersonList() { //Retrun List of Person List<Person> list = new List<Person>() { new Person{ID=1,Name="Person1",Age=32}, new Person{ID=2,Name="Person2",Age=45}, new Person{ID=3,Name="Person3",Age=43}, new Person{ID=4,Name="Person4",Age=21}, new Person{ID=5,Name="Person5",Age=76}, new Person{ID=6,Name="Person6",Age=54}, }; return list; } }
If you want to Read the of Control in DataGrid in client side then use jquery.
follow this steps-
step1. create on css clss in aspx page, like this-
<style type ="text/css" >
.clslbl
{
}
</style>
step2. Assign this css class to control in DataGrid, like this-
<asp:TemplateField>
<ItemTemplate>
<asp:Label ID="lbl" runat ="server" CssClass ="clslbl" ></asp:Label>
</ItemTemplate>
</asp:TemplateField>
step3. Now access this control ,like this-
<script type="text/javascript">
function ReadControl() {
var value= $('.clslbl).val();
</script>
call this function. to disable the chechBox.
Try this code and let me know.
Do this way
var parent = document.getElementById("<%=datagrid.ClientID%>");
var tr = parent.getElementsByTagName("TR")[0];
var tc = tr.getElementsByTagName("TD")[0];
var val = parseInt(tc.innerHTML);
public
void
yourDataGridView_Click(
object
sender, DataGridViewEventHandler e)
/*Put the value of the current cell that has the focus into oString */
string
oString = yourDataGridView[yourDataGridView.CurrentCell.RowIndex,yourDataGridView.CurrentCell.ColumnIndex].ToString();
// then set yourTextBox.text to oString
yourTextBox.Text = oString;