Pulling parameters from a datagridview C#.NET
I have what is undoubtedly a simple question, but I can't seem to find that answer anywhere. I am writing a C# Windows form application that contains a datagridview that I'm using to run a SQL UPDATE statement out to the database with a dataadapter. I am using a parameterized query and need to populate the parameters with columns from the datagridview, but I'm not sure what syntax to use to represent the column itself. I thought that I could use something like:
da.UpdateCommand.Parameters.Add("@CustomerID", SqlDbType.VarChar, 4).Value = RuleTable.Columns["CustomerID"].ToString();
but while that doesn't is updating the correct records, it seems to have populated every one of those fields with the word "Data." This is roughly what I have in pseudocode:
using (SqlDataAdapter ruleTableDA = new SqlDataAdapter("SELECT CustomerID, etc ...", con))
{
ruleTableDA.UpdateCommand = new SqlCommand("UPDATE CustomerTable SET " +
"CustomerTable.CustomerID = @CustomerID. etc ... WHERE CustomerTable.MachineID = @MachineID", con);
if (BobstPresses.S开发者_开发技巧electedIndex >= 0)
{
ruleTableDA.UpdateCommand.Parameters.Add("@CustomerID",
SqlDbType.VarChar, 4).Value = RuleTable.Columns["CustomerID"];
DataTable dt = new DataTable();
dt = RuleTable.DataSource as DataTable;
ruleTableDA.Update(dt);
}
}
I'm not sure if I'm understanding correctly, a guess is that you want to use something like:
RuleTable.Rows[index].Cells["CustomerID"].Value
Where index is the index of the row in the DataGridView
containing the data you want to use in the query.
精彩评论