Solved

Bulk Edit in GridView using DOTNET

Posted on 2014-10-14
6
371 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
  • 3
  • 2
6 Comments
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 40381918
0
 
LVL 28

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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
LVL 28

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 28

Expert Comment

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

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

758 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now