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);
}
但上面的代码显示在表格中输入的所有行。我的意思是行不是根据条件而改变的。
任何人都可以帮助我找到正确的代码片段用于检索基于条件的数据集?
请帮帮我。
在此先感谢。