Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > DataGridView - how to programmatically configure the bound SQLServer server data source?
 

DataGridView - how to programmatically configure the bound SQLServer server data source?

I have used the VS2005 designer (C#) to create a form with a DataGridView control bound to a particular table on a particular database on a particular SQLServer server. As far as it goes this currently works fine. However, I need to extend this so that the user can choose at runtime (i.e. via the UI - not the app.config file) one of multiple servers on which the particular named database and table may reside to populate the DataGridView on creation. This does not seem to be particularly easy toachieve with the code as provided by the designer- can someone advise me thebest/easiest way to implement this?
thanks
TC
taci  Wednesday, September 16, 2009 10:20 AM
Before the (generated) call to <table name>TableAdapter.Fill in the <Form>_Load method of the form containing the DataGridView control you can create an appropriate SqlConnectionStringBuilder object, assign the DataSource property of this with the appropriate server name and thenassign theSqlConnectionStringBuilder's ConnectionString property to the form's <table name>TableAdapter.Connection.ConnectionString property. This will cause the DataGridView to load the data from the specified server.

e.g.

.
.
.

        private void Users_Load(object sender, EventArgs e)
        {
            SqlConnectionStringBuilder cc1 = new SqlConnectionStringBuilder(
                userTableAdapter.Connection.ConnectionString);
            cc1.DataSource = Server;
            userTableAdapter.Connection.ConnectionString = cc1.ConnectionString;
            // TODO: This line of code loads data into the 'dbDataSet.User' table. You can move, or remove it, as needed.
            this.userTableAdapter.Fill(this.dbDataSet.User);

        }

.
.
.
  • Marked As Answer bytaci Wednesday, September 16, 2009 1:55 PM
  •  
taci  Wednesday, September 16, 2009 1:55 PM
Before the (generated) call to <table name>TableAdapter.Fill in the <Form>_Load method of the form containing the DataGridView control you can create an appropriate SqlConnectionStringBuilder object, assign the DataSource property of this with the appropriate server name and thenassign theSqlConnectionStringBuilder's ConnectionString property to the form's <table name>TableAdapter.Connection.ConnectionString property. This will cause the DataGridView to load the data from the specified server.

e.g.

.
.
.

        private void Users_Load(object sender, EventArgs e)
        {
            SqlConnectionStringBuilder cc1 = new SqlConnectionStringBuilder(
                userTableAdapter.Connection.ConnectionString);
            cc1.DataSource = Server;
            userTableAdapter.Connection.ConnectionString = cc1.ConnectionString;
            // TODO: This line of code loads data into the 'dbDataSet.User' table. You can move, or remove it, as needed.
            this.userTableAdapter.Fill(this.dbDataSet.User);

        }

.
.
.
  • Marked As Answer bytaci Wednesday, September 16, 2009 1:55 PM
  •  
taci  Wednesday, September 16, 2009 1:55 PM

You can use google to search for other answers

Custom Search

More Threads

• error checking cells from unbound grid
• The selected rows aren't displayed
• gridview - sorting
• Automatic insertion of rows thru datagrid (.net 1.1) windows Forms
• MSDataSetGenerator, how does it work?
• Editable Datagrid
• Binding controls to nullable fields in strongly type dataset rows
• Using a subset of data as a ComboBox's DataSource
• Solution to wrongly selected new row problem
• Adding a combo box to a DGV?