?
Solved

get the value of the first cell in the datagrid

Posted on 2005-04-28
10
Medium Priority
?
407 Views
Last Modified: 2013-12-03
Hi,

How can we get the value of the first cell in the datagrid? the datagrid is bound to a table and using edit bound in asp.net vb.

Thanks.


Sub manage_update( s As Object, e As DataGridCommandEventArgs)
        Dim key As String= DataGrid1.DataKeys.Item(DataGrid1.EditItemIndex)
        Dim txtdatecoached1 As TextBox = DirectCast(e.Item.Cells(1).Controls(0),TextBox)

label1.text=txtdatecoached1.text
End Sub


Server Error in '/' Application.
--------------------------------------------------------------------------------

Specified cast is not valid.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.InvalidCastException: Specified cast is not valid.

Source Error:


Line 368:        Sub manage_update( s As Object, e As DataGridCommandEventArgs)
Line 369:        Dim key As String= DataGrid1.DataKeys.Item(DataGrid1.EditItemIndex)
Line 370:        Dim txtdatecoached1 As TextBox = DirectCast(e.Item.Cells(1).Controls(0),TextBox)
Line 371:        
Line 372:        
Source File: c:\inetpub\wwwroot\cardviewdetails.aspx    Line: 370
0
Comment
Question by:EdwardPeter
  • 5
  • 2
  • 2
  • +1
10 Comments
 
LVL 23

Assisted Solution

by:b1xml2
b1xml2 earned 1000 total points
ID: 13887251
Sub manage_update(ByVal sender As Object, ByVal e As DataGridCommandEventArgs)
      Select Case e.Item.ItemType
            Case ListItemType.Edit
            Dim key As String = CStr(DataGrid1.DataKeys.Item(e.Item.ItemIndex))
            Dim dateCoached As TextBox = CType(e.Item.Cells(0).Controls(0),TextBox)
            If Not dateCoached Is Nothing Then
            
            End If
      End Select
End Sub

the TableCellCollection class starts from 0, not 1 in accessing its cells
0
 
LVL 18

Accepted Solution

by:
tusharashah earned 1000 total points
ID: 13887274
The Column Number for FirstCell for any row is 0.. try with following:

Sub manage_update( s As Object, e As DataGridCommandEventArgs)
        Dim key As String= DataGrid1.DataKeys.Item(DataGrid1.EditItemIndex)
        Dim txtdatecoached1 As TextBox = DirectCast(e.Item.Cells(0).Controls(0),TextBox)

label1.text=txtdatecoached1.text
End Sub


2) Also... I do not see any thing with DataGrid1.DataKeys.Item use this:

Sub manage_update( s As Object, e As DataGridCommandEventArgs)
        Dim key As String= DataGrid1.DataKeys(DataGrid1.EditItemIndex).ToString()
        Dim txtdatecoached1 As TextBox = DirectCast(e.Item.Cells(1).Controls(0),TextBox)

label1.text=txtdatecoached1.text
End Sub

-tushar
0
 

Author Comment

by:EdwardPeter
ID: 13887307
tusharashah,

I'm not sure what's happening, do you need to see the html datagrid tags ? so that we can furthur troubleshoot.

Thanks.


Server Error in '/' Application.
--------------------------------------------------------------------------------

Specified cast is not valid.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.InvalidCastException: Specified cast is not valid.

Source Error:
Line 368:        Sub manage_update( s As Object, e As DataGridCommandEventArgs)
Line 369:        Dim key As String= DataGrid1.DataKeys(DataGrid1.EditItemIndex).ToString()
Line 370:        Dim txtdatecoached1 As TextBox = DirectCast(e.Item.Cells(1).Controls(0),TextBox)
Line 371:                
Line 372:        
Source File: c:\inetpub\wwwroot\qualityassurance\cardviewdetails.aspx    Line: 370
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 18

Expert Comment

by:tusharashah
ID: 13887309
I'll take 2nd suggetion back.. DataGrid1.DataKeys.Item is correct for VB.Net :)
0
 

Author Comment

by:EdwardPeter
ID: 13887360
tusharashah,

The datagrid has 12 columns.

Thanks.

Server Error in '/' Application.
--------------------------------------------------------------------------------

Specified cast is not valid.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.InvalidCastException: Specified cast is not valid.
Source Error:
Line 369:        Sub manage_update( s As Object, e As DataGridCommandEventArgs)
Line 370:        Dim key As String= DataGrid1.DataKeys.Item(DataGrid1.EditItemIndex)
Line 371:        Dim txtdatecoached1 As TextBox = DirectCast(e.Item.Cells(1).Controls(0),TextBox)
Line 372:        'Dim txtdatemonitored As TextBox = DirectCast(e.Item.Cells(3).Controls(0),TextBox)
Line 373:        'Dim txtcitystate As TextBox = DirectCast(e.Item.Cells(4).Controls(0),TextBox)
 

Source File: c:\inetpub\wwwroot\qualityassurance\cardviewdetails.aspx    Line: 371


-------------------------------------------------------------------------------------HTML tags for datagrid1


                            <asp:DataGrid id="DataGrid1" runat="server" Font-Names="Verdana" Font-Size="XX-Small" AutoGenerateColumns="False" CellPadding="4" onPageIndexChanged="pageGrid" AllowPaging="True" PageSize="40" OnDeleteCommand="manage_delete" OnUpdateCommand="manage_update" OnCancelCommand="manage_cancel_edit" OnEditCommand="manage_edit" OnItemDataBound="manage_ItemDataBound" GridLines="Horizontal" AllowSorting="True" OnSortCommand="Sort_Grid" datakeyfield="operatoranalysisid">
                                <EditItemStyle wrap="False"></EditItemStyle>
                                <ItemStyle wrap="False"></ItemStyle>
                                <HeaderStyle forecolor="Blue"></HeaderStyle>
                                <Columns>
                                    <asp:BoundColumn DataField="CallNumber" SortExpression="CallNumber" ReadOnly="True" HeaderText="Call#">
                                        <HeaderStyle forecolor="Blue"></HeaderStyle>
                                        <ItemStyle horizontalalign="Center"></ItemStyle>
                                    </asp:BoundColumn>
                                    <asp:TemplateColumn HeaderText="DateCoached">
                                        <ItemTemplate>
                                            <asp:Label runat="server" text='<%# DataBinder.Eval(Container, "DataItem.DateCoached") %>'></asp:Label>
                                        </ItemTemplate>
                                        <EditItemTemplate>
                                            <asp:TextBox runat="server" name="txtDateCoached" maxlength="10" width="70px" Text='<%# DataBinder.Eval(Container, "DataItem.DateCoached") %>'></asp:TextBox>
                                        </EditItemTemplate>
                                    </asp:TemplateColumn>

0
 
LVL 23

Expert Comment

by:b1xml2
ID: 13887637
if you have an explicit TextBox,

Dim txtDateCoached As TextBox = DirectCast(e.Item.FindControl("txtDateCoached"),TextBox)

You seem intent on not using the e.Item.ItemType to deduce what is the current Grid Type (which is not a good idea unless you have compelling reasons to do so)


Sub manage_update( s As Object, e As DataGridCommandEventArgs)
Dim key As String= DataGrid1.DataKeys.Item(DataGrid1.EditItemIndex)
'what happens if this is not an Edit Item, you'll never get the TextBox
Dim txtdatecoached1 As TextBox = DirectCast(e.Item.FindControl("txtDateCoached"),TextBox)
....
End Sub

0
 
LVL 35

Expert Comment

by:YZlat
ID: 13888143
Sub manage_update( s As Object, e As DataGridCommandEventArgs)
      Dim key As String= DataGrid1.DataKeys(DataGrid1.EditItemIndex).ToString()
           Dim txtdatecoached1 As TextBox = CType(e.Item.Cells(1).Controls(0),TextBox)
0
 

Author Comment

by:EdwardPeter
ID: 13888152
b1xml2,

I'm open to any new idea for us to get the value of the fisrt cell, second, third, etc

Thanks.


Server Error in '/' Application.
--------------------------------------------------------------------------------

Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.
Source Error:
Line 395:
Line 396:
Line 397:              label3.text=txtdatecoached1.text
Line 398:
Line 399:        End Sub
 Source File: c:\inetpub\wwwroot\cardviewdetails.aspx    Line: 397
0
 

Author Comment

by:EdwardPeter
ID: 13888343
b1xml2,

I mean, when user click the edit link bound to the datagrid : )

Thanks.
0
 

Author Comment

by:EdwardPeter
ID: 13888502
It's okay now !!!

I'm not sure what Controls(1) does, but it solved the problem.

is it okay if anyone could kindly explain what happend?

dim txtdatecoached1 As textbox = DirectCast(e.Item.Cells(1).Controls(1),TextBox)
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this Article, I will provide a few tips in problem and solution manner. Opening an ASPX page in Visual studio 2003 is very slow. To make it fast, please do follow below steps:   Open the Solution/Project. Right click the ASPX file to b…
ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
Suggested Courses
Course of the Month13 days, 8 hours left to enroll

750 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question