C# Visual Studio 2012 - Devexpress gridView - saving data back to database

Hi experts,

I have a table pulled from SQL where I am allowing the user to edit some of the fields and be able to hit a save button to update it in sql. My challenge is that I need to give the user the ability to hit save at any time as they're editing a field and be able to save back to the db. They do not need to get out of the row or the cell. They could type something half way in a cell and still be able to save it. What's the best way to do this?

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

I actually use a hidden button in a panel to achieve the same ballpark result.  When the user leaves the textbox the button automatically clicks and saves to the DB.

this sample is without a gridview that requires modified code..  it filters the id in the same way and avoids painful grid c# syntax etc

       <asp:Panel ID="Panel1" runat="server" Style="visibility: hidden;" Height="1px">
            <asp:Button ID="btnUploadImageUpdateTitle" runat="server" Style="visibility: hidden;" OnClick="btnUploadImageUpdateTitle_Click" Width="1" ForeColor="#003756" BackColor="#003756" BorderColor="#003756" Height="1" />

Open in new window

It then runs the following code; this updates 3 fields in sql. It should help get you going at least.. Just make the button visible if you want them to click it. Otherwise it just saves automatically..One less button for people to click

protected void btnUploadImageUpdateTitle_Click(object sender, EventArgs e)
        string siteid = SiteID.ToString();  //I usually use a session variable to get the filter here

                    string Usql = "";

                    Usql = "UPDATE dbo.YourTableName SET Image10 = @Image10, Image10OriginalName = @Image10OriginalName, Image10Title = @Image10Title";
                    Usql += " WHERE  SiteID=" + SiteID.ToString();

                    SqlCommand Ucom = new SqlCommand();
                    if (Session["Image10"].ToString() != "0")
                        Ucom.Parameters.Add("@Image10", SqlDbType.VarChar).Value = DMPDBSession.Image10.ToString();
                        Ucom.Parameters.Add("@Image10OriginalName", SqlDbType.VarChar).Value = DMPDBSession.Image10OriginalName.ToString();
                        Ucom.Parameters.Add("@Image10Title", SqlDbType.VarChar).Value = tbxTitleImg10.Text.ToString();
                        Ucom.Parameters.Add("@Image10", SqlDbType.VarChar).Value = "";
                        Ucom.Parameters.Add("@Image10OriginalName", SqlDbType.VarChar).Value = "";
                        Ucom.Parameters.Add("@Image10Title", SqlDbType.VarChar).Value = "";
                        data.ExecuteNonQuery(Usql, Ucom);
                    catch (Exception ex)
                        Response.Write("<script>alert('" + Server.HtmlEncode(ex.ToString()) + "')</script>");

Open in new window

this is .net 4.5....try changing your field names and types where required.  You will obviously need to match the fields with your Markup etc.

i think the code below was actually my autosubmit code.  I had the same thing running slightly different on the same page. It was originally from a drag and drop image and auto update database type page.

 <asp:Button ID="btnAutoSubmitImage1" runat="server" Style="visibility: hidden;"
                                Visible="False" />
                                    function submitFile(sender, e) {
                                        $get("<%= btnAutoSubmitImage1.ClientID %>").click();
                                    function submitFile(sender, e) {
                                        $get("<%= btnUploadImageUpdateTitle.ClientID %>").click();


Open in new window

I believe that the field you are editing is committed (in the data context) at the time you focus away from it.
When you hit your "Save" button you are un-focusing the field being edited.
I think it is sufficient to get your context and persist it to the db.

Furthermore, I suggest you to contact the DevExpress support, as they seem to be very responsive on any kind problem or question.


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
timberadminAuthor Commented:
Found the exact answer on the DevExpress forums. Thanks guys.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.