Solved

save all gridview updates

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

809 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