passing data to another form
-
Hi, I have the following code, which works perfect, when I makes af DataGridView with drag and drop (the code pass over the FakturaId to the new form, so I can make a select on this) System.Data.DataRowView SelectedRowView; fakturasystemDataSet.FakturaRow SelectedRow; SelectedRowView = (System.Data.DataRowView)fakturaBindingSource.Current; SelectedRow = (fakturasystemDataSet.FakturaRow)SelectedRowView.Row; FakturaVis FakturaVisForm = new FakturaVis(); FakturaVisForm.LoadFakturaVis(SelectedRow.FakturaID); FakturaVisForm.Show(); But now I have made my own dataset and adapter and I have some problems with the above code. I don't know, how I get the FakturaRow in intellisense (how I get this into the code, where I instansiate the dataset and the adapter)? Is my dsView the same as, the system generated fakturaBindingSource? My code where I instansiate the dataset and the adapters: private void loadKundeFaktura() { //create a connection string to the access database OleDbConnection cn = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0; User Id=;Password=; Data Source=" + myDB); // Create the DataSet ds = new DataSet("KundeFaktura"); // Fill the Dataset with Kunder, map Default Tablename // "Table" to "Kunder". da1 = new OleDbDataAdapter("SELECT KundeId, Navn, Adresse, Postnr, Byen, Telefonnr, Mobilnr, EmailAdr, Noter FROM Kunder", cn); da1.TableMappings.Add("Table", "Kunder"); da1.Fill(ds); // Fill the Dataset with Faktura, map Default Tablename // "Table" to "Faktura". ORDER BY FakturaID DESC da2 = new OleDbDataAdapter("SELECT FakturaID, KundeID, Dato, BetalingsDato, Betalt FROM Faktura ORDER BY FakturaID DESC", cn); da2.TableMappings.Add("Table", "Faktura"); da2.Fill(ds); // Establish the Relationship "RelOrdDet" // between Kunder ---< [Faktura] System.Data.DataRelation relOrdDet; System.Data.DataColumn colMaster2; System.Data.DataColumn colDetail2; colMaster2 = ds.Tables["Kunder"].Columns["KundeId"]; colDetail2 = ds.Tables["Faktura"].Columns["KundeID"]; relOrdDet = new System.Data.DataRelation("RelOrdDet", colMaster2, colDetail2); ds.Relations.Add(relOrdDet); // The DataViewManager returned by the DefaultViewManager // property allows you to create custom settings for each // DataTable in the DataSet. dsView = ds.DefaultViewManager; // Databinding for the Grid's dgrKunder.DataSource = dsView; dgrKunder.DataMember = "Kunder"; dgrFaktura.DataSource = dsView
-
Hi, I have the following code, which works perfect, when I makes af DataGridView with drag and drop (the code pass over the FakturaId to the new form, so I can make a select on this) System.Data.DataRowView SelectedRowView; fakturasystemDataSet.FakturaRow SelectedRow; SelectedRowView = (System.Data.DataRowView)fakturaBindingSource.Current; SelectedRow = (fakturasystemDataSet.FakturaRow)SelectedRowView.Row; FakturaVis FakturaVisForm = new FakturaVis(); FakturaVisForm.LoadFakturaVis(SelectedRow.FakturaID); FakturaVisForm.Show(); But now I have made my own dataset and adapter and I have some problems with the above code. I don't know, how I get the FakturaRow in intellisense (how I get this into the code, where I instansiate the dataset and the adapter)? Is my dsView the same as, the system generated fakturaBindingSource? My code where I instansiate the dataset and the adapters: private void loadKundeFaktura() { //create a connection string to the access database OleDbConnection cn = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0; User Id=;Password=; Data Source=" + myDB); // Create the DataSet ds = new DataSet("KundeFaktura"); // Fill the Dataset with Kunder, map Default Tablename // "Table" to "Kunder". da1 = new OleDbDataAdapter("SELECT KundeId, Navn, Adresse, Postnr, Byen, Telefonnr, Mobilnr, EmailAdr, Noter FROM Kunder", cn); da1.TableMappings.Add("Table", "Kunder"); da1.Fill(ds); // Fill the Dataset with Faktura, map Default Tablename // "Table" to "Faktura". ORDER BY FakturaID DESC da2 = new OleDbDataAdapter("SELECT FakturaID, KundeID, Dato, BetalingsDato, Betalt FROM Faktura ORDER BY FakturaID DESC", cn); da2.TableMappings.Add("Table", "Faktura"); da2.Fill(ds); // Establish the Relationship "RelOrdDet" // between Kunder ---< [Faktura] System.Data.DataRelation relOrdDet; System.Data.DataColumn colMaster2; System.Data.DataColumn colDetail2; colMaster2 = ds.Tables["Kunder"].Columns["KundeId"]; colDetail2 = ds.Tables["Faktura"].Columns["KundeID"]; relOrdDet = new System.Data.DataRelation("RelOrdDet", colMaster2, colDetail2); ds.Relations.Add(relOrdDet); // The DataViewManager returned by the DefaultViewManager // property allows you to create custom settings for each // DataTable in the DataSet. dsView = ds.DefaultViewManager; // Databinding for the Grid's dgrKunder.DataSource = dsView; dgrKunder.DataMember = "Kunder"; dgrFaktura.DataSource = dsView
I don't understand the question. Why do you need intellisense ? If you've passed the ID, you can request the data from the database, surely ? Otherwise, communication between forms typically happens with delegates, or is this ASP.NET ?
Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog
-
I don't understand the question. Why do you need intellisense ? If you've passed the ID, you can request the data from the database, surely ? Otherwise, communication between forms typically happens with delegates, or is this ASP.NET ?
Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog
It isn't ASP.NET. I have to pass the Id to another form. Normally I would do it this way: I drag and drop a datagridview (AND use the Visual Studios wizard to make the connection and so on) into form1 and then I use the below code1 on a button, to pass the id with it. But my problem is (I'm a very newbee), I tried instead of using the wizard to make my own connection to the database and put data into the datagridview. See code2. Now I don't know what I shall do, to pass the id over to form2. On form2 I have som fields, which i also get from the database - but I cann't get them, don't have have the id. Kind regards, simsen :-) Code1: System.Data.DataRowView SelectedRowView; fakturasystemDataSet.FakturaRow SelectedRow; SelectedRowView = (System.Data.DataRowView)fakturaBindingSource.Current; SelectedRow = (fakturasystemDataSet.FakturaRow)SelectedRowView.Row; FakturaVis FakturaVisForm = new FakturaVis(); FakturaVisForm.LoadFakturaVis(SelectedRow.FakturaID); FakturaVisForm.Show(); Code2 private void loadKundeFaktura() { //create a connection string to the access database OleDbConnection cn = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0; User Id=password=; Data Source=" + myDB); // Create the DataSet ds = new DataSet("KundeFaktura"); // Fill the Dataset with Kunder, map Default Tablename // "Table" to "Kunder". da1 = new OleDbDataAdapter("SELECT KundeId, Navn, Adresse, Postnr, Byen, Telefonnr, Mobilnr, EmailAdr, Noter FROM Kunder", cn); da1.TableMappings.Add("Table", "Kunder"); da1.Fill(ds); // Fill the Dataset with Faktura, map Default Tablename // "Table" to "Faktura". ORDER BY FakturaID DESC da2 = new OleDbDataAdapter("SELECT FakturaID, KundeID, Dato, BetalingsDato, Betalt FROM Faktura ORDER BY FakturaID DESC", cn); da2.TableMappings.Add("Table", "Faktura"); da2.Fill(ds); // Establish the Relationship "RelOrdDet" // between Kunder ---< [Faktura] System.Data.DataRelation relOrdDet; System.Data.DataColumn colMaster2; System.Data.DataColumn colDetail2; colMaster2 = ds.Tables["Kunder"].Columns["KundeId"]; colDetail2 = ds.Tables["Faktura"].Columns["KundeID"]; relOrdDet = new System.Data.DataRelation("RelOrdDet", colMaster2, colDetail2); ds.Relations.Add(relOrdDet); // The DataViewManager returned by the DefaultViewManager // property allows you to create custom settings for each // DataTable in the DataSet. dsView = ds.DefaultViewManager; // Databinding for the Grid
-
It isn't ASP.NET. I have to pass the Id to another form. Normally I would do it this way: I drag and drop a datagridview (AND use the Visual Studios wizard to make the connection and so on) into form1 and then I use the below code1 on a button, to pass the id with it. But my problem is (I'm a very newbee), I tried instead of using the wizard to make my own connection to the database and put data into the datagridview. See code2. Now I don't know what I shall do, to pass the id over to form2. On form2 I have som fields, which i also get from the database - but I cann't get them, don't have have the id. Kind regards, simsen :-) Code1: System.Data.DataRowView SelectedRowView; fakturasystemDataSet.FakturaRow SelectedRow; SelectedRowView = (System.Data.DataRowView)fakturaBindingSource.Current; SelectedRow = (fakturasystemDataSet.FakturaRow)SelectedRowView.Row; FakturaVis FakturaVisForm = new FakturaVis(); FakturaVisForm.LoadFakturaVis(SelectedRow.FakturaID); FakturaVisForm.Show(); Code2 private void loadKundeFaktura() { //create a connection string to the access database OleDbConnection cn = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0; User Id=password=; Data Source=" + myDB); // Create the DataSet ds = new DataSet("KundeFaktura"); // Fill the Dataset with Kunder, map Default Tablename // "Table" to "Kunder". da1 = new OleDbDataAdapter("SELECT KundeId, Navn, Adresse, Postnr, Byen, Telefonnr, Mobilnr, EmailAdr, Noter FROM Kunder", cn); da1.TableMappings.Add("Table", "Kunder"); da1.Fill(ds); // Fill the Dataset with Faktura, map Default Tablename // "Table" to "Faktura". ORDER BY FakturaID DESC da2 = new OleDbDataAdapter("SELECT FakturaID, KundeID, Dato, BetalingsDato, Betalt FROM Faktura ORDER BY FakturaID DESC", cn); da2.TableMappings.Add("Table", "Faktura"); da2.Fill(ds); // Establish the Relationship "RelOrdDet" // between Kunder ---< [Faktura] System.Data.DataRelation relOrdDet; System.Data.DataColumn colMaster2; System.Data.DataColumn colDetail2; colMaster2 = ds.Tables["Kunder"].Columns["KundeId"]; colDetail2 = ds.Tables["Faktura"].Columns["KundeID"]; relOrdDet = new System.Data.DataRelation("RelOrdDet", colMaster2, colDetail2); ds.Relations.Add(relOrdDet); // The DataViewManager returned by the DefaultViewManager // property allows you to create custom settings for each // DataTable in the DataSet. dsView = ds.DefaultViewManager; // Databinding for the Grid
How does form2 get created ? Surely it's from form1, in which case you can pass the ID through the constructor ? The problem with all the wizard garbage in .NET 2.0 is that you can get so far, then you hit a brick wall and realise you do need to learn all the stuff the framework has been taking care of for you.
Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog
-
How does form2 get created ? Surely it's from form1, in which case you can pass the ID through the constructor ? The problem with all the wizard garbage in .NET 2.0 is that you can get so far, then you hit a brick wall and realise you do need to learn all the stuff the framework has been taking care of for you.
Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog
Hi Christian First thanks for helping me. I'm totally lost here, and I don't know what to do. Yes I found out, I couldn't use the wizard to do the things I will. So no I'm leaning myself the hard way, by trying to make a program for my friend. I think I use a constructer (if I understand the book I use correctly which means a constructor is when I make a "new" statement). See the below code. I have to problems. I don't know how to get the Id from the datagridview I stand on. And then to pass that with the constructer. If you want to see all the code etc, you can get it here: http://www.ansi-design.dk/ANSI.zip[^] The button (constructer) I use to open the form2 with code:
private void btnFakturaVis_Click(object sender, EventArgs e) { //The below code I used, when I had only one datagridview and used the wizard //System.Data.DataRowView SelectedRowView; //fakturasystemDataSet.FakturaRow SelectedRow; //SelectedRowView = (System.Data.DataRowView)fakturaBindingSource.Current; //SelectedRow = (fakturasystemDataSet.FakturaRow)SelectedRowView.Row; //FakturaVis FakturaVisForm = new FakturaVis(); //FakturaVisForm.LoadFakturaVis(SelectedRow.FakturaID); //FakturaVisForm.Show(); //This code I initialize the FakturaVisForm with //I here miss how to get the FakturaId from grdFaktura - dataset (ds) //dataadapter (da2) FakturaVis FakturaVisForm = new FakturaVis(); FakturaVisForm.Show(); }
-
Hi Christian First thanks for helping me. I'm totally lost here, and I don't know what to do. Yes I found out, I couldn't use the wizard to do the things I will. So no I'm leaning myself the hard way, by trying to make a program for my friend. I think I use a constructer (if I understand the book I use correctly which means a constructor is when I make a "new" statement). See the below code. I have to problems. I don't know how to get the Id from the datagridview I stand on. And then to pass that with the constructer. If you want to see all the code etc, you can get it here: http://www.ansi-design.dk/ANSI.zip[^] The button (constructer) I use to open the form2 with code:
private void btnFakturaVis_Click(object sender, EventArgs e) { //The below code I used, when I had only one datagridview and used the wizard //System.Data.DataRowView SelectedRowView; //fakturasystemDataSet.FakturaRow SelectedRow; //SelectedRowView = (System.Data.DataRowView)fakturaBindingSource.Current; //SelectedRow = (fakturasystemDataSet.FakturaRow)SelectedRowView.Row; //FakturaVis FakturaVisForm = new FakturaVis(); //FakturaVisForm.LoadFakturaVis(SelectedRow.FakturaID); //FakturaVisForm.Show(); //This code I initialize the FakturaVisForm with //I here miss how to get the FakturaId from grdFaktura - dataset (ds) //dataadapter (da2) FakturaVis FakturaVisForm = new FakturaVis(); FakturaVisForm.Show(); }
Ah... OK. Now, any class has a constuctor which is auto generated and takes no parameters. To pass parameters, you need to write another constructor. So, you have this class FakturaVis { public FakturaVis () { } } That's a class with an empty constructor. This may not even be in your code, if it's not, it gets inserterted. If you want to force people to pass an int value over, do this: class FakturaVis { private int ID; private FakturaVis () { } public FakturaVis (int ID) { this.ID = ID; } } The name you pass in does not have to be the same as the one you store, but if it is, this will allow you to scope it properly. I've made the default constructor private, as I can't stop it being generated, private stops it being used. Then I've added a constructor which I can pass the ID in to. Now when you call new, you need to pass that ID, and you now have it within your new class instance. To get the ID, I assume you're responding to a row being selected, the datagrid will have a selected row property, and then you can access the columns by index or name.
Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog
-
Ah... OK. Now, any class has a constuctor which is auto generated and takes no parameters. To pass parameters, you need to write another constructor. So, you have this class FakturaVis { public FakturaVis () { } } That's a class with an empty constructor. This may not even be in your code, if it's not, it gets inserterted. If you want to force people to pass an int value over, do this: class FakturaVis { private int ID; private FakturaVis () { } public FakturaVis (int ID) { this.ID = ID; } } The name you pass in does not have to be the same as the one you store, but if it is, this will allow you to scope it properly. I've made the default constructor private, as I can't stop it being generated, private stops it being used. Then I've added a constructor which I can pass the ID in to. Now when you call new, you need to pass that ID, and you now have it within your new class instance. To get the ID, I assume you're responding to a row being selected, the datagrid will have a selected row property, and then you can access the columns by index or name.
Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog
-
Thank you very very much. That was exactly what I needed. Now I can pass my id to form2. And thank you for having the patient with a newbee like me, who doesn't know the correct way to explain myself. Kind regards, simsen :-)
Glad to help. I'm self taught myself, which means I learned from asking in forums like these, myself.
Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog