Click here to Skip to main content
15,896,489 members
Home / Discussions / C#
   

C#

 
GeneralRe: Multiple forms and datagridviews Pin
bwood202015-May-09 10:09
bwood202015-May-09 10:09 
GeneralRe: Multiple forms and datagridviews Pin
Henry Minute15-May-09 10:33
Henry Minute15-May-09 10:33 
GeneralRe: Multiple forms and datagridviews Pin
bwood202015-May-09 12:17
bwood202015-May-09 12:17 
GeneralRe: Multiple forms and datagridviews Pin
Henry Minute15-May-09 13:09
Henry Minute15-May-09 13:09 
GeneralRe: Multiple forms and datagridviews [modified] Pin
bwood202018-May-09 6:50
bwood202018-May-09 6:50 
GeneralRe: Multiple forms and datagridviews Pin
Henry Minute18-May-09 8:18
Henry Minute18-May-09 8:18 
GeneralRe: Multiple forms and datagridviews Pin
bwood202018-May-09 8:44
bwood202018-May-09 8:44 
GeneralRe: Multiple forms and datagridviews Pin
Henry Minute18-May-09 9:54
Henry Minute18-May-09 9:54 
bwood2020 wrote:
Right now I have it selecting all just to get the dgv on Form 2 to populate. Later the query will include a Select Distinct. Do you think this is a good way to do this or is there a more efficient method to code this?


This is a valid approach as 'proof of concept', and for now I would leave it like that, then, as you say, refine it later.

The problem with SELECT * FROM [System.Windows.Forms.DataGridView] is that System.Windows.Forms.DataGridView is not a table in a database and therefore there is nothing there to SELECT. However even if this worked, the data retrieved is not being assigned to private DataTable sds;, which is where Form2 is looking for its data. This problem can be solved with one simple change

Your code
string Query = ("SELECT * FROM [" + dataGridView1 + "]");
DataTable Sds = new DataTable(Query);
DataRow myNewRow;
myNewRow = Sds.NewRow();
Sds.Rows.Add(myNewRow);


Changed code
string Query = ("SELECT * FROM [" + dataGridView1 + "]");
sds = new DataTable(Query);  <============= Here is the change, get rid of 'DataTable Sds' and replace it with just 'sds'
DataRow myNewRow;
myNewRow = Sds.NewRow();
Sds.Rows.Add(myNewRow);


That way you would be assigning the results of the query to the field that Form2 is using.
However, I do not believe that change will cause Form2s dataGridView1 to populate as there is no data being returned from your query.
All that
sds = new DataTable(Query);

does is create a new DataTable with its Name set to "SELECT * FROM [System.Windows.Forms.DataGridView]". That DataTable with the very long name is not attached to anything, it is not part of your database, it is an orphan.

Overall the problem is that Query is not being executed. So we need to get a correct string into Query and then get your query to execute and return a result.

Can I suggest that you look at look at Executing a Query that returns Rows[^], scroll down till you get to the section Executing SQL Statements that Return Rows Using a Command Object, which is what I think will be the best way for you to do it. For now, where it shows 'SELECT * FROM customer' replace customer with the name of one of your tables (I believe you mentioned one called Products previously, that would do nicely). This example leaves you with a SqlDataReader, called reader. You need to turn that into a DataTable. Luckily, someone has done that already so look here[^], remove the 'static' modifier and copy the method into Form1 and use that routine like this.
replace
string Query = ("SELECT * FROM [" + dataGridView1 + "]");
DataTable Sds = new DataTable(Query);
DataRow myNewRow;
myNewRow = Sds.NewRow();
Sds.Rows.Add(myNewRow);

with
the code from the first link (modified for your tables)
sds = ReaderToDataTable(reader);


If that gives a result that is something approaching what you want, we can then resolve the problem of getting the right stuff from dataGridView1 on Form1 into Query

Henry Minute

Do not read medical books! You could die of a misprint. - Mark Twain
Girl: (staring) "Why do you need an icy cucumber?"
“I want to report a fraud. The government is lying to us all.”

GeneralRe: Multiple forms and datagridviews Pin
bwood202018-May-09 12:54
bwood202018-May-09 12:54 
GeneralRe: Multiple forms and datagridviews Pin
Henry Minute18-May-09 13:01
Henry Minute18-May-09 13:01 
GeneralRe: Multiple forms and datagridviews Pin
bwood202019-May-09 7:13
bwood202019-May-09 7:13 
GeneralRe: Multiple forms and datagridviews [modified] Pin
Henry Minute19-May-09 8:33
Henry Minute19-May-09 8:33 
GeneralRe: Multiple forms and datagridviews Pin
bwood202019-May-09 9:22
bwood202019-May-09 9:22 
GeneralRe: Multiple forms and datagridviews Pin
Henry Minute19-May-09 10:52
Henry Minute19-May-09 10:52 
GeneralRe: Multiple forms and datagridviews Pin
bwood202019-May-09 12:36
bwood202019-May-09 12:36 
GeneralRe: Multiple forms and datagridviews Pin
Henry Minute19-May-09 13:48
Henry Minute19-May-09 13:48 
GeneralRe: Multiple forms and datagridviews Pin
bwood202020-May-09 10:02
bwood202020-May-09 10:02 
GeneralRe: Multiple forms and datagridviews Pin
Henry Minute20-May-09 10:14
Henry Minute20-May-09 10:14 
GeneralRe: Multiple forms and datagridviews Pin
Henry Minute20-May-09 10:19
Henry Minute20-May-09 10:19 
GeneralRe: Multiple forms and datagridviews Pin
bwood202021-May-09 6:25
bwood202021-May-09 6:25 
GeneralRe: Multiple forms and datagridviews Pin
Henry Minute21-May-09 7:01
Henry Minute21-May-09 7:01 
GeneralRe: Multiple forms and datagridviews Pin
bwood202021-May-09 8:48
bwood202021-May-09 8:48 
GeneralRe: Multiple forms and datagridviews Pin
Henry Minute21-May-09 9:08
Henry Minute21-May-09 9:08 
GeneralRe: Multiple forms and datagridviews Pin
bwood202021-May-09 9:54
bwood202021-May-09 9:54 
GeneralRe: Multiple forms and datagridviews Pin
Henry Minute21-May-09 10:42
Henry Minute21-May-09 10:42 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.