chris1973

chris1973
- Name: [not set]
- Favorite Languages: [not set]
- Website: [not set]
- Location: [not set]
- About Me: [not set]
-
C# Tutorial - Binding a DataGridView to a Database
05/22/2009 - 03:25
I placed the code in the click event of a button, as is, how would you declare the bSource, dAdapter, and dTable somewhere else, as the objects are instantiated with parameters ie
OleDbDataAdapter dAdapter = new OleDbDataAdapter(strSql, con);
AND
OleDbCommandBuilder cBuilder = new OleDbCommandBuilder(dAdapter);
If i can get my head around this it will go along way to helping with other apps that i am coding
Much appreciated
Chris
PS I am coming from a VB6 background and finding the transition to C# a bit challenging
CODE:
private void btnSearch_Click_1(object sender, EventArgs e)
{
txtSearch.BackColor = Color.White;if (txtSearch.Text.Trim() != "")
{try
{
OleDbConnection con = new OleDbConnection(clsConnSrt.GetConnectionString("PastelCostingSlip.Properties.Settings.PastelCostingSlipConnectionString").ToString());string strSql;
strSql = "SELECT * FROM Sheet1 WHERE PlantDescription LIKE '" + txtSearch.Text.Trim() + "%" + "'";OleDbDataAdapter dAdapter = new OleDbDataAdapter(strSql, con);
//create a command builder
OleDbCommandBuilder cBuilder = new OleDbCommandBuilder(dAdapter);//create a DataTable to hold the query results
DataTable dTable = new DataTable();con.Open();
//fill the DataTable
dAdapter.Fill(dTable);//BindingSource to sync DataTable and DataGridView
BindingSource bSource = new BindingSource();//set the BindingSource DataSource
bSource.DataSource = dTable;//set the DataGridView DataSource
dataGridView1.DataSource = bSource;con.Close();
con.Dispose();}
catch (System.Exception ex)
{
System.Windows.Forms.MessageBox.Show(ex.Message);
}
finally
{}
-
C# Tutorial - Binding a DataGridView to a Database
05/21/2009 - 04:36
Hi
I have followed your example without a hitch, however,when doing the update in the gridviews validating event
private void dataGridView1_Validating(object sender, CancelEventArgs e)
{
bSource.EndEdit();dAdapter.Update(dTable);
}The following errors are encountered:
Error 1: The name 'bSource' does not exist in the current context
Error 2: The name 'dAdapter' does not exist in the current context
Error 3:The name 'dTable' does not exist in the current context
I know there is something simple to fix here, just don't know what it is.
The article is extremely helpful, thanks
Recent Comments