2 Replies - 459 Views - Last Post: 25 October 2019 - 06:54 AM Rate Topic: -----

#1 ikhlas06   User is online

  • D.I.C Regular

Reputation: 2
  • View blog
  • Posts: 337
  • Joined: 20-May 16

OnDataBound & OnRowCommand together (Index issue)

Posted 24 October 2019 - 12:10 PM

I am using VB .NET

summary issue:
on 1st window, if you click on ID#1 then you get a error (Index out of bound)
on 1st window, If you click on ID#2 then ID#1 passes to 2nd window.
on 1st window, if you click on ID#3 then ID#2 passes to 2nd window.
on 1st window, if you click on ID#4 then ID#3 passes to 2nd window.
... so on





On 1st window... Below is a basic gridview on with 3 cols (ID, Name, Location). User can click on 'ID' col and that will open up a popup. I am using CommandName 'ID_CMD' to pass my values to 2nd window(pop up window)
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false"
... 
OnDataBound = "OnDataBound" OnRowDataBound="OnRowDataBound">
    <Columns>
          <asp:TemplateField HeaderText="ID" SortExpression="LM_Employee_ID">
            <ItemTemplate>
                 <asp:LinkButton ID="myButton"  runat="server" Text='<%# Eval("ID").ToString() %>' 
                    CausesValidation="false" CommandName="ID_CMD" CommandArgument='<%# Eval("ID").ToString() + ";" +  DataBinder.Eval(Container.DataItem,"Name") + ";" + DataBinder.Eval(Container.DataItem,"Location") %>'></asp:LinkButton>
            </ItemTemplate>
        </asp:TemplateField>
		 <asp:BoundField DataField="ID" HeaderText="ID" SortExpression="ID"  /> 
		  <asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name"  /> 
		   <asp:BoundField DataField="Location" HeaderText="Location" SortExpression="Location"  /> 
		   ...
 </asp:GridView>




on 1st window... I wanted to add a basic header to gridview.
Note* if i remove this function that it works fine.... my guess is that by adding this header... .NET is thinging header is at index 0
Protected Sub OnDataBound(sender As Object, e As EventArgs)
        Dim row As New GridViewRow(0, 0, DataControlRowType.Header, DataControlRowState.Normal)

        Dim cell As New TableHeaderCell()
        cell.Text = "Section#1"
        cell.ColumnSpan = 1
        cell.Style.Add("text-align", "center") 'Text Center
        cell.Style.Add("Border-right", "#919191 2px solid") ' border
        cell.BackColor = ColorTranslator.FromHtml("#d0ebf2")
        cell.ForeColor = Color.Black
        cell.Font.Bold = True
        row.Controls.Add(cell)

        cell = New TableHeaderCell()
        cell.Text = "Section#2"
        cell.ColumnSpan = 2
        cell.Style.Add("text-align", "center") 'Text Center
        cell.Style.Add("Border-right", "#919191 2px solid") ' border
        cell.BackColor = ColorTranslator.FromHtml("#99e3f4")
        cell.ForeColor = Color.Black
        cell.Font.Bold = True
        row.Controls.Add(cell)
        GridView1.HeaderRow.Parent.Controls.AddAt(0, row)
    End Sub
	




This is where I am creating my 2nd window. If user click on a ID.. than open a window with 3 passing values....
Protected Sub GridView1_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles GridView1.RowCommand
     Select Case e.CommandName
         Case "ID_CMD"
		 
             ScriptManager.RegisterStartupScript(Me, Me.GetType(), "Pop", "openModal();", True)
             ' 0 = ID
             ' 1 = Name 
             ' 2 = Location 
             Dim sqlConn As New OdbcConnection(myConnectionString1)
             Dim CmdArg As String() = New String(3) {}
             CmdArg = e.CommandArgument.ToString().Split(";")
             IDL.Text = CmdArg(0)
             NameL.Text = CmdArg(1)
             Location.Text = CmdArg(3)

             Try
                 sqlConn.Open()
                 Dim ds As DataSet = New DataSet()
                 Dim adapter As OdbcDataAdapter

                 Dim getAll As String = "select ...  "
                 getAll = getAll & " and sa.entity_key = " & CmdArg(0)
                 
                 ' bind my sql data into label... on my 2nd window

                 sqlConn.Close()
             Catch ex As Exception
                 myErrorPopL.Text = "<div class='alert alert-danger' role='alert'><Strong>Error#2 </strong>" +
                                         "Can't connect to database." & ex.Message & "</div>"
             End Try

             BindGridData()
     End Select
 End Sub


This post has been edited by ikhlas06: 24 October 2019 - 12:14 PM


Is This A Good Question/Topic? 0
  • +

Replies To: OnDataBound & OnRowCommand together (Index issue)

#2 Martyr2   User is offline

  • Programming Theoretician
  • member icon

Reputation: 5496
  • View blog
  • Posts: 14,470
  • Joined: 18-April 07

Re: OnDataBound & OnRowCommand together (Index issue)

Posted 24 October 2019 - 04:47 PM

Quote

Note* if i remove this function that it works fine.... my guess is that by adding this header... .NET is thinging header is at index 0


I suspect it is too. You even appear to add it to index 0...

GridView1.HeaderRow.Parent.Controls.AddAt(0, row)



Have you tried adding cells directly to the headerrow?

Dim tbCell1 As New TableCell()
tbCell1.Text = "ID"
Gridview1.HeaderRow.Cells.Add(tbCell1)



I ask because this might not be adding it specifically as row 0. But I am not sure since it is has been awhile since I worked with gridviews.
Was This Post Helpful? 1
  • +
  • -

#3 ikhlas06   User is online

  • D.I.C Regular

Reputation: 2
  • View blog
  • Posts: 337
  • Joined: 20-May 16

Re: OnDataBound & OnRowCommand together (Index issue)

Posted 25 October 2019 - 06:54 AM

View PostMartyr2, on 24 October 2019 - 03:47 PM, said:

Quote

Note* if i remove this function that it works fine.... my guess is that by adding this header... .NET is thinging header is at index 0


I suspect it is too. You even appear to add it to index 0...

GridView1.HeaderRow.Parent.Controls.AddAt(0, row)



Have you tried adding cells directly to the headerrow?

Dim tbCell1 As New TableCell()
tbCell1.Text = "ID"
Gridview1.HeaderRow.Cells.Add(tbCell1)



I ask because this might not be adding it specifically as row 0. But I am not sure since it is has been awhile since I worked with gridviews.




Thank you for getting back, I did tried adding a cell directly to headerrow but it just adding an another column and not a HeaderRow

only other options would be to maybe do headerrow on front-end with Gridview code. but i have not found anything on this topic on google
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1