我有一个datagridview,我更新并填写。但是,我在datagridview中的列(ID)中搜索值(returnID)的部分显示“无法找到列名为ID的列。 参数名称:columnName“我似乎无法让它工作。如果有人有任何帮助,我们非常感激。
代码:
private void Application_Load(object sender, EventArgs e)
{
// TODO: This line of code loads data into the 'registrationDataSet1.TestDB' table. You can move, or remove it, as needed.
this.testDBTableAdapter.Fill(this.registrationDataSet1.TestDB);
textBox1.Select();
}
private void DataTable_Connection(object returnName, string returnID)
{
String varID;
Object varName;
varID = textBox1.Text.Replace(@"L", "").ToString();
varName = returnName;
OleDbConnection OLEDB_Connection = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\\Registration.accdb");
OleDbCommand updateCmd = new OleDbCommand();
try
{
updateCmd.CommandText = "INSERT INTO TestDB ([Name], [ID]) VALUES (@NAME, @ID)";
updateCmd.Parameters.AddWithValue("@Name", varName);
updateCmd.Parameters.AddWithValue("@ID", varID);
OLEDB_Connection.Open();
updateCmd.Connection = OLEDB_Connection;
updateCmd.ExecuteNonQuery();
OLEDB_Connection.Close();
}
catch
{
}
textBox1.Clear();
int index = -1;
DataGridViewRow row = DataGridView1.Rows
.Cast<DataGridViewRow>()
.Where(r => r.Cells["ID"].Value.ToString().Equals(returnID))
.First();
index = row.Index;
}
private void Register_Selection(object sender, KeyPressEventArgs e)
{
SqlConnection DBConnection = new SqlConnection("Data Source=DATABASE;Initial Catalog=imis;Integrated Security=True");
SqlCommand cmd = new SqlCommand();
String returnValue;
Object returnName;
Object returnID;
string txtend = textBox1.Text;
if (e.KeyChar == 'L')
{
e.Handled = true;
DBConnection.Open();
}
if (DBConnection.State == ConnectionState.Open)
{
if (textBox1.Text.Length != 6) return;
{
cmd.CommandText = ("SELECT last_name +', '+ first_name from name where id =@Name");
cmd.Parameters.Add(new SqlParameter("Name", textBox1.Text.Replace(@"L", "")));
cmd.CommandType = CommandType.Text;
cmd.Connection = DBConnection;
returnValue = cmd.ExecuteScalar() + "\t " + textBox1.Text.Replace(@"L", "");
returnName = cmd.ExecuteScalar();
returnID = textBox1.Text.Replace(@"L", "");
DBConnection.Close();
DataTable_Connection(returnName, returnID.ToString());
this.testDBTableAdapter.Fill(this.registrationDataSet1.TestDB);
}
}
}
答案 0 :(得分:1)
我发现当我测试这个... MessageBox.Show(dataGridView1.Columns [0]请将.Name);
消息框显示id列headertext为“idDataGridViewTextBoxColumn”
因此,我没有引用列名“ID”,而是使用“idDataGridViewTextBoxColumn”来引用它。它起作用了。
答案 1 :(得分:0)
尝试提供示例0
的列索引,而不是给出列名ID
。