Solved

C# BindingNavigator Control

Posted on 2006-07-19
4
3,473 Views
Last Modified: 2012-08-14
I have populated DateGridView control with following code:

DataTable dataTable = new DataTable();
SqlDataAdapter da = new SqlDataAdapter("select * from Customers", someConnectionString);
dataTable.Clear();
da.Fill(dataTable);
dataGridView1.DataSource = dataTable;

Now I would like to add BindingNavigator Control so that user can easily navigate, add or remove records.
How do I "linked" the two controls?
0
Comment
Question by:michellechan57
  • 2
  • 2
4 Comments
 
LVL 6

Accepted Solution

by:
e1v earned 500 total points
ID: 17142028
If you have a BindingNavigator called bindingNavigator1 on your form, you can to this:

DataTable dataTable = new DataTable();
SqlDataAdapter da = new SqlDataAdapter("select * from Customers", someConnectionString);
dataTable.Clear();
da.Fill(dataTable);

BindingSource bindingSource = new BindingSource(dataTable, null);
dataGridView1.DataSource = bindingSource;
bindingNavigator1.BindingSource = bindingSource;


0
 

Author Comment

by:michellechan57
ID: 17142381
Hi e1v,

It works. Thanks. May I ask why the add and delete buttons in the BindingNavigator did not update the table when pressed (or should I ask as a new question)?
0
 
LVL 6

Expert Comment

by:e1v
ID: 17144222
Hi,  with "did not update the table " did you mean that dhe database did not get updated? To that you must explisit do a update and for that you need update, delete and insert-commands in your SqlDataAdapter.
Those can you create using SqlCommandBuilder like:

 SqlCommandBuilder sb = new SqlCommandBuilder(da);
            da.UpdateCommand = sb.GetUpdateCommand();
            da.DeleteCommand = sb.GetDeleteCommand();
            da.InsertCommand = sb.GetInsertCommand();

and then do
da.Update(dataTable); to write changes to the database.

All this you get for free when using drag-and-drop databinding with datasets, so for simple projects it might be worth looking into.
0
 

Author Comment

by:michellechan57
ID: 17146779
Thanks.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question