1
我正在創建一個應用程序,其中我想根據用戶在文本框中輸入的值在DataGridView中顯示行。根據條件從DataBase爲DataGridView填充值
例如, 如果用戶在文本框中輸入書名,則應在DataGridView中顯示關於該書的所有詳細信息。
我已經使用了以下值編碼:
SqlConnection objSqlConnection = new SqlConnection();
string connectionStringSettings = "Data Source =.; Initial Catalog = LibrarySystemManagement;Integrated Security = SSPI";
private void btnSearch_Click(object sender, EventArgs e)
try
{
objSqlConnection.ConnectionString = connectionStringSettings;
objSqlConnection.Open();
if ((txtBookName.Text != "") || (txtCategory.Text != ""))
{
SqlDataAdapter objSqlDataAdapter = new SqlDataAdapter("select * from LIBRARYBOOKDETAILS where Title = '"+txtTitle.Text+"'", objSqlConnection);
SqlCommandBuilder objSqlCommandBuilder = new SqlCommandBuilder(objSqlDataAdapter);
DataTable objDataTable = new DataTable();
objSqlDataAdapter.Fill(objDataTable);
BindingSource objBindingSource = new BindingSource();
objBindingSource.DataSource = objDataTable;
dataGridView1.DataSource = objBindingSource;
objSqlDataAdapter.Update(objDataTable);
objSqlConnection.Close();
}
}
catch (Exception e1)
{
MessageBox.Show(e1.Message + e1.Source);
}
但上面的代碼顯示在表格中輸入的所有行。我的意思是行不是根據條件而改變的。
任何人都可以幫助我找到正確的代碼片段用於檢索基於條件的數據集?
請幫幫我。
在此先感謝。