What are different ways in VB Dot Net to populate DataGridView Grid using SQL Query? - sql

I want to populate a DataGridView Grid in my Form with Data from SQL Query or from some Table form Database. I want to know various possible ways to populate the DataGridView Grid.
Thanks in advance.

There are basically 3 ways to display data in a DataGridView
Create the rows manually in a loop: it's very inefficient if you have a lot of data
Use the DataGridView's virtual mode: the DGV only creates as many rows as can be displayed, and
dynamically changes their contents when the user scrolls. You need
to handle the CellValueNeeded event to provide the required data to
the DGV
Use databinding: that's by far the easiest way. You just fill a
DataTable with the data from the database using a DbDataAdapter, and
you assign this DataTable to the DGV's DataSource property. The DGV
can automatically create the columns (AutoGenerateColumns = true),
or you can create them manually (you must set the DataPropertyName
of the column to the name of the field you want to display). In
databound mode, the DGV works like in virtual mode except that it
takes care of fetching the data from the datasource, so you don't
have anything to do. It's very efficient even for a large number of
rows
Link.

Related

vb.net create tabpages and datagridview from list exported from SQL

Just after some ideas on the best way to create additional tabpages dynamically within a tabcontrol and also to then add the relevant data on each tab with a datagridview.
The tabcontrol will always exist, but I'm not sure whether to initially create the additional tabpages first by identifying the distinct data in SQL, and then run a further script to extract the data, which then needs to be added to the datagridview, or whether I can use vb.net to create the distinct tabpages and add the data from one table, as below.
I can add another tabpage dynamically but not sure the best way to do it for a list, that is extracted from SQL.
Hope that makes sense. Thanks
TabCode
TabName
DataName
DataType
1
One
Test Name
Text
1
One
Test Date
Date
2
Two
Test Value
Value
I've managed to create new tabpages, but only single instances as not sure how to create them from a list extracted from SQL. I'm then struggling to add the data to the relevant datagridview, which I envisage would be created with a name linked to the tabpage name.

Auto generate controls with data from datagridview

I have data copies from datagridview1 to datagridview2 the data in datagridview2 I'm trying to figure out how to take specific columns and copy there contents over to another panel where that data will be put into labels, textboxs, comboboxes. I first did it with another datagrid but its far to crowded to hold all the controls along with the data. Below is and example picture of what I'm trying to recreate from a webpage we used to use but on a winform they now want to use, this shows 1 product however if datagridview2 has multiple entries then this would show multiple lines..etc
I have no idea how to go about this or if its even doable.
I'm thinking maybe the form generates a table layout view control with all the columns, adds rows depending on amount of rows from datagridview2 and generates the controls needed in each row for each cell from datagridview2. I may be entirely wrong there, but I'm reaching the end of my knowledge on doing something like this, so here I am to be schooled =)

VB.NET : Getting all the values in DataGridView then storing it to an Access Database

I used the DataGridView instead of using a lot of textbox and combobox, that way I won't have to input data one by one and I could just do it in one go.
My database in access looks exactly the same as my datagrid. Same column number, same column names.
How do I transfer all the data I've put on the datagrid into my database when I click the Add button.
You must add a BIND to your database. This way, all changes and inputs will be realtime put into database.
See:
https://msdn.microsoft.com/en-us/library/fbk67b6z(v=vs.110).aspx

binding textboxes to a table

I have two tables that are related to each other in a 1-1 relationship (each row in the main table has exactly one corresponding row in the second table).
I also have a winform in which I'd like to show the main table in a datagridview, and for each row selected in the grid to show the fields of the corresponding row of the second table in various textboxes below the grid.
I know how to bind a datagridview to a datatable. But I'm not sure about binding several textboxes to a single row in the related datatable. I don't know what is the best way to implement it.
I'm writing in VB.Net (but can read some code in C#), using VS2008.
Any help, hints or ideas will be welcomed. Thanks.
Best way to do it is that instead of using two tables, use a join and get result in a single table, bind that table to gridview, and hide columns which you don't want to display to user.
And then use cell click event of gridview and then get the index of selected row and then use
txtBoxName.Text = GridViewName["col_name",e.RowIndex].Value.ToString();

Display Headers in Infragistics ULTRAGRID when no data in table

I am fetching data in from an SQL table using a DataSet in VB.Net. When there is data in table, it displays the data properly in the grid, but when there is no data in the table, it only shows the UltraGrid's basic view.
How can I display the column names of the table as headings of the UltraGrid even when there is no data in Table?
Thanks for the reply, but I think the problem that JD is having is a bit different from mine - in my application the data got fetched properly from SQL Server. My problem is that when there is no data in the table, I want to display the columns of the table as the headings of the grid with 0 rows. This is not happening.
It just shows a message box saying that no data is found, and the UltraGrid shows as it does by default in the application.
This is discussed in this Infragistics forum thread.
Do you know what the headers of the columns will be or is it dynamic based on the data in the table? If you know beforehand you can create the columns with the appropriate headers in an empty dataset and assign that to the grids datasource.
I notice this same behavior when I manually create a datatable and assign it as the grid's datasource. If the data table is empty, all the column header info that was previously set on the grid is lost. My solution to this was to never actually give it an empty table, if I have no rows in my table, at least have all the columns defined.
DataTable table = new DataTable("fooTable");
table.Columns.Add("fooCol1", typeof(long));
table.Columns.Add("fooCol2", typeof(string));
table.Columns.Add("fooCol3", typeof(bool));
myUltraGrid.DataSource = table;
By never setting the grid to an empty table, you keep your header info.