Solved

save all gridview updates

Posted on 2011-02-25
3
205 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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
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…

707 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

18 Experts available now in Live!

Get 1:1 Help Now