Solved

Using GridView (VB)

Posted on 2014-12-03
6
96 Views
Last Modified: 2014-12-04
First off, can this be done. If so, where is an example or tutorial...

I'm using a GridView control in an ASP.Net page to display a list of records retrieved via a SQL Server stored procedure. This part is currently working.

The following I need to try and work out:

The GridView displays 9 columns. The first three columns need to be just display (no edit). The next 4 columns are checkboxes, the final two columns are fully editable. Currently these columns are read-only (that is, click on a checkbox and nothing happens). I need the user to be able to change the values in columns 4 through 9 and then save them.

Back to the question(s). Is this possible? How?

TIA
0
Comment
Question by:Clifton Bardwell
  • 3
  • 2
6 Comments
 
LVL 18

Expert Comment

by:Jerry Miller
ID: 40479103
You need to put in the gridview in edit mode to update the fields. CheckBoxes are read-only until then. The fields that you don't want them to change should have this ReadOnly="True" in the tag.

 <asp:BoundField DataField="Vol" HeaderText="Vol" ReadOnly="False" ControlStyle-Font-Size="Small"
   SortExpression="Vol" DataFormatString="{0:C2}" ItemStyle-HorizontalAlign="Right" ControlStyle-Width="90px"/>

This needs to be in the gridview wherever you want the Edit button:
<asp:CommandField ShowEditButton="true" ShowDeleteButton="true" ><ControlStyle CssClass="HyperLinkBlack" /></asp:CommandField>

See these links for examples:
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview(v=vs.110).aspx

Checkboxes:
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.checkboxfield(v=vs.110).aspx

Bounds Fields:
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.boundfield(v=vs.110).aspx
0
 

Author Comment

by:Clifton Bardwell
ID: 40479233
Well, I was hoping for something a little more automated.  The user wants to be able to check boxes and add notes without having to click an "Edit" link for each row they are working on, then click an "Update" link before they can move to the next row.
0
 
LVL 18

Expert Comment

by:Jerry Miller
ID: 40479533
You can use template fields and update from a button click. I couldn't find an example with a textbox, but here are a couple with checkboxes,

<asp:TemplateField SortExpression="Append" HeaderText="Append">
   <ItemTemplate>
   <asp:CheckBox ID="cbAppend" runat="server" Checked='<%# DataBinder.Eval(Container.DataItem,"Append")%>' visible="true" />
   </ItemTemplate>
   </asp:TemplateField>
   <asp:TemplateField SortExpression="Delete" HeaderText="Delete">
   <ItemTemplate>
   <asp:CheckBox ID="cbDelete" runat="server" Checked='<%# DataBinder.Eval(Container.DataItem,"Delete")%>' visible="true" />
   </ItemTemplate>
   </asp:TemplateField>

Open in new window

0
 
LVL 18

Accepted Solution

by:
Jerry Miller earned 500 total points
ID: 40479554
Here are my methods for appending and deleting records.
Protected Sub btnAppend_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim sp As String = String.Empty
        Dim statusCode As String = String.Empty
        Dim cb As String = String.Empty

        sp = "APO.usp_moveData"
        statusCode = APPEND_STATUS
        cb = "cbAppend"
        updateDataEntry(statusCode, cb, sp)
    End Sub

    Protected Sub btnDeleteMarked_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim sp As String = String.Empty
        Dim statusCode As String = String.Empty
        Dim cb As String = String.Empty

        sp = "APO.usp_deleteData"
        statusCode = DELETE_STATUS
        cb = "cbDelete"
        updataDataEntry(statusCode, cb, sp)

    End Sub

    Protected Sub updateDataEntry(ByVal newStatus As String, ByVal cbName As String, ByVal spName As String)
        Dim key As Integer
        Dim MyCheckBox As CheckBox
        Dim gv As GridView = gvData
        Dim projectdbo As New ProjectDataBO

        For Each Item As GridViewRow In gv.Rows
            MyCheckBox = CType(Item.FindControl(cbName), CheckBox)
            If MyCheckBox.Checked Then
                Dim index As Integer = Item.RowIndex

                key = gv.DataKeys.Item(index).Value
                If Len(key) > 0 Then
                      projectdbo.update_Data(key, newStatus, spName)

                End If
            End If
        Next
        Response.Redirect("Data_Form.aspx", False)
    End Sub

Open in new window

0
 

Author Closing Comment

by:Clifton Bardwell
ID: 40481019
Thanks.  :-)
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

792 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