Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: ASP.NET VB.NET
hi all,
 

 
can u help me on this..i already manage to insert textbox values to gridview.but when i want to edit the value of gridview row (pass back to textbox) it didnt work.it always get the 0 row index.please help me..it is urgent.i already try from yesterday but failed.
 

 
my code behind:
 
Private Sub FirstGridViewRow()
        Dim dt As New DataTable()
        Dim dr As DataRow = Nothing
        dt.Columns.Add(New DataColumn("Bil", GetType(String)))
        dt.Columns.Add(New DataColumn("Wheel Record No", GetType(String)))
        dt.Columns.Add(New DataColumn("SMH", GetType(String)))
        dt.Columns.Add(New DataColumn("Tindakan", GetType(String)))
        dt.Columns.Add(New DataColumn("Jenis Tayar", GetType(String)))
        'dt.Columns.Add(New DataColumn("Kuantiti", GetType(String)))

        dr = dt.NewRow()
        'dr("RowNumber") = i + 1
        dr("Wheel Record No") = String.Empty
        dr("SMH") = String.Empty
        dr("Tindakan") = String.Empty
        dr("Jenis Tayar") = String.Empty
        'dr("Kuantiti") = String.Empty

        dt.Rows.Add(dr)
 
        grvDO.DataSource = dt
        grvDO.DataBind()
 

    End Sub
 

    'A method that Inserts the records to the database
    Private Sub InsertRecords(ByVal sc As StringCollection)
        Dim conn As New SqlConnection(connectionString)
        Dim sb As New StringBuilder(String.Empty)
 
        Dim splitItems As String() = Nothing
        For Each item As String In sc
 
            Const sqlStatement As String = "Insert into delivery_order (tarikh,contractno,tindakan,jenisroda,wheelno,smhref,dono,authorizedby,kuantiti,status) values "
 
            If item.Contains(",") Then
                splitItems = item.Split(",".ToCharArray())
                sb.AppendFormat("{0}('{1}','{2}','{3}','{4}','{5}','{6}','{7}','{8}','{9}','{10}'); ", sqlStatement, splitItems(0), splitItems(1), splitItems(2), splitItems(3), splitItems(4), splitItems(5), splitItems(6), splitItems(7), splitItems(8), splitItems(9))
 
            End If
        Next
 
        Try
            conn.Open()
            Dim cmd As New SqlCommand(sb.ToString(), conn)
 
            cmd.CommandType = CommandType.Text
 
            cmd.ExecuteNonQuery()
 
            'Display a popup which indicates that the record was successfully inserted

            Page.ClientScript.RegisterClientScriptBlock(GetType(Page), "Script", "alert('Records Successfully Saved!');", True)
        Catch ex As System.Data.SqlClient.SqlException
            Dim msg As String = "Insert Error:"
            msg += ex.Message
 
            Throw New Exception(msg)
        Finally
            conn.Close()
        End Try
    End Sub
 
   
 
    Protected Sub btnSave_Click(ByVal sender As Object, ByVal e As EventArgs)
        Dim rowIndex As Integer = 0
        Dim q As Integer = 0
        Dim sc As New StringCollection()
        If ViewState("CurrentTable") IsNot Nothing Then
            Dim dtCurrentTable As DataTable = DirectCast(ViewState("CurrentTable"), DataTable)
            If dtCurrentTable.Rows.Count > 0 Then
                For i As Integer = 1 To dtCurrentTable.Rows.Count
                    'extract the TextBox values
                    Dim wheelno As Label = DirectCast(grvDO.Rows(rowIndex).Cells(1).FindControl("wheel"), Label)
                    Dim smhref As Label = DirectCast(grvDO.Rows(rowIndex).Cells(2).FindControl("smh"), Label)
                    Dim ddl_tindakan As Label = DirectCast(grvDO.Rows(rowIndex).Cells(3).FindControl("tindakan"), Label)
                    Dim ddl_jenisroda As Label = DirectCast(grvDO.Rows(rowIndex).Cells(4).FindControl("axletype"), Label)
                    'Dim kuantiti As Label = DirectCast(grvDO.Rows(rowIndex).Cells(5).FindControl("kuantiti"), Label)
                    authorizedby.Text = "SMH Rep."
                    status.Text = "Pending"
                    q = dtCurrentTable.Rows.Count
                    kuantiti.Text = q
 
                    'get the values from the TextBoxes
                    'then add it to the collections with a comma "," as the delimited values
                    sc.Add(tarikh.Text + "," + contractno.Text + "," + ddl_tindakan.Text + "," + ddl_jenisroda.Text +
                           "," + wheelno.Text + "," + smhref.Text + "," + dono.Text + "," + authorizedby.Text + "," + kuantiti.Text + "," + status.Text)
                    rowIndex += 1
                Next
                'Call the method for executing inserts
                'Insert()
                InsertRecords(sc)
                FirstGridViewRow()
                contractno.Text = String.Empty
                smhref.Text = String.Empty
                wheelno.Text = String.Empty
                ddl_jenisroda.ClearSelection()
                ddl_tindakan.ClearSelection()
            End If
        End If
 
       
    End Sub
 
    Protected Sub tambah_Click(ByVal sender As Object, ByVal e As EventArgs)
        If ViewState("CurrentTable") IsNot Nothing Then
            Dim dt As DataTable = DirectCast(ViewState("CurrentTable"), DataTable)
            Dim count As Integer = dt.Rows.Count
            BindGrid(count)
        Else
            BindGrid(1)
        End If
        btnSave.Visible = True
        smhref.Text = String.Empty
    End Sub
 
    Protected Sub BindGrid(ByVal rowcount As Integer)
        Dim i As Integer = 0
        Dim dt As New DataTable()
        Dim dr As DataRow
        dt.Columns.Add(New DataColumn("Bil", GetType(String)))
        dt.Columns.Add(New System.Data.DataColumn("Wheel Record No", GetType(String)))
        dt.Columns.Add(New System.Data.DataColumn("SMH", GetType(String)))
        dt.Columns.Add(New System.Data.DataColumn("Tindakan", GetType(String)))
        dt.Columns.Add(New System.Data.DataColumn("Jenis Tayar", GetType(String)))
        'dt.Columns.Add(New System.Data.DataColumn("Kuantiti", GetType(String)))

        If ViewState("CurrentTable") IsNot Nothing Then
            For j As Integer = 0 To rowcount + 1
                dt = DirectCast(ViewState("CurrentTable"), DataTable)
                If dt.Rows.Count > 0 Then
                    dr = dt.NewRow()
                    dr(0) = dt.Rows(0)(0).ToString()
 
                End If
            Next
            dr = dt.NewRow()
 
            dr(0) = i + 1
            dr(1) = wheelno.Text
            dr(2) = smhref.Text
            If ddl_tindakan.SelectedValue = "Pilih" Then
                dr(3) = ""
            Else
                dr(3) = ddl_tindakan.SelectedValue
            End If
 

 
            If ddl_jenisroda.SelectedValue = "Pilih" Then
 
                dr(4) = ""
            Else
                dr(4) = ddl_jenisroda.SelectedValue
            End If
            'dr(5) = kuantiti.Text

 

            dt.Rows.Add(dr)
        Else
            dr = dt.NewRow()
            dr(0) = i + 1
            dr(1) = wheelno.Text
            dr(2) = smhref.Text
            dr(3) = ddl_tindakan.SelectedValue
            dr(4) = ddl_jenisroda.SelectedValue
            'dr(5) = kuantiti.Text
            dt.Rows.Add(dr)
 
        End If
 
        ' If ViewState has a data then use the value as the DataSource
        If ViewState("CurrentTable") IsNot Nothing Then
            grvDO.DataSource = DirectCast(ViewState("CurrentTable"), DataTable)
            grvDO.DataBind()
        Else
            ' Bind GridView with the initial data assocaited in the DataTable
            grvDO.DataSource = dt
 
            grvDO.DataBind()
        End If
        ' Store the DataTable in ViewState to retain the values
        ViewState("CurrentTable") = dt
        grvDO.Visible = True
    End Sub
 
    Protected Sub ImageButton1_Click(ByVal sender As Object, ByVal e As EventArgs) 'edit
        Dim row As GridViewRow = CType(CType(sender, ImageButton).Parent.Parent, GridViewRow)
        Dim mySelectedRow As GridViewRow = row
        Label1.Text = row.Cells(0).Text
     
 
        Dim wheel As String = CType(mySelectedRow.FindControl("wheel"), Label).Text.ToString() 'DirectCast(grvDO.Rows(row).Cells(1).FindControl("wheel"), Label).Text.ToString() 
        wheelno.Text = wheel
        Dim smh As String = CType(mySelectedRow.FindControl("smh"), Label).Text.ToString()
        smhref.Text = smh
 
        Dim tindakan As String = CType(mySelectedRow.FindControl("tindakan"), Label).Text.ToString()
        ddl_tindakan.SelectedValue = tindakan
        Dim axletype As String = CType(mySelectedRow.FindControl("axletype"), Label).Text.ToString()
        ddl_jenisroda.SelectedValue = axletype
      
        Button1.Visible = True
        tambah.Visible = False
 

    End Sub
 
    Protected Sub grvDO_RowUpdating(ByVal sender As Object, ByVal e As GridViewUpdateEventArgs)
        'Dim sn As Integer = Convert.ToInt16(DirectCast(grvDO.Rows(e.RowIndex).Cells(0).Controls(0), TextBox).Text)
        'Label2.Text = sn
        Dim row As GridViewRow = grvDO.Rows(e.RowIndex)
        'Find the row that was clicked for updating.
        grvDO.EditIndex = -1
        'Change the edit index to -1 .
        If row IsNot Nothing Then
            Dim t As TextBox = DirectCast(row.Cells(1).Controls(0), TextBox)
            'Find the Control in the grid.

            'CurrentTable = DirectCast(Session("rows"), DataTable)
            Dim dt As DataTable = DirectCast(ViewState("CurrentData"), DataTable)
            'Get the values of the datatable from the session variable
            'Traverse through the Datatable till you hit the same row as the row needed to be updated. 

            For i As Integer = 0 To dt.Rows.Count - 1
                If e.RowIndex = i Then
 
                    dt.Rows(i)(0) = Convert.ToInt32(t.Text)
                    ' Change the row's 0th column with the value entered in the textbox.
                    ViewState("CurrentData") = dt 'Session("rows") = dt
                    'Update the session variable with the changes.
                    'The same old stuff, set the Datasource and bind the Grid.! everyone is happy!
                    grvDO.DataSource = dt
                    grvDO.DataBind()
                End If
            Next
        End If
 
    End Sub
 

    Protected Sub UpdateRecord(ByVal sender As Object, ByVal e As EventArgs)
 
        Dim dr As GridViewRow = CType(CType(sender, Button).Parent.Parent, GridViewRow)
        Dim row As GridViewRow = dr
        Dim rowIndex As Integer = 0 'Integer.Parse(Label1.Text)
        Dim dtCurrentTable As DataTable = DirectCast(ViewState("CurrentTable"), DataTable)
        Dim columnIndex As Integer = 0
        Label2.Text = row.Cells(0).Text
 
        For i As Integer = 0 To dtCurrentTable.Rows.Count
 
            Dim wheel As Label = DirectCast(grvDO.Rows(rowIndex).Cells(1).FindControl("wheel"), Label)
            wheel.Text = wheelno.Text 'CType(row.FindControl("wheel"), Label)
            Dim smh As Label = DirectCast(grvDO.Rows(rowIndex).Cells(2).FindControl("smh"), Label)
            smh.Text = smhref.Text
            Dim tindakan As Label = DirectCast(grvDO.Rows(rowIndex).Cells(3).FindControl("tindakan"), Label)
            tindakan.Text = ddl_tindakan.SelectedValue 'CType(row.FindControl("tindakan"), Label)
            Dim axletype As Label = DirectCast(grvDO.Rows(rowIndex).Cells(4).FindControl("axletype"), Label)
            axletype.Text = ddl_jenisroda.SelectedValue
 
        Next
 

        
 

        Button1.Visible = False
        tambah.Visible = True
    End Sub
 
aspx code:
 
<blockquote class="FQ"><div class="FQA">Quote:</div><asp:Panel ID="Panel1" runat="server" Width="100%">
    <table rules="rows">
        
        <tr>
            <td style="width:70px">
                Date
            </td>
            <td style="width:150px">
                <asp:TextBox ID="tarikh" runat="server" Width="100"><asp:CalendarExtender ID="CalendarExtender1"
                    runat="server" Format="dd/MM/yyyy" TargetControlID="tarikh">
                
            </td>
            <td style="width:100px">
            Contract No.
            </td>
            <td style="width:150px">
            KTMB <asp:TextBox ID="contractno" runat="server" Width="70px">
 
            </td>
 
          </tr>  
          <tr>
          <td style="width:70px">
                Tindakan
            </td>
            <td style="width:150px">
            <asp:DropDownList ID="ddl_tindakan" runat="server">
            <asp:ListItem>Pilih
            <asp:ListItem>Redisc
        <asp:ListItem>Reprofile
        <asp:ListItem>Bearing Service
        <asp:ListItem>Other
                
                
            </td>
            <td style="width:100px">
            Jenis Tayar
            </td>
            <td style="width:150px">
            <asp:DropDownList ID="ddl_jenisroda" runat="server">
                <asp:ListItem>Pilih
                <asp:ListItem>16T
                <asp:ListItem>14T
                <asp:ListItem>Koyo Large
                <asp:ListItem>KOROS
                <asp:ListItem>PGC
                <asp:ListItem>GH
                <asp:ListItem>81 class_motor
                <asp:ListItem>81 class_trailer
                <asp:ListItem>82 class_motor
                <asp:ListItem>82 class_trailer
                <asp:ListItem>83 class_motor
                <asp:ListItem>83 class_trailer
                <asp:ListItem>23 class
                <asp:ListItem>24 class
                <asp:ListItem>25 class
                <asp:ListItem>26 class
                <asp:ListItem>29 class
                
 
            </td>
            
          </tr>
          <tr>
          <td style="width:100px">
          Wheel Record No
          </td>
          <td>
              <asp:TextBox ID="wheelno" runat="server">
          </td>
          <td style="width:100px">
          SMH Ref. No.
          </td>
          <td>
              <asp:TextBox ID="smhref" runat="server">
          </td>
          
          </tr>
          
    </table>
    
 
    
 
        <asp:Button ID="tambah" runat="server" Text="Tambah" OnClick="tambah_Click" />
        <asp:Button ID="Button1" runat="server" Text="Kemaskini" Visible="False" OnClick="UpdateRecord"/>
        <asp:Label ID="authorizedby" runat="server" Text="" Visible="False">
    <asp:Label ID="dono" runat="server" Text="" Visible="False">
    <asp:Label ID="Label1" runat="server" Text="" Visible="False">
    <asp:Label ID="status" runat="server" Text="" Visible="False">
    <asp:Label ID="kuantiti" runat="server" Text="" Visible="False">
    <asp:Label ID="Label2" runat="server" Text="" Visible="False">
    
 
    
 
    <asp:GridView ID="grvDO" runat="server" ItemStyle-Width="30px" AutoGenerateColumns="False" 
    Width="70%" BorderColor="#486070" HeaderStyle-BackColor="#D8E8F0"  
    AlternatingRowStyle-BackColor="White" BackColor="White" FooterStyle-BackColor="#D8E8F0" 
    FooterStyle-BorderColor="#486070" ShowFooter="True"  >
                <columns>
                    <asp:TemplateField HeaderText="Bil" ItemStyle-Width="20px">
    <itemtemplate>
        <asp:Label ID="bil" runat="server" Text=<%# Container.DataItemIndex+1 %>>
Posted 18-Dec-12 22:22pm
musiw640

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

  Permalink  
Comments
musiw at 21-Dec-12 23:33pm
   
no it's not what im looking for.i bind data to gridview using view state data table.then insert to database.so how i want to update datatable and bind it back before i insert into database.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 318
1 Sergey Alexandrovich Kryukov 286
2 RyanDev 75
3 PhilLenoir 60
4 nv3 60
0 Sergey Alexandrovich Kryukov 6,691
1 OriginalGriff 6,119
2 CPallini 2,473
3 Richard MacCutchan 1,697
4 Abhinav S 1,560


Advertise | Privacy | Mobile
Web04 | 2.8.140821.2 | Last Updated 19 Dec 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100