0

I have a simple table as shown below:

enter image description here

When user clicks the Template Field Link button, it should send to another page and fill in the information into the following text boxes:

enter image description here

I want to be able to populate an SQL statement that uses the table's information to populate the information from database:

Below is my gridview:

<Columns>

   asp:TemplateField HeaderText="Action" SortExpression="Date">
        <ItemTemplate>
          <asp:LinkButton ID="LinkButton1" runat="server" CommandName="view" >Display</asp:LinkButton> 
        </ItemTemplate>
           <ControlStyle Width="45px" />
           <ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" Width="100px" />
        </asp:TemplateField>

    <asp:BoundField DataField="Classid" HeaderText="ID" 
                            SortExpression="Date" > 
        <ItemStyle cssClass="grid_padding" />
    </asp:BoundField>

    <asp:BoundField DataField="Addate" HeaderText="Date" SortExpression="Date" 
                            DataFormatString="{0:d}" >
        <ItemStyle cssClass="grid_padding" />
    </asp:BoundField>

    <asp:BoundField DataField="username" HeaderText="User Name" 
                            SortExpression="Date" > 
        <ItemStyle cssClass="grid_padding" />
    </asp:BoundField>

    <asp:BoundField DataField="category" HeaderText="Category" SortExpression="Date"> 
        <ItemStyle cssClass="grid_padding" />
    </asp:BoundField>

    <asp:BoundField DataField="description" HeaderText="Description" 
                            SortExpression="Date" >                
         <ItemStyle CssClass="grid_padding2" />
    </asp:BoundField>

</Columns>

This is where I am at so far for the Link button (UPDATE: all commented out lines give me errors so they don't work):

Protected Sub DisplayClassifieds_SelectedIndexChanged(sender As Object, e As EventArgs) Handles DisplayClassifieds.SelectedIndexChanged
    Dim strSelect As String
    Dim strFilter As String = " "
    Dim counter As Integer = 0
    Dim v As Integer = 0
    'cell = DisplayClassifieds[0,Row].Value

    'cell = DisplayClassifieds.Rows(e.NewSelectedIndex).Cells(0).Text
    'strFilter = DisplayClassifieds.SelectedRowStyle(0).Value

    strSelect = "SELECT Classid, Addate, Username, Category, Description, Fulldescription FROM TABLENAME WHERE Classid = '" & strFilter & "' "


    Page.Session.Add("Display_Values", strSelect)
    Response.Redirect("DispAd.aspx")
End Sub
narue1992
  • 1,143
  • 1
  • 14
  • 40
  • So do you just need help with passing the values from one page to another? – user3841709 Jun 23 '15 at 13:29
  • I need help getting the gridview cell values from the row that the Display button is clicked at. If I can just get a value like `strFilter = cell[0]` of the row then I can use that value inside my SQL statement @user3841709 – narue1992 Jun 23 '15 at 13:31
  • `.SelectedRow` throws me an error that it isn't a member of GridView so I am not sure if my `Protected Sub DisplayClassifieds_RowCommand(sender As Object, e As System.Web.UI.WebControls` is the wrong one or something else. – narue1992 Jun 23 '15 at 13:35

2 Answers2

0

I am a bit rusty with my vb.net GridView as I have been using DataGridViews with WinForms but they are basically the same. Something like this should get you in the right direction. This should go in your template button click event

 dim ID as string =  DataGridView1.Rows(e.RowIndex).Cells("ID").Value

 dim Date as string = DataGridView1.Rows(e.RowIndex).Cells("Date").Value

Continue To do this for all of the columns that you need data from, Then you can pass your ID, Date variables to the next page using session variables

user3841709
  • 386
  • 6
  • 28
  • Thanks for your help. I had tried this in the past but RowIndex throws an error. I figured it out finally after so many attempts though – narue1992 Jun 23 '15 at 13:58
0

Took so many attempts but having SelectedRow.Cells(1).Text... or .Value gave me issues so I split up my command:

Dim row As GridViewRow = DisplayClassifieds.SelectedRow
strFilter = row.Cells(1).Text

With this I got my "ID" value which is enough to use for my SQL

narue1992
  • 1,143
  • 1
  • 14
  • 40