Avatar of Ru1995
Ru1995
Flag for United States of America asked on

Use a checkbox to edit gridview

I'm trying to use a checkbox to edit items in a grid view.  Instead of the user clicking on a edit hyperlink to edit the grid I want them to select a check box.  Below is the code that I'm using.  What I want to do is make it to where if a user clicks on a check box the edit function executes where the user can update the information being pulled from the database to populate the grid.  How can I do this?


 <asp:UpdatePanel ID="UpdatePanel1" runat="server">
    <ContentTemplate>
    <asp:GridView ID="GridView1" runat="server" OnRowEditing="EditJob" OnRowUpdating="UpdateJob"
                    OnRowCancelingEdit="CancelEdit" OnRowDeleting="delete" AutoGenerateColumns="False"
                    CellPadding="4" DataKeyNames="job_id" 
            OnPageIndexChanging="GridView1_PageIndexChanging" DataSourceID="sqlJobList" ShowFooter="True" 
                    ForeColor="#333333" GridLines="None" Height="148px" Width="866px"  
            AllowSorting="True"> 
        <RowStyle BackColor="#E3EAEB" />
        <Columns>
             <asp:CommandField ShowDeleteButton="True" ShowEditButton="false" />
             <asp:TemplateField ShowHeader="False">
                <ItemTemplate>
                    <asp:LinkButton ID="btnView" runat="server" CausesValidation="False" 
                        CommandArgument='<%# Eval("job_id") %>' OnCommand="viewPosting" Text="View"></asp:LinkButton>
                </ItemTemplate>
            </asp:TemplateField>

 <asp:TemplateField HeaderText="Job ID" InsertVisible="False" 
                SortExpression="job_id" Visible="False">
                <EditItemTemplate>
                    <asp:Label ID="lbljob_id" runat="server" Text='<%# Bind("job_id") %>'></asp:Label>
                </EditItemTemplate>
                <ItemTemplate>
                    <asp:Label ID="lbljob_id" runat="server" Text='<%# Bind("job_id") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>

 <asp:TemplateField HeaderText="Title">
                <ItemTemplate>
                    <asp:Label ID="lbltitle" runat="server" Text='<%# Eval("title") %>'></asp:Label>
                </ItemTemplate>
                <EditItemTemplate>
                    <asp:TextBox ID="txtTitle" runat="server" Text='<%# Eval("title") %>'></asp:TextBox>
                </EditItemTemplate>
                <FooterTemplate>
                    <asp:TextBox ID="txtTitle" runat="server"></asp:TextBox>
                </FooterTemplate>
            </asp:TemplateField>


            <asp:TemplateField HeaderText="Region">
                <ItemTemplate>
                    <asp:Label ID="lblRegion" runat="server" Text='<%# Eval("Region") %>'></asp:Label>
                </ItemTemplate>
                <EditItemTemplate>
                    <asp:TextBox ID="txtRegion" runat="server" Text='<%# Eval("Region") %>'></asp:TextBox>
                </EditItemTemplate>
                <FooterTemplate>
                    <asp:TextBox ID="txtRegion" runat="server"></asp:TextBox>
                </FooterTemplate>
            </asp:TemplateField>
            
             <asp:TemplateField HeaderText="Open Posting">
                <ItemTemplate>
                    <asp:Label ID="lblOpen_Check" runat="server" Text='<%# Eval("open_check") %>'></asp:Label>
                </ItemTemplate>
                <EditItemTemplate>
                    <asp:DropDownList ID="DropDownList1" runat="server" 
                        SelectedValue='<%# Bind("open_check") %>'>
                        <asp:ListItem Value="y">Yes</asp:ListItem>
                        <asp:ListItem Value="n">No</asp:ListItem>
                    </asp:DropDownList>
                </EditItemTemplate>
                <FooterTemplate>
                     <asp:DropDownList ID="DropDownList1" runat="server" 
                        SelectedValue='<%# Bind("open_check") %>'>
                        <asp:ListItem Value="y">Yes</asp:ListItem>
                        <asp:ListItem Value="n">No</asp:ListItem>
                    </asp:DropDownList>
                </FooterTemplate>
            </asp:TemplateField>

            <asp:TemplateField HeaderText="Open ID" InsertVisible="False" 
                SortExpression="openjob_id" Visible="False">
                <EditItemTemplate>
                    <asp:Label ID="lblopenjob_id" runat="server" Text='<%# Bind("openjob_id") %>'></asp:Label>
                </EditItemTemplate>
                <ItemTemplate>
                    <asp:Label ID="lblopenjob_id" runat="server" Text='<%# Bind("openjob_id") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>



             <asp:TemplateField HeaderText="Close Date">
                <ItemTemplate>
                    <asp:Label ID="lblClose_Date" runat="server" Text='<%# Eval("close_date") %>'></asp:Label>
                </ItemTemplate>
                <EditItemTemplate>
                    <asp:TextBox ID="txtclose_date" runat="server" Text='<%# Eval("close_date") %>'></asp:TextBox>
                </EditItemTemplate>
                <FooterTemplate>
                    <asp:TextBox ID="txtclose_date" runat="server"></asp:TextBox>
                </FooterTemplate>
            </asp:TemplateField>

 <%--Use checkbox to edit items--%>
            <asp:TemplateField HeaderText="Action CheckBox">
            <ItemTemplate>
                <asp:CheckBox ID="chkSelect" runat="server" OnCheckedChanged="EditJob" AutoPostBack="true" />
            </ItemTemplate>
            <HeaderTemplate>
                <input id="chkSelect" name="Select All" onclick="SelectAll(this)" type="checkbox"
                runat="server" />All
            </HeaderTemplate>
            <FooterTemplate>
                <asp:Button ID="btnadd" runat="server" Text="Add" OnClick="AddNewJob" />
                </FooterTemplate>

</asp:TemplateField>
            </Columns>
    </asp:GridView>
    </ContentTemplate>
    </asp:UpdatePanel>


<!--Here is the code behind part of the page

  '**insert new Job in gridview**

    Protected Sub AddNewJob(ByVal sender As Object, ByVal e As EventArgs)
        Dim Title As String = DirectCast(GridView1.FooterRow.FindControl("txtTitle"), TextBox).Text
        Dim Region As String = DirectCast(GridView1.FooterRow.FindControl("txtRegion"), TextBox).Text
        Dim open_check As String = DirectCast(GridView1.FooterRow.FindControl("DropDownList1"), DropDownList).SelectedValue

        ' Dim close_date As String = DirectCast(GridView1.FooterRow.FindControl("DropDownList1"), DropDownList).SelectedValue
        Dim cmd As New SqlCommand()
        cmd.CommandType = CommandType.Text
        cmd.CommandText = "insert into tblJobPostings(Title,Region,open_check) values(@Title,@Region,@open_check); SELECT  job_id, [openjob_id],[title], [open_check], [region],[close_date] FROM (SELECT j.job_id, [openjob_id],[title], [open_check], [region],[close_date],ROW_NUMBER() OVER(PARTITION BY j.job_id ORDER BY [close_date] DESC) AS RowNo FROM [tblJobPostings] j LEFT OUTER JOIN [tblOpenJobs] hON j.job_id = h.job_id  )sub WHERE sub.RowNo = 1"
        cmd.Parameters.Add("@Title", SqlDbType.VarChar).Value = Title
        cmd.Parameters.Add("@Region", SqlDbType.VarChar).Value = Region
        cmd.Parameters.Add("@open_check", SqlDbType.VarChar).Value = open_check
        'cmd.Parameters.Add("@close_date", SqlDbType.VarChar).Value = close_date
        cmd.Connection = con
        con.Open()
        cmd.ExecuteNonQuery()
        con.Close()
        binddata()
    End Sub

    'Function to bind data
    Private Sub binddata()

        con.Open()
        Dim query As New String("SELECT  job_id, [openjob_id],[title], [open_check], [region],[close_date] FROM (SELECT j.job_id, [openjob_id],[title], [open_check], [region],[close_date], ROW_NUMBER() OVER(PARTITION BY j.job_id ORDER BY [close_date] DESC) AS RowNo FROM [tblJobPostings] j LEFT OUTER JOIN [tblOpenJobs] h ON j.job_id = h.job_id  ) sub WHERE sub.RowNo = 1 ")
        Dim da As New SqlDataAdapter(query, con)
        Dim ds As New DataSet()
        da.Fill(ds)
        If ds.Tables(0).Rows.Count > 0 Then
            'GridView1.DataSource = ds
            GridView1.DataBind()
            con.Close()
        End If
    End Sub

    '***to edit gridview**
    Protected Sub EditJob(ByVal sender As Object, ByVal e As GridViewEditEventArgs)
        GridView1.EditIndex = e.NewEditIndex
        binddata()
    End Sub

    '**to cancel edit**
    Protected Sub CancelEdit(ByVal sender As Object, ByVal e As GridViewCancelEditEventArgs)
        GridView1.EditIndex = -1
        binddata()
    End Sub

    '**to delete from gridview from database**
    Protected Sub delete(ByVal sender As Object, ByVal e As GridViewDeleteEventArgs)
        con.Open()
        Dim cmd As New SqlCommand("delete from tblJobPostings where job_id=@job_id", con)
        cmd.Parameters.Add("@job_id", SqlDbType.Int).Value = DirectCast(GridView1.Rows(e.RowIndex).FindControl("lbljob_id"), Label).Text
        cmd.ExecuteNonQuery()
        con.Close()
        binddata()
    End Sub

    '**to update from gridview and database**
    Protected Sub UpdateJob(ByVal sender As Object, ByVal e As GridViewUpdateEventArgs)
        Dim job_id As Integer = DirectCast(GridView1.FooterRow.FindControl("lbljob_id"), Label).Text
        Dim openjob_id As Integer = DirectCast(GridView1.FooterRow.FindControl("lblopenjob_id"), Label).Text
        Dim close_date As Date = DirectCast(GridView1.FooterRow.FindControl("txtclose_date"), TextBox).Text
        Dim open_check As String = DirectCast(GridView1.FooterRow.FindControl("DropDownList1"), DropDownList).SelectedValue
        'execute store procedure
        con.Open()
        Dim cmd As New SqlCommand("spJobPostings", con)
        cmd.CommandType = CommandType.StoredProcedure

        cmd.Parameters.AddWithValue("@job_id", CInt(job_id))
        cmd.Parameters.AddWithValue("@openjob_id", CInt(openjob_id))
        cmd.Parameters.AddWithValue("@close_date", CDate(close_date))
        cmd.Parameters.AddWithValue("@open", CStr(open_check))


    End Sub

   Protected Sub GridView1_PageIndexChanging(ByVal sender As Object, ByVal e As GridViewPageEventArgs)
        GridView1.PageIndex = e.NewPageIndex
        binddata()
    End Sub

    Protected Sub dvJobs_ItemInserted(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DetailsViewInsertedEventArgs) Handles dvJobs.ItemInserted
        GridView1.DataBind()

    End Sub

    Protected Sub dvJobs_ItemUpdated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DetailsViewUpdatedEventArgs) Handles dvJobs.ItemUpdated
        GridView1.DataBind()
    End Sub


    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        If Not Page.IsPostBack Then
            'tabMenu.Tabs("3").Selected = True
            binddata()
        End If

    End Sub

Open in new window

ASP.NET.NET Programming

Avatar of undefined
Last Comment
Ru1995

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
kblau

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
Ru1995

ASKER
I apologize I should have mentioned I'm pretty new to .net can you kind of show me what you mean?
Ru1995

ASKER
I figured it out with a co-worker but I'll give the points anyway
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck