Link to home
Start Free TrialLog in
Avatar of EdwardPeter
EdwardPeter

asked on

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


Avatar of vadivhere
vadivhere

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!!
SOLUTION
Avatar of b1xml2
b1xml2
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
Avatar of EdwardPeter

ASKER

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

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
tusharashah,

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

one with bound and another with template?

Thanks so much.
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
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>
           
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial