Solved

save all gridview updates

Posted on 2011-02-25
3
209 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Sometimes in DotNetNuke module development you want to swap controls within the same module definition.  In doing this DNN (somewhat annoyingly) swaps the Skin and Container definitions to the default admin selections.  To get around this you need t…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

696 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