开发者

DataGridView Edit Cell

my last question was closed because it wasn't clear, so I'll try again because I really need help for this...

I am using Visual Studio 2010 with an OLEDB connection. Right now I'm developing a windows form. I noticed that a datagridview allows you to have an edit/add/delete mode, so I want to be able to use it.

What I want to do is have the cellcontentchanged event used to edit my database. When I change the content and leave, it'll ask me if I want to edit this cell. If I say yes, then that cell will be changed.

I made sure ReadOnly = false and enabled = true. Before, what from what I read, I would just do something like this:

adapter.Update(dataset); 

or something like this. however, this does not save the changes of my databse. I'm also not sure how to edit programmatically. I'm also not sure if I should have an OLEDB update command or not. If anything is not clear, please reply and I would be glad to clarify. Thank you!

edit: sorry if its still hard to understand!

here is some code...

    private void dgv_DataLookup_CellValueChanged(object sender, DataGridViewCellEventArgs e)
    {

        DialogResult dr;
        dr = MessageBox.Show("Are you sure you want to edit this field?", "Edit Cell", MessageBoxButtons.YesNo);

        if (dr == DialogResult.Yes)
        {
            //update the table in my database
        }
    }

this is not a lot yet, but hopefully you can try to see what i'm trying to do

edit again: This is all the relevant code for this part (this is the code that displays the datagridview that I want to edit )

    OleDbConnection cs;
    OleDbDataAdapter da = new OleDbDataAdapter();

    DataSet dsB = new DataSet();



    //If clicked search button
        else if (combo_View.Text == "Orders")
        {

            da.SelectCommand = new OleDbCommand("SELECT * FROM TestQuery WHERE (VendorName = @VendorName OR @VendorName = '') AND (CustomerName = @CustomerName OR @CustomerName = '')  AND ((@From IS NULL AND @To IS NULL) OR orderDate BETWEEN @From AND @To) AND (ItemNum = @ItemNum OR @ItemNum = '') AND (PO = @PO OR @PO = '') ORDER BY CustomerName", cs);

            da.SelectCommand.Parameters.Add("@VendorName", OleDbType.VarChar).Value = combo_VendorView.Text.ToString();
            da.SelectCommand.Parameters.Add("@CustomerName", OleDbType.VarChar).Value = combo_CustomerView.Text.ToString();

            if (!chk_viewAllDates.Checked)
            {
                da.SelectCommand.Parameters.Add("@From", OleDbType.Date).Value = "#" + tp_viewFrom.Value.Date.ToString("M/d/yyyy") + "#";
                da.SelectCommand.Parameters.Add("@To", OleDbType.Date).Value = "#" + tp_viewTo.Value.Date.ToString("M/d/yyyy") + "#";
            }

            else
            {
                da.SelectCommand.Parameters.Add("@From", OleDbType.Date).Value = DBNull.Value;
                da.SelectCommand.Parameters.Add("@To", OleDbType.Date).Value = DBNull.Value;
            }

            da.SelectCommand.Parameters.Add("@PO", OleDbType.VarChar).Value = txt_POLookup.Text.ToString();
            da.SelectCommand.Parameters.Add("@ItemNum", OleDbType.VarChar).Value = combo_ItemNumLookup.Text.ToString();

            dsB.Clear();
            da.Fill(dsB);
            dgv_DataLookup.DataSource = dsB.Tables[0];

        }

please let me know if you need more code. any help is appreciat开发者_开发技巧ed :)


I'm not really sure what you are asking here either, but I'll make some assumptions that you are just asking how to save the edited data back to the database. I would not use CellValueChanged event. Usually you just let the user edit the data in the grid and then have them click a save button or save the data on form closing.

void Save()
{ 
    OleDbConnection con=new OleDbConnection("Put your connect string here");
    OleDbDataAdapter da = new OleDbDataAdapter("SELECT * FROM TestQuery", con);
    OleDbCommandBuilder cb = new OleDbCommandBuilder(da);
    DataTable tbl = dsB.Tables[0];
    da.Update(tbl);
    tbl.AcceptChanges();
}

You'll probably want to reuse some of the objects you have already created like the connection but this is the basics.


Fixed this myself. Just use owningcolumn owningrow to get values i needed. Thanks

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜