Results 1 to 9 of 9

Thread: udpating gridview

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2007
    Posts
    617

    udpating gridview

    hi, there's few things i want to do with the grid. first is how can i remove the row whenever the hit button fires up. Note that it will only remove the row in the grid and not deleting the entry. Second, when the Update button fires up I need to update all rows in the grid and set value of Status column to the row dropdownlist value.

    I'm thinking of adding another button inline with the row instead of doing a batch update. Would you think it would be convenient design in terms of net traffic? How would I do that?

    Thanks.

    [IMG] Uploaded with ImageShack.us[/IMG]
    Learn something new every .001 second.

  2. #2
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: udpating gridview

    Hey,

    Can you show the current ASPX markup that you are using for the GridView? Also, how are you binding the information to the Grid? We will need to know these things before we are able to help you properly.

    Gary

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2007
    Posts
    617

    Re: udpating gridview

    hi gary, I decided to change the design instead of doing batch. so the update button will be place inside the grid itself where the row will be updated one at a time.

    update_button | Description | Status



    HTML
    Code:
    <asp:GridView ID="GridView1"   runat="server"   AllowPaging="True" AutoGenerateColumns="False" GridLines="None"  CssClass="gridview_leftpadding">
                                         <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
                                        <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
                                        <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
                                        <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
                                        <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
                                        <EditRowStyle BackColor="#999999" />
                                        <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
                                    <Columns>
    
                                    <asp:TemplateField  ShowHeader="False">
                                        <ItemTemplate><asp:Button ID="deletebutton" runat="server" Text="update" ></asp:Button>
                                        </ItemTemplate>
                                        <ItemStyle Width="5px" CssClass="gridview_leftpadding" />
                                    </asp:TemplateField>
                                    
    
    
                                    <asp:TemplateField HeaderText="Req_ID">
                                        <ItemTemplate><asp:Label ID="idlabel" runat="server"></asp:Label>
                                        </ItemTemplate>
                                          <ItemStyle Width="10px" CssClass="gridview_leftpadding"/>
                                    </asp:TemplateField>
                                    
                                    
                                    <asp:TemplateField HeaderText="Description">
                                        <ItemTemplate><asp:Label ID="descriptionlabel" runat="server"></asp:Label>
                                        </ItemTemplate>
                                          <ItemStyle Width="250px" CssClass="gridview_leftpadding"/>
                                    </asp:TemplateField>
    
    
                                    <asp:TemplateField HeaderText="Status">
                                        <ItemTemplate>
                                            <asp:DropDownList ID="statusDropdownlist" runat="server">
                                                <asp:ListItem>Approved</asp:ListItem>
                                                <asp:ListItem>Pending</asp:ListItem>
                                                <asp:ListItem>Closed</asp:ListItem>
                                            </asp:DropDownList>  
                                        </ItemTemplate>
                                          <ItemStyle Width="70px" CssClass="gridview_leftpadding"/>
                                    </asp:TemplateField>
                                        <asp:CommandField ShowEditButton="True" />
    
    
    
                                    </Columns>
                                </asp:GridView>
    VB
    Code:
        Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound
            If e.Row.RowType = DataControlRowType.DataRow Then
                If e.Row.RowState = DataControlRowState.Normal Or e.Row.RowState = DataControlRowState.Alternate Then
    
                    Dim _idlabel As Label = CType(e.Row.FindControl("idlabel"), Label)
                    Dim _descriptionlabel As Label = CType(e.Row.FindControl("descriptionlabel"), Label)
                    Dim _statusDropdownlist As DropDownList = CType(e.Row.FindControl("statusDropdownlist"), DropDownList)
    
                    _idlabel.Text = e.Row.DataItem("id")
                    _descriptionlabel.Text = e.Row.DataItem("itemdescription")
    
                    _statusDropdownlist.SelectedIndex = _statusDropdownlist.Items.IndexOf(_statusDropdownlist.Items.FindByText(e.Row.DataItem("status").ToString))
    
    
                End If
    
    
            End If
        End Sub
    Last edited by jlbantang; Nov 23rd, 2010 at 09:46 AM.
    Learn something new every .001 second.

  4. #4
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: udpating gridview

    Hey,

    Does this mean that you have it working now, or that you are still having problems? I am not clear.

    Gary

  5. #5

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2007
    Posts
    617

    Re: udpating gridview

    hi,

    The design is working the update button has empty code. I need to perform db update whenever the update button fires up.
    Last edited by jlbantang; Nov 23rd, 2010 at 10:21 AM.
    Learn something new every .001 second.

  6. #6
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: udpating gridview

    Ah, ok, I see what you are saying.

    Set the CommandName property of your button to Update.

    http://msdn.microsoft.com/en-us/libr...mmandname.aspx

    This should fire the GridView's RowCommand:

    http://msdn.microsoft.com/en-us/libr...owcommand.aspx

    Where you can then handle the updating of the DB.

    Gary

  7. #7

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2007
    Posts
    617

    Re: udpating gridview

    hi gary, Its a perfect example just as needed. I can even add a dynamic search by rebuilding selectcommand property. But its bind sqldatasource to populate gridview. How is it possible to update rows through datatable? Is there an option to do this or I have to work on sqldatasource.

    vb binding source
    Code:
    GridView1.DataSource = this_helper.get_DataTable("select pk_itemid as ID,  itemdescription,[status] from qlpodetails")
    GridView1.DataBind()
    It seems I cant find the right answer how this code work from the above hlink sample.

    Code:
     CommandArgument="<&#37;# CType(Container, GridViewRow).RowIndex %>" />
    Learn something new every .001 second.

  8. #8
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: udpating gridview

    If I had a choice, I wouldn't use the SqlDataSource at all

    What exactly is the problem that you are having? I would actually set the CommandArgument using the RowDataBound event of the GridView, rather than do it inline in your ASPX markup.

    Gary

  9. #9

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2007
    Posts
    617

    Re: udpating gridview

    make sense at all. My apology but I get lost when you say

    " I would actually set the CommandArgument using the RowDataBound event of the GridView"

    I just follow the sample code and in general it work fine. I made some changes in populating gridview as the user require to dynamic search data.

    Code:
    <form id="form1" runat="server">
    
          <h3>GridView RowCommand Example</h3>
          
          
         <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
         <asp:Button ID="searchButton2"
             runat="server" Text="Button" />
             
             
         <br />
    
          <asp:GridView id="EmployeeGridView" 
            DataSourceID="EmployeeDataSource"
            
            DataKeyNames="employeeID"
            
            AllowPaging="True" 
            OnRowCommand="EmployeeGridView_RowCommand"
            AutoGenerateColumns="False"
            runat="server">
            <Columns>
              <asp:BoundField DataField="Firstname" HeaderText="Firstname" />
              
                  
              <asp:TemplateField HeaderText="Last Name">
                <ItemTemplate>
                  <asp:Textbox ID="lastTextbox" runat="server" 
                    Text='<&#37;# Bind("lastname") %>'>
                  </asp:Textbox>
                </ItemTemplate>
              </asp:TemplateField>
              
              
              <asp:TemplateField>
                <ItemTemplate>                
                  <asp:Button runat="server" ID="IncreaseButton"
                    Text="udpate"
                    CommandName="update"
                     CommandArgument="<%# CType(Container, GridViewRow).RowIndex %>"
    
                   />
                </ItemTemplate>
              </asp:TemplateField>
            </Columns>
          </asp:GridView>
    
          <!-- This example uses Microsoft SQL Server and connects    -->
          <!-- to the AdventureWorks sample database. Use an ASP.NET  -->
          <!-- expression to retrieve the connection string value     -->
          <!-- from the Web.config file.                              -->
          <asp:SqlDataSource id="EmployeeDataSource"
          
            
              
            UpdateCommand="UPDATE Employees SET lastname=@lastname WHERE [employeeid] = @employeeid"
            ConnectionString="<%$ ConnectionStrings:NWConnectionString %>"
            runat="server" />
    
        </form>

    Code:
        Protected Sub EmployeeGridView_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles EmployeeGridView.RowCommand
            If e.CommandName = "update" Then
    
                ' Convert the row index stored in the CommandArgument
                ' property to an Integer.
                Dim index = Convert.ToInt32(e.CommandArgument)
    
                ' Retrieve the row that contains the button clicked 
                ' by the user from the Rows collection.
                Dim row = EmployeeGridView.Rows(index)
    
                ' Calculate the new price.
                Dim lastTextbox = CType(row.FindControl("lastTextbox"), TextBox)
                lastTextbox.Text = lastTextbox.text ' (Convert.ToDouble(listPriceTextBox.Text) * 1.05).ToString()
    
                ' Update the row.
                EmployeeGridView.UpdateRow(index, False)
    
    
            End If
    
        End Sub
    
        Protected Sub searchButton2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles searchButton2.Click
            EmployeeDataSource.SelectCommand = "select [employeeID], [firstName], lastname FROM Employees where lastname like'" & TextBox1.Text & "%'"
        End Sub
    Learn something new every .001 second.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width