how to get only the edited row and save the date as modified date?

i have 6 fields in sql server:
    dtl_id    =    int
    cost      =    decimal
    startdate =    datetime
    enddate   =    datetime
    status    =    varchar
    modified_date = datetime   (modified_date is declare in sql as getdate)


.
this is my gridview:
modified_date: visible=false

| ID | cost | start date | end date | status | modified_date |
--------------------------------------------------------------
| 1  | 66.00| 1/02/13   | 1/20/13 | active |     1/02/13     |
| 2  | 12.00| 2/21/13   | 2/30/13 | active |     2/21/13     |
| 3  | 67.00| 3/28/13   | 3/10/13 | active |     3/28/13     |
-------------------------------------------------------------
| 4  | 20.00| 10/21/13  | 11/28/13| active |     10/21/13    |
-------------------------------------------------------------
                                         |Add|
|SAVE|=insert/update data to sql database

when i edit the 4th row and i click the SAVE button it should update in sql and save the modified_date(datetime now). but in my code, all data in gridview and its modified_date are modifying. i want only to save/update the edited row..pls. help me .

here's my code:

foreach (GridViewRow row in gvw.Rows)
            {
                    HiddenField dtl = (HiddenField)row.FindControl("hfDtl");
                    TextBox cost = (TextBox)row.FindControl("estimated_cost");
                    DateTimeControl dtissued = (DateTimeControl)row.FindControl("date_issued");
                    DateTimeControl dexp = (DateTimeControl)row.FindControl("expiration_date");
                    TextBox resp = (TextBox)row.FindControl("responsible");
                    DropDownList stat = (DropDownList)row.FindControl("status");
                    string msg = string.Empty;

                    msg = maDDL.submitItem(id.ToString(), Convert.ToInt32(dtl.Value).ToString(), Convert.ToDateTime(dtissued.SelectedDate).ToString()
                                            , txtCode.Text.ToString(), txtArea.Text.ToString(), txtDesc.Text.ToString()
                                            , Convert.ToDecimal(cost.Text).ToString(), Convert.ToDateTime(dexp.SelectedDate).ToString()
                                            , resp.Text.ToString(), stat.SelectedItem.Text.ToString()
                                            );

                    if (msg == "Record has been successfully saved!")
                    {
                        ShowMessage(msg);
                    }
                    else
                    {
                        Panel1.Visible = true;
                        lblMsg.Text = msg;
                        lblMsg.ForeColor = System.Drawing.Color.Red;
                    }
            }

tnx in advance.

Recommended Answers

All 6 Replies

Essentially you're submitting every row to the update method.

Instead, you need to select the current row you're working on and check whether it is "dirty".

foreach(GridViewRow rowView in dgv.Rows)
{
    DataRow row = rowView.Row;
    dgv.CurrentCell = dgv.Rows[rowView.Index].Cell[0];
    if(row.RowState != DataRowState.Unchanged || dgv.IsCurrentRowDirty)
    {
        maDDL.submitItem(.....);
    }
}

This is probably the easiest way I know of. I'm sure there's a cleaner way but I can't think of one off hand.

its not working sir. im not using datagridview sir ,im using gridview control.

My experise doesn't run much to database programming or web programming. However the System.Web.UI.WebControls.GridView has a SelectedRow property which should allow you to update just that row.

ok sir.thank you so much :)
i will try it.

Hi tinstaafl,

That gets the currently selected row of the currently selected cell. It seems like the OP is iterating every row in his view.

OP, this MSDN link should help. The GridView raises a RowUpdated event when you click the update button.

i use checkbox so i can get the selected row and modified it.and it worked.. so i have another question if its ok to all of you.

my question is how to hide the checked row when i click the delete button outside the gridview.i need to hide the checked row..i add checkbox as templatefield in gridview in the first column/cell.pls help me in this..

here's my code to hide row but its only hide from bottom to top.i want to hide the checked row.

 protected void lnkDelete1_Click(object sender, EventArgs e)
        {
            int count = 0;

            for (int i = 0; i < gvw.Rows.Count; i++)
            {
                CheckBox chk = (CheckBox)gvw.Rows[i].Cells[0].FindControl("chk");

                if (chk != null)
                {
                    if (chk.Checked)
                    {
                        int rowCnt = int.Parse(ViewState["rowCnt"].ToString());
                        int VisData = int.Parse(ViewState["VisData"].ToString());

                        if (VisData <= gvw.Rows.Count)
                        {
                            gvw.Rows[rowCnt - VisData].Visible = false;
                            ViewState["VisData"] = VisData + 1;
                            count++;
                        }
                    }
                }
            }
            ShowCount(count);
        }

and here's my code in binding gridview adn viewstate:

        private void BindGrid(string id)
        {
            DataTable dt = maDDL.getMADetail(id);

            try
            {
                gvw.DataSource = dt;
                gvw.DataBind();
            }
            catch (SqlException ex)
            {
                string msg = "Fetch Error:";
                msg += ex.Message;
                throw new Exception(msg);
            }
            finally
            {
                conn.Close();
            }

            ViewState["rowCnt"] = gvw.Rows.Count;
            ViewState["VisData"] = 1;
        }

tnx in advance.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.