Bulk Edit in GridView using DOTNET

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.
welcome 123Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

CodeCruiserCommented:
0
sammySeltzerCommented:
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
welcome 123Author Commented:
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
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

sammySeltzerCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
welcome 123Author Commented:
U are a life saver
0
sammySeltzerCommented:
You are very welcome!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.