Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Get the contents of a given cell in Datagrid

Status
Not open for further replies.

dpdoug

Programmer
Nov 27, 2002
455
US
How do you get the contents of a given cell when it is clicked on?

Code:
Dim dg As DataGridItem
dg = e.Item

dg.Attributes.Add("onclick", "getCellData('" & [???] & "')")

......

function getCellData(s){
	alert(s)
}

What do I substitute for the [???] in the example above?
 
If you are adding the attributes in the ItemDataBound you can use e.Item.Cells(0).Text to get the text of a particular cell.

--------------------------------------------------------------------------------------------------------------------------------------------

Need help finding an answer?

Try the search facility ( or read FAQ222-2244 on how to get better results.
 
Thanks. Ok. The e.Item.Cells(0).Text gives me the contents of the first cell in the selected row.

What I need to get is the selected column. How do I get the contents of the cell that the user clicked?

If I could get it to return the index of the column that the user clicked, that would also work. But if I hard code an index number in the Cells(?) it will only return me the value that is in that cell and not the one the user clicked on.
 
If you perform this operation in the OnItemCommand of the DataGrid it will return the value from the row that was clicked. If you want a primary key or index returned you can bind it to the grid as a BoundColumn Visible="False" and retrieve the value that way (using the above method) or, you can add the primary key column to your grid as a DataKeyField (in Properties) and retrieve it that way by going after e.Item.ItemIndex (
 
BTW, here the rest of the code on the page.

Code:
<%@ Page Language="VB" ContentType="text/html" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>

<HTML>
	<HEAD>
		<title>Add New Truck</title>
		<script language="VB" runat="server">
Dim DSConnection As SqlConnection

Sub Page_Load(Sender As Object, E As EventArgs)
   
If Not IsPostBack Then
BindDropDowns()
Else 

   End IF
End Sub

Sub BindDropDowns()
'LOCATION
'set dims
        Dim DS_Location As DataSet
       ' Dim DSConn_Location As SqlConnection
        Dim Cmd_Location As SqlDataAdapter
'set up data connection via stored proceedure
        DSConnection = New SqlConnection(System.Configuration.ConfigurationSettings.AppSettings("PSF_DataStore"))
        Cmd_Location = New SqlDataAdapter("Psf_sp_ActiveDateList_TEST", DSConnection)
		Cmd_Location.SelectCommand.CommandType = CommandType.StoredProcedure
'create new dataset
        DS_DateList = New DataSet()
        Cmd_DateList.Fill(DS_DateList, "Date")
'bind data to control
        DataList1.DataSource = DS_DateList.Tables("Date").DefaultView
        DataList1.DataBind()
End Sub

    Sub GetListItems(sender As Object, e As EventArgs)
        If DataList1.Items.Count > 0 Then
	'testing to see if i get any data.
            Label1.Text = "The Items collection contains: <br>"
            
            Dim dlItem As DataListItem
			Dim Date_KeyID As String 
			Dim txtBoxDate As String 
            For Each dlItem In  DataList1.Items
 			Date_KeyID = DataList1.DataKeys(dlItem.ItemIndex).ToString
 			txtBoxDate = CType((dlItem.FindControl("fld_projectedsale")).Text, TextBox)
            Next dlItem
        End If
    End Sub 'Button_Click
	
</script>

<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Untitled Document</title>
</head>
<body>
<form action="" runat="server" method="post">
<asp:DataList CellPadding="1" DataKeyField="Date_Key" DataMember="Date_Key"
                Font-Name="Verdana"
                Font-Size="10pt"
                HorizontalAlign="Center" id="DataList1"
                RepeatColumns="14" runat="server" 
                Width="85%" Border="1" >

<ItemTemplate> 
  <div align="center"><a href="#">go<%# DataBinder.Eval(Container.DataItem, "Date_Key") %></a>
      <br>
  </div>
  <asp:TextBox ID="fld_projectedsale"  Columns="5" runat="server" /> </ItemTemplate>
<footertemplate> <asp:Button ID="btn_submit"   OnClick="GetListItems" Text="Insert" runat="server" /></footertemplate>
</asp:datalist>
</form>
<asp:Label id="Label1"
           runat="server"/>



</body>
</html>

Thanks
Talenx
 
sorry, please direguard my last post, i posted this on the wrong post...

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top