datagrid Controls(1),

Hi,

How can we query the value of the column number 1 when delete link was click using datagrid.

DirectCast(e.Item.Cells(1).Controls(1),TextBox) <---------why do we need to place a 1 inside control to work?

How can we remove the 1 and still be able to query like so
DirectCast(e.Item.Cells(1).Controls(0),TextBox)

Kindly assist.

Thanks.


Sub manage_update( s As Object, e As DataGridCommandEventArgs)
         Dim key As String = DataGrid1.DataKeys(e.Item.ItemIndex).ToString()
         dim txtdatecoached1 As textbox = DirectCast(e.Item.Cells(1).Controls(1),TextBox)  <---------notice Controls(1)
End sub


EdwardPeterAsked:
Who is Participating?
 
tusharashahConnect With a Mentor Commented:
From your ASPX code you have BoundColumn and seems like CallNumber is teh value that you need... use following and see what you get

Dim Key2 As String = e.Item.Cells(0).Text
label1.text=Key2 &" Deleted."

-tushar
0
 
vadivhereCommented:
Yes, It is necessary and it makes more sense. Basically, all your Items in the DataGrid will be a object collection, and in and every there will be the controls like literal control, text box etc.,

So, to refer this control you have to specify the 1 and it means you're refering Item's cell 1's control 1.

But, if you're facing any sort of problem b'coz of saying 1 here ... post it here, we will try to give you solution.

Cheers!!
0
 
b1xml2Connect With a Mentor Commented:
you can use the DataGridItem itself and the FindControl method to find the textbox, but you';d need to know the ID of the textBox

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

That way, you really do not need to know which cell the control is located.
0
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

 
tusharashahCommented:
Usually when delete link is clicked you do not want to grab value from TextBox... cause you dont want your Users to modify some number and then click delete link.. try get value using following line in your delete command:

Dim key As String = e.Item.Cells(1).Text


-tushar
0
 
EdwardPeterAuthor Commented:
tusharashah,

no errors, it only showed "Deleted."

kindly assist how can we troubleshoot?

Thanks.


Sub manage_Delete(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs)
                        Dim key As String = DataGrid1.DataKeys(e.Item.ItemIndex).ToString()

                        Dim VarTest As String = e.Item.Cells(1).Text <-----------------change done.

                        Dim con As New SqlConnection("....")
                        Dim cmd As SqlCommand = con.CreateCommand()
                        cmd.CommandType = CommandType.StoredProcedure
                        cmd.CommandText = "SP_operator_score_delete"
                        cmd.Parameters.Add(New SqlParameter("@RETURN_VALUE", SqlDbType.Int, 4, ParameterDirection.ReturnValue, False, CType(0, Byte), CType(0, Byte), "", DataRowVersion.Current, Nothing))
                        cmd.Parameters.Add("@operatoranalysisid",SqlDbType.int).Value = key
                        Dim da As New SqlDataAdapter(cmd)
                        Dim ds As New DataSet()
                        da.Fill(ds,"DataSetName")

                        label1.text=VarTest &" Deleted."   <-------to display
                        cmd.dispose
                        cmd.parameters.clear
                        con.close
                           DataGrid1.CurrentPageIndex = 0
                           datagrid1.DataSource=GenerateSource().DefaultView
                           datagrid1.DataBind()
                   End Sub

0
 
tusharashahCommented:
How do you have 1st Column on your DataGrid? is it BoundColumn or Template Column? Paste the code of DataGrid from ASPX in here...

Also change following line: ( you dont need SqldataAdapter and DataSet just to run a query )

                        Dim da As New SqlDataAdapter(cmd)
                        Dim ds As New DataSet()
                        da.Fill(ds,"DataSetName")

Replace with:     cmd.ExecuteNonQuery()


-tushar
0
 
EdwardPeterAuthor Commented:
tusharashah,

is it okay if you could kindly guide me both ways?

one with bound and another with template?

Thanks so much.
0
 
tusharashahCommented:
If you have that particular column as BoundColumn then you would use: e.Item.Cells(1).Text

& if you have that particular column as TemplateColumn then you'll have to find the control that you have inside your template column:
Lets say you have Label inside then you would use it liek this:

 Dim key As String = CType(e.Item.Cells(1).FindControl("Label1"), Label).Text  

Also, remember that Column Number in DataGrid starts from 0 not 1. So, if in case the value that you want is in 1st Column then you'd use: e.Item.Cells(0).text


-tushar
0
 
EdwardPeterAuthor Commented:
tusharashah,

Sad to say no error messages and the label1 only displayed "Deleted." without the callnumber column.

kindly assist.

Thanks.



    Sub manage_Delete(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs)
    Dim key As String = DataGrid1.DataKeys(e.Item.ItemIndex).ToString()
    Dim key2 As String = e.Item.Cells(1).Text




<asp:DataGrid id="DataGrid1" runat="server" Font-Size="xx-small" Font-Names="Verdana" datakeyfield="operatoranalysisid" OnSortCommand="Sort_Grid" AllowSorting="True" GridLines="Horizontal" OnItemDataBound="manage_ItemDataBound" OnEditCommand="manage_edit" OnCancelCommand="manage_cancel_edit" OnUpdateCommand="manage_update" OnDeleteCommand="manage_delete" PageSize="40" AllowPaging="True" onPageIndexChanged="pageGrid" CellPadding="4" AutoGenerateColumns="False">


                                <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>
           
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.