I finally solved one problem but now i have another. I found out how to get the name of the current field my selector is on, but now when I want to use an update command for it, they will not let me set a parameter. Heres 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)
{
string myCell = dgv_DataLookup.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString();
string myIndex = dgv_DataLookup.CurrentCell.OwningRow.Cells[6].Value.ToString();
string myColumn = dgv_DataLookup.CurrentCell.OwningColumn.HeaderCell.Value.ToString();
myColumn.Trim();
da.UpdateCommand = new OleDbCommand("UPDATE tbl_Orders SET @myColumn = @myCell WHERE oid = @myIndex", cs);
da.UpdateCommand.Parameters.Add("@myColumn", OleDbType.VarChar).Value = myColumn;
da.UpdateCommand.Parameters.Add("@myCell", OleDb开发者_运维百科Type.VarChar).Value = myCell;
da.UpdateCommand.Parameters.Add("@myIndex", OleDbType.Integer).Value = myIndex;
cs_Execute("UPDATE");
}
}
when I change myColumn to the actual column, say itemNum, it works fine. However, the error was Cannot update '@myColumn'; field not updateable
I also had a message box showing me the value of myColumn and it was the correct value. I was just wondering if this was legal, seting a parameter to a parameter. If not, then I'll just have to update all the rows. Thanks!
p.s. cd_Execute is just like cs.Open() da.UpdateCommand.ExecuteNonQuery() cs.Close()
You cannot have dynamic column names in parametrized queries. You will need to resort to dynamic SQL.
This will do what you want, though open to SQL Injection:
da.UpdateCommand = new OleDbCommand(string.Format("UPDATE tbl_Orders SET {0} = @myCell WHERE oid = @myIndex", myColumn), cs);
精彩评论