Display SQL Server table in DataGridView - c#

I think it's kind of noob question but I'm new to SQL Server in .NET and I've already lost several hours on this...
I started new project, inserted DataGridView on empty form and as Data Source I chose Add->Database and I created new SQL Server Database File. I called it db.mdf. Now I get DataSet named dbDataset and BindingSource named dbDataSetBindingSource. I also added LINQ to SQL Classes to my project and dragged my table (where I added some rows before) to my .dbml. Next I doubleclicked my dbDataset and in designer dragged and dropped the same table.
I hope you have an image now ;). The thing is that when I run program, nothing shows in DataGridView. I know that I can do
dbDataClassesDataContext db = new dbDataClassesDataContext();
var records= from rec in db.MyTable select rec;
dataGridView1.DataSource = records;
And it works perfect. But I believe that dbDataSet and dbDataSetBindingSource should work too... How to use them just to show data in DataGridView?

OMG, found it... For any others with this problem in future. Click on BindingSource in designer and in properties choose table in DataMember.

Related

Extend View and Dataset of new column. Giv empty columns only new columns

I have problem with new columns in datagrid. I have many columns but I need to add 3 more.
In SQL Server 2008 Management Studio, I design the view and add more columns that exist in the ViewTable "V_FKD".
In VS2008 DataSet Designer. I add fine columns to DataSet table "V_FKDD" and in DataSet TableAdapter "V_FKDD_TableAdapter" extend all query.
The query works fine in Management Studio.
I add 3 new column to DataGridView to which it is assigned BindingSource to which it's is assigned DataSet.
And all columns in row have data but my new columns are always empty (I check this in DB I have many rows).
Please tell me why? How to resolve this problem?
Resolved! Program have other query (SqlCommand) in control BackgroundWorker

updating dataset when adding a new field in database in c#

I have made a database and created a table with five fields and used add new datasource and added a new dataset by using that. And I have made a form and inserted all fields by dragging them into the form and changed lots of the properties. Now I need to add a new field into the table in database. Do I have to create a new datasource and a new dataset and do everything from beginning or there is a way to update the datasource and dataset so that the new field automatically appears in them?
Any answer would be appreciated.
As you can read here you should right-click the TableAdapter in the Dataset Designer and click 'Configure' the TableAdapter Configuration Wizard opens where you can reconfigure the main query that defines the schema of your table. This should leave the additional TableAdapter queries (the additional methods) that were added after initial configuration.
So follow these steps
Open you xsd file.
Right click on Fill,GetData -> Configure
Click Query builder and add the new column in the query statment
Click ok and then your dataset is updated with the new database structure
If it doesn't work try deleting the dataTable and drag your table from Server Explorer again in your xsd file

Refreshing DataGridView?

I would like to provide a "Refresh" button on a form containing a DataGridView object bound to a MySQL database. I have gotten screens without columns populated from a query with calculated fields by "new"ing the MySQLCommand, MySQLDataAdapter, MySqlCommandBuilder and DataSet objects and rerunning the query and reloading the binding source to the DataGridView object.
For ones that have calculated columns, this does not work.
Surely, there must be some way to have a DataGridView object reload from the DB.
I am using SharpDevelop C# 5 with .NET framework 4.0, MySQL Community Server 5.3.33 and MySQL Data Connector/.NET V 6.7.4
Any leads much appreciated!
Jesse and Brian,
I am doing WinForms - sorry for not being clear. I have just worked out a solution. First, just executing what was run in the initial load is not enough. I found one must "new" the MySqlCommand, MySQLDataAdapter, MySqlCommandBuilder and DataSet objects, then run the data fetch as in the form load. Also, this cannot be called while doing anything that looks or smells like "editing" or it will throw an exception. I wanted my form to refresh just after edit, so I put a timer on the form and enabled it at the end of the RowValidated event and let the timer call the ReloadGrid routine. Also, if the user is on, say row 822, I found the focus went to row 0 and cell 0, so I save the current row and cell to a point and then set the focus back after the data refresh.
private void BindGrid(){
//retrieve data
//bind
DataGridView.DataSource = dataSource;
DataGridView.DataBind();
}
//call BindGrid() anytime you need to refresh data, for example in a button event click handler

How do I get bound winforms controls to refresh when their datasource is updated?

I'm very new to working with databases in C# (but not C# itself) and the whole concept of data binding, so please bear that in mind. I'm also using SQL Server CE if that affects this.
I'm trying to use a strongly-typed dataset built with VS2010 to access an SQL Server CE database, and bind WinForms controls to it to display data. I can get the controls bound fine, and they show the data that's currently in the database no problem. I can also insert new data into the database, and if I restart the application the inserted data shows up in the controls (specifically a ComboBox, but I'll be using more in the future).
The problem is that the new rows don't show up at all until I restart the app, and my understanding of data binding is that the controls should automagically update themselves. I've read about INotifyPropertyChanged but I'm not sure if it's the right thing to use here, and if it is, how do I use it for row insertions?
I'm binding the combobox like this:
DataSet.tagDataTable tagdata = new tagTableAdapter().GetData();
comboBox1.DataSource = tagdata;
comboBox1.DisplayMember = tagdata.tagnameColumn.ColumnName;
comboBox1.ValueMember = tagdata.tagIDColumn.ColumnName;
and inserting like this:
Guid g = Guid.NewGuid();
new tagTableAdapter().Insert(g, Name)
where Name is just a string pulled from a textbox.
Thanks.
EDIT: I should mention that the DB table I'm using is called tag, with two columns, tagID and tagname. tagID is a GUID, tagname is a varchar.
What you are doing with your TableAdpater.Insert is inserting your data directly into your database and you're circumventing any notifications in your application. If you do that then you have to do what iefpw said and that was to reload your datatable and rebind your controls.
An alternate method would be to add a row into your datatable. You can try something like this:
// retrieve the datatable from the control
DataSet.tagDataTable tagdata = comboBox1.DataSource as DataSet.tagDataTable;
// create a new row and fill in the data
var dr = tagdata.NewRow();
dr["tagid"] = Guid.NewGuid();
dr["tagname"] = Name;
// actually add it to the table
tagdata.Rows.Add(dr);
At this point you've added it to your DataTable and your combobox will automatically update, but the caveat is that it has not been written to your database so you'll need to make sure at some point you save to your database.
You should bind the data again and refresh the controls again like you did the first time. It doesn't just display the data. It is mechanic. It is not realtime.

Populating a DataGridView using a DataTable and TableAdapter based on a SELECT statement

I am pretty new to C# (and Visual Studio) and I've run into a problem.
I have an Access database that I'm using, and I have need to fill a DataGridView using a SELECT statement. Here's what I've done so far:
Opened up my DataSet.xsd file.
Created a new DataTable (called MyNewTable) and TableAdapter based on the SELECT statement I want to run.
Went to my Form.cs file, added a DataGridView, and set its DataSource to Other Data Sources > Project Data Sources > DataSet > MyNewTable.
Build and run the program.
When I run it, however, it doesn't show any rows. So to check and make my SELECT statement actually returns rows, I head back over to my DataSet.xsd file and right click on MyNewTable and select Preview Data. Sure enough, when I hit the Preview Data button, there are two rows in the table.
Just to check and make sure I wasn't doing something wrong in the binding, I tried going back and setting the DataSource to a different table (a real table from the database, not one based on a SELECT statement.) When I reran it with the real table it showed data.
So what am I doing wrong? What is the proper procedure for creating a DataGridView that displays the results of a SELECT statement?
simple ..
see first of all go to the xaml of your form.. find the datagridview that you dragged ..
in there you have to find autogeneratecolumns property , then set it to true
after that come back to your c# code and do it like this...
yourdataset x = new yourdataset();
yourtableadapter y = new yourtableadapter();
y.Fill(x.yourtable);
datagridview1.itemsource = x.yourtable.defaultview ;
Please keep in mind .. this is not accurate code ... it was just for the sake of illustration..
Your dataset and tablenames might be different.. so use appropriate names..
Any problems let me know ..

Categories

Resources