Bind DataGridView with BindingSource with .net 2.0

M

Mike

i have a small difficulties with BindingSource and dataGridView bind
db has properly opened and bind doesn't works. Unfortunately I didn't
find any good example how to connect MS Access with dataGridView and
BindSource class.

Maybe something like this:

DataTable dtResult = new DataTable();
dtResult = GetData(); // this line returns a error GetData
doesn't exists!
dgvPoslovi.DataSource = dtResult;
dgvPoslovi.Update();

I'd like to that programmaticaly.

Thanks for help

CODE
--------------------------------------------------

private bool FindJob(string JobToFind)
{
string selectString;


string connectionString =
@"Provider=Microsoft.Jet.OLEDB.4.0;" +
@"Data Source=" +
@"|DataDirectory|\\Data\\JS.mdb";
/*
@"User Id=Admin;" +
@"Jet OLEDB:Database Password=ahsss"
*/

OleDbConnection MyConn = new
OleDbConnection(connectionString);

try
{
// open a connection
MyConn.Open();
}
catch(OleDbException ex)
{
MessageBox.Show("Error in connection
...."+ ex.Message);
return false;
}
selectString = "SELECT * FROM PosaoTable WHERE PosaoID='"
+ JobToFind + "' ORDER BY PosaoID";

OleDbDataAdapter da = new
OleDbDataAdapter(selectString, MyConn);
OleDbCommandBuilder cmd = new OleDbCommandBuilder(da);
DataTable dt = new DataTable();
//da.Fill(dt);

BindingSource bs = new BindingSource();
bs.DataSource = dt;
dgPoslovi.DataSource = bs;

MyConn.Close();

return true;
}/*FindJob()*/
 
N

Nicholas Paldino [.NET/C# MVP]

Mike,

Why did you comment out the call to Fill? That should be getting the
data, then setting the data source on the binding source, which will then
populate the grid.
 
M

Mike

Because that doesn't works.
Error description: "Data type mismatch in criteria expression."

Furthermore, less important, I am not sure is that the way how it
should work with .net 2.0
 
N

Nicholas Paldino [.NET/C# MVP]

Mike,

That's exactly how it should work when you are using the BindingSource.

Basically, the flow is like this:

- The ADO.NET Data Provider (in this case the classes in the
System.Data.OleDb namespace) is used to fill a DataSet.

- The DataSet is the underlying data source for the BindingSource.

- The BindingSource is the source of the data for the grid.

The BindingSource isn't responsible for fetching data or anything of
that nature. It only simplifies operations which can be performed on the
data source and the binding.
 
M

Mike

Thanks Nicholas,
that was the way how I done that with VS 2003 and framework 1.x and it
works well. I am confused why that doesn't work with VS 2005 framework
2,0.

I am faced with errors with my code as I described.

If I understand well we should use new approach with Binding Source
class something like this:
DataGridView dgv = new DataGridView();
BindingSource bs = new BindingSource();
DataTable dt = GetDataTableFromSomeWhere(); // Gets the
DataTable

bs.DataSource = dt;
dgv.DataSource = bs;
 
N

Nicholas Paldino [.NET/C# MVP]

Mike,

Yes. The BindingSource was introduced to unify access to the data, as
well as information related to the binding on that data. It doesn't do
anything more than that. You are still responsible for giving it the data
in the traditional ways.
 
M

Mike

Nicholas,

It is very interesting. Thanks for patience and understanding.

I try to use part of my old code within this application (agian) and
that is part of code which works great in another "old fashion"
application.

Error occured in the line:
dgvPoslovi.SetDataBinding(ds, dataTableName);
Error description: 'System.Windows.Forms.DataGridView' does not
contain a definition for 'SetDataBinding'

Only difference between this "old fashion" approach is in dataGrid. In
this aoplication we are working with DataGridView control instead
which is not capable to recognize definition for 'SetDataBinding'

I really do not understand how I should do that with new style???

Here is part of code:

private bool FillTable(string JobToFind)
{
string selectString;
string connectionString =
@"Provider=Microsoft.Jet.OLEDB.4.0;" +
@"Data Source=" +
@"|DataDirectory|\\Data\\Job.mdb";

OleDbConnection Conn = new
OleDbConnection(connectionString);

try
{
Conn.Open();
}
catch (OleDbException ex)
{
MessageBox.Show("Error in connection ..."+ex.Message);
return false;
}

selectString = "SELECT * FROM PosaoTable WHERE PosaoID='"
+ JobToFind + "' ORDER BY PosaoID";

OleDbCommand ListCommand = new OleDbCommand(selectString,
Conn);
OleDbDataAdapter MyDataAdaper = new
OleDbDataAdapter(ListCommand);

DataSet ds = new DataSet();
string dataTableName = "PosaoTable";
MyDataAdaper.Fill(ds, dataTableName);

// DataGrid binding
dgvPoslovi.SetDataBinding(ds, dataTableName);
Conn.Close();
return true;
} /*End of FillTable()*/
 
M

Mike

Correction

Instead:
Only difference between this "old fashion" approach is in dataGrid. In
this aoplication we are working with DataGridView control instead
which is not capable to recognize definition for 'SetDataBinding'

Should be:
Only difference between this "old fashion" approach is in dataGrid. In
this application we are working with new DataGridView control instead
old one and the new one is not capable to recognize definition for
'SetDataBinding'
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top