Solved

save all gridview updates

Posted on 2011-02-25
3
207 Views
Last Modified: 2012-05-11
have a gridview with checkboxes - allow the user to check/uncheck a row. how do i save all the changes instead of going row by row edits?

asp:GridView ID="grdvwD" runat="server" AutoGenerateColumns="False" 
    CellPadding="4" ForeColor="#333333" 
    GridLines="None" DataKeyNames="ID" AllowSorting="True" 
    ondatabound="grdvwD_DataBound" onsorting="grdvwD_Sorting">
    <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
    <Columns>
        <asp:TemplateField>
           <ItemTemplate>
                <asp:CheckBox ID="chkStatus" runat="server" />
           </ItemTemplate>                    
        </asp:TemplateField>
        <asp:BoundField DataField="ID" HeaderText="ID" SortExpression="ID" InsertVisible="False" ReadOnly="True" Visible="False" />
        <asp:TemplateField HeaderText="Active"  SortExpression="Active" >
           <ItemTemplate >
                <asp:Label ID="lblActive" runat="server" Text='<%# Eval("Active") %>'></asp:Label>
           </ItemTemplate>                    
        </asp:TemplateField>
        <asp:BoundField DataField="Center Name" HeaderText="Center Name" 
            SortExpression="Center Name" />
    </Columns>
    <EditRowStyle BackColor="#999999" />
    <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
    <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
    <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>

<asp:Button ID="btnSave" runat="server" Text="Save" onclick="btnSave_Click" />



protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                loadDataGrid();
            }
	}


protected void loadDataGrid()
        {
            SQLQryString = "select from db tbl";
            SQLCmd = new SqlCommand(SQLQryString);

            using (SQLCon = new SqlConnection(SQLConnectionString))
            {
                SQLCmd.Connection = SQLCon;

                SQLCon.Open();
                SQLCmd.ExecuteNonQuery();

                SQLDR = SQLCmd.ExecuteReader();

                grdvwD.DataSource = SQLDR;
                grdvwD.DataBind();
            }
        }


        protected void btnSave_Click(object sender, EventArgs e)
        {
            //saveDataGrid();

        }

Open in new window

0
Comment
Question by:vicomin
  • 2
3 Comments
 
LVL 13

Accepted Solution

by:
agarwalrahul earned 500 total points
ID: 34981092
you can use the following code:

foreach (GridViewRow currentRow in grdHierarchy.Rows)

{if (((CheckBox)currentRow.FindControl("chkSelect")).Checked == true)

{

Get the template name and save it in database
}
}


0
 

Author Comment

by:vicomin
ID: 34982046
does this make sense? but I'm not sure how to do this or get the databound value of ID


protected void btnSave_Click(object sender, EventArgs e)
        {
            //string rowId = string.Empty;
            //string activeValue = string.Empty;

            //update each row - use value of ID and value of lblActive
            foreach (GridViewRow currentRow in grdvwD.Rows)
            {
                //how do i get the id when it is a bound field? do i need to convert it to a template field?

                Label lbl = (Label)currentRow.FindControl("lblActive");

                //update the row
                saveDataGrid(rowId, lbl.Text); //method call to update database table
            }
        }

Open in new window

0
 

Author Comment

by:vicomin
ID: 34982905
one more quick question - what i've got modifies all of the rows even if the user did not change anything. how do i detect only the changed rows?
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

This article discusses the ASP.NET AJAX ModalPopupExtender control. In this article we will show how to use the ModalPopupExtender control, how to display/show/call the ASP.NET AJAX ModalPopupExtender control from javascript, how to show/display/cal…
In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…

785 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