Solved

Bulk Edit in GridView using DOTNET

Posted on 2014-10-14
6
423 Views
Last Modified: 2014-10-16
I need to help user in building them a editable gridview which acts like a excel where they can edit multiple rows and update on click of a submit button in the end. Tried a few online but no success so far.
0
Comment
Question by:welcome 123
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 40381918
0
 
LVL 29

Expert Comment

by:sammySeltzer
ID: 40382401
I have an editable gridview that allows you to edit one or multiple rows at same time but it does not look like an excel sheet.

See screenshot.

Will this work for you?
multichecks.jpg
0
 

Author Comment

by:welcome 123
ID: 40382597
I am not sure how far I can convince in buying something but can give it a try and sammySeltzer I really like the screenshot and I think that's enough for achieving the multi edit functionality though not like excel but I really like it can you share the source code too?
0
Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

 
LVL 29

Accepted Solution

by:
sammySeltzer earned 500 total points
ID: 40382650
Here you go. Hope it helps you.

'///Markup

    <p style="height: 0px; margin-bottom: 50px; font-style: normal; font-size: large; top: auto;">
        Click to check one or more checkboxes, enter worker's Test scores and click the <b>Store the score</b> button to save scores to the database.
        When you are done storing the scores for a particular worker, click the <b>Return</b> link below to continue.
    </p> 
    <br /><br />
           <asp:label id="Message" forecolor="firebrick" runat="server"/>&nbsp;|&nbsp; <a href="yourpage.aspx">Return</a>
           <br /><br />
    <div style="display: block;">
       <fieldset style="margin-bottom: 20px;width:600px;">
         <legend style="font-weight: bold;">In-Person Training</legend>
           <asp:GridView ID="gvInPerson" 
            CssClass="tablestyle" 
            AllowSorting="True"
            runat="server" 
            AutoGenerateColumns="False" 
            DataKeyNames = "trainingId"  
            OnPageIndexChanging="gvInPerson_PageIndexChanging" 
            AllowPaging="true" PageSize="10" 
            ForeColor="#333333" CellPadding="4" GridLines="None"> 
               <EditRowStyle BackColor="#999999" />
               <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
            <HeaderStyle   
                BackColor="#5D7B9D"   
                ForeColor="White" 
                Font-Bold="True"
                /> 
                <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
        <Columns>
            <asp:TemplateField>
                <HeaderTemplate>
                    <asp:CheckBox ID = "chkAll" runat="server" AutoPostBack="true" OnCheckedChanged="OnCheckedChanged" />
                </HeaderTemplate>
                <ItemTemplate>
                    <asp:CheckBox ID="CheckBox1" runat="server" AutoPostBack="true" OnCheckedChanged="OnCheckedChanged" />
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Name" ItemStyle-Width = "250">
                <ItemTemplate>
                    <asp:Label ID="nameLB" runat="server" Text='<%# Eval("FullName") %>'></asp:Label>
                    <asp:TextBox ID="txtName" runat="server" Text='<%# Eval("FullName") %>' Width="200" Visible="false"></asp:TextBox>
                </ItemTemplate>

<ItemStyle Width="250px"></ItemStyle>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Training" ItemStyle-Width = "250">
                <ItemTemplate>
                    <asp:Label ID="courseLB" runat="server" Text='<%# Eval("CourseName") %>'></asp:Label>
                    <asp:TextBox ID="txtCourse" runat="server" Text='<%# Eval("CourseName") %>'  Width="200" Visible="false"></asp:TextBox>
                </ItemTemplate>

<ItemStyle Width="150px"></ItemStyle>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Test Scores" ItemStyle-Width = "250">
                <ItemTemplate>
                    <asp:Label ID="scoresLB" runat="server" Text='<%# Eval("Scores") %>'></asp:Label>
                    <asp:TextBox ID="txtScores" runat="server" Text='<%# Eval("Scores") %>' Visible="false"></asp:TextBox>
                </ItemTemplate>

<ItemStyle Width="150px"></ItemStyle>
            </asp:TemplateField>
        </Columns>
               <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
               <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
               <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
               <SortedAscendingCellStyle BackColor="#E9E7E2" />
               <SortedAscendingHeaderStyle BackColor="#506C8C" />
               <SortedDescendingCellStyle BackColor="#FFFDF8" />
               <SortedDescendingHeaderStyle BackColor="#6F8DAE" />
    </asp:GridView>
    <br />
    <asp:Button ID="btnUpdate" runat="server" Text="Store the score" OnClick = "Update" Visible = "false"/>
            </fieldset>
       </div>
</div>

Open in new window


'///Codebehind

Imports System.Data
Imports System.Linq
Imports System.Configuration
Imports System.Data.SqlClient
Partial Class RecordInPersonAnswers
    Inherits System.Web.UI.Page
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
        If Not IsPostBack Then
            If (Not (Request.QueryString("SelectedUser")) Is Nothing) Then
                Session("selectedworker") = Request.QueryString("SelectedUser")
            End If
            If (Not (Request.QueryString("SelectedDate")) Is Nothing) Then
                Session("DateSelected") = Request.QueryString("SelectedDate")
            End If

            'Dim dateNtime As String
            'dateNtime = Request.QueryString("SelectedUser")
            Message.Text = "Add scores for the following training date: " & Request.QueryString("SelectedDate")
            'Response.Write(Session("DateSelected") & " - " & Session("selectedworker"))
            'Response.End()

            Me.BindGrid()
        End If

    End Sub
    Private Sub BindGrid()
        Dim cmd As New SqlCommand("select....from yourTable where = " & Session("selectedworker") & _
                        " ORDER BY FullName ASC")
        gvInPerson.DataSource = Me.ExecuteQuery(cmd, "SELECT")
        gvInPerson.DataBind()
    End Sub
    Private Function ExecuteQuery(ByVal cmd As SqlCommand, ByVal action As String) As DataTable
        Dim conString As String = ConfigurationManager.ConnectionStrings("DBConnectionString").ConnectionString
        Using con As New SqlConnection(conString)
            cmd.Connection = con
            Select Case action
                Case "SELECT"
                    Using sda As New SqlDataAdapter()
                        sda.SelectCommand = cmd
                        Using dt As New DataTable()
                            sda.Fill(dt)
                            Return dt
                        End Using
                    End Using
                Case "UPDATE"
                    con.Open()
                    cmd.ExecuteNonQuery()
                    con.Close()
                    Exit Select
            End Select
            Return Nothing
        End Using
    End Function

    Protected Sub Update(ByVal sender As Object, ByVal e As EventArgs)
        For Each row As GridViewRow In gvInPerson.Rows
            If row.RowType = DataControlRowType.DataRow Then
                Dim isChecked As Boolean = row.Cells(0).Controls.OfType(Of CheckBox)().FirstOrDefault().Checked
                If isChecked Then
                    Dim cmd As New SqlCommand("UPDATE yourTable SET scores = @scores WHERE courseId = @cname and trainingId = @TrainId")
                    cmd.Parameters.AddWithValue("@scores", row.Cells(3).Controls.OfType(Of TextBox)().FirstOrDefault().Text)
                    cmd.Parameters.AddWithValue("@cname", Session("selectedworker"))
                    cmd.Parameters.AddWithValue("@TrainId", gvInPerson.DataKeys(row.RowIndex).Value)
                    Me.ExecuteQuery(cmd, "SELECT")
                End If
                Message.Text = "Score successfully stored in the database"
            End If
        Next
        btnUpdate.Visible = False
        Me.BindGrid()
    End Sub

    Protected Sub OnCheckedChanged(ByVal sender As Object, ByVal e As EventArgs)
        Dim isUpdateVisible As Boolean = False
        Dim chk As CheckBox = TryCast(sender, CheckBox)
        If chk.ID = "chkAll" Then
            For Each row As GridViewRow In gvInPerson.Rows
                If row.RowType = DataControlRowType.DataRow Then
                    row.Cells(0).Controls.OfType(Of CheckBox)().FirstOrDefault().Checked = chk.Checked
                End If
            Next
        End If

        Dim chkAll As CheckBox = TryCast(gvInPerson.HeaderRow.FindControl("chkAll"), CheckBox)
        chkAll.Checked = True
        For Each row As GridViewRow In gvInPerson.Rows
            If row.RowType = DataControlRowType.DataRow Then
                Dim isChecked As Boolean = row.Cells(0).Controls.OfType(Of CheckBox)().FirstOrDefault().Checked
                For i As Integer = 1 To row.Cells.Count - 1
                    row.Cells(i).Controls.OfType(Of Label)().FirstOrDefault().Visible = Not isChecked
                    If row.Cells(i).Controls.OfType(Of TextBox)().ToList().Count > 0 Then
                        row.Cells(i).Controls.OfType(Of TextBox)().FirstOrDefault().Visible = isChecked
                    End If
                    If row.Cells(i).Controls.OfType(Of DropDownList)().ToList().Count > 0 Then
                        row.Cells(i).Controls.OfType(Of DropDownList)().FirstOrDefault().Visible = isChecked
                    End If
                    If isChecked AndAlso Not isUpdateVisible Then
                        isUpdateVisible = True
                    End If
                    If Not isChecked Then
                        chkAll.Checked = False
                    End If
                Next
            End If
        Next
        btnUpdate.Visible = isUpdateVisible
    End Sub
    Protected Sub gvInPerson_PageIndexChanging(ByVal sender As Object, ByVal e As GridViewPageEventArgs) Handles gvInPerson.PageIndexChanging
        gvInPerson.PageIndex = e.NewPageIndex
        Me.BindGrid()
        gvInPerson.DataBind()
    End Sub
End Class

Open in new window

0
 

Author Comment

by:welcome 123
ID: 40385008
U are a life saver
0
 
LVL 29

Expert Comment

by:sammySeltzer
ID: 40385074
You are very welcome!
0

Featured Post

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

732 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