?
Solved

datagrid Controls(1),

Posted on 2005-04-29
9
Medium Priority
?
269 Views
Last Modified: 2010-04-07
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


0
Comment
Question by:EdwardPeter
9 Comments
 
LVL 5

Expert Comment

by:vadivhere
ID: 13894747
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
 
LVL 23

Assisted Solution

by:b1xml2
b1xml2 earned 400 total points
ID: 13894900
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
 
LVL 18

Expert Comment

by:tusharashah
ID: 13894906
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

Author Comment

by:EdwardPeter
ID: 13895648
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
 
LVL 18

Expert Comment

by:tusharashah
ID: 13895758
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
 

Author Comment

by:EdwardPeter
ID: 13895887
tusharashah,

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

one with bound and another with template?

Thanks so much.
0
 
LVL 18

Expert Comment

by:tusharashah
ID: 13896168
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
 

Author Comment

by:EdwardPeter
ID: 13896251
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
 
LVL 18

Accepted Solution

by:
tusharashah earned 1600 total points
ID: 13896305
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

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Suggested Courses
Course of the Month17 days, 1 hour left to enroll

862 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