Visualizzazione dei risultati da 1 a 2 su 2
  1. #1
    Utente di HTML.it L'avatar di slacko
    Registrato dal
    Sep 2005
    Messaggi
    14

    [C#] DataGrid ed insert

    Salve a tutti,
    per un piccolo programma di gestione di magazzino sto utilizzando per la prima volta VisualStudio.
    Come linguaggio ho scelto C#, perchè molto vicino a Java (che conosco da tempo) e come DBMS utilizzo Access vista la poca quantità di dati da gestire e la semplicità delle relazioni tra le tabelle.
    Per la visualizzazione di tali dati ho utilizzato il componente DataGrid, vorrei sapere se rendendo editabile il contenuto dei campi del datagrid fosse possibile eseguire direttamente operazioni di update o insert nel Db,
    c'è qualcuno più pratico di me che mi può aiutare?
    Grazie in anticipo
    "Cosa è una firma?!" (cit.)

  2. #2
    Si lo puoi fare.
    Se come databinding le fornisci un dataset popolato a sua volta da un DataAdapter, puoi fornire al dataadapter 4 proprietà (SelectCommand, InsertCommand, DeleteCommand e UpdateCommand) per la manipolazione dei dati sul DB. Ti posto un esempio del SelectCommand dall'msdn:


    codice:
    using System;
    using System.Data;
    using System.Data.SqlClient;
    using System.Drawing;
    using System.Windows.Forms;
    
    public class DataGridSample:Form{
       DataSet ds;
       DataGrid myGrid;
    
       static void Main(){
          Application.Run(new DataGridSample());
       }
    
       public DataGridSample(){
          InitializeComponent();
       }
    
       void InitializeComponent(){
          this.ClientSize = new System.Drawing.Size(550, 450);
          myGrid = new DataGrid();
          myGrid.Location = new Point (10,10);
          myGrid.Size = new Size(500, 400);
          myGrid.CaptionText = "Microsoft .NET DataGrid";
          this.Text = "C# Grid Example";
          this.Controls.Add(myGrid);
          ConnectToData();
          myGrid.SetDataBinding(ds, "Suppliers");
       }
       void ConnectToData(){
          // Create the ConnectionString and create a SqlConnection.
          // Change the data source value to the name of your computer.
          
          string cString = "Persist Security Info=False;Integrated Security=SSPI;database=northwind;server=mySQLServer";
          SqlConnection myConnection = new SqlConnection(cString);
          // Create a SqlDataAdapter.
          SqlDataAdapter myAdapter = new SqlDataAdapter();
          myAdapter.TableMappings.Add("Table", "Suppliers");
          myConnection.Open();
          SqlCommand myCommand = new SqlCommand("SELECT * FROM Suppliers",
          myConnection);
          myCommand.CommandType = CommandType.Text;
       
          myAdapter.SelectCommand = myCommand;
          Console.WriteLine("The connection is open");
          ds = new DataSet("Customers");
          myAdapter.Fill(ds);
          // Create a second Adapter and Command.
          SqlDataAdapter adpProducts = new SqlDataAdapter();
          adpProducts.TableMappings.Add("Table", "Products");
          SqlCommand cmdProducts = new SqlCommand("SELECT * FROM Products", 
          myConnection);
          adpProducts.SelectCommand = cmdProducts;
          adpProducts.Fill(ds);
          myConnection.Close();
          Console.WriteLine("The connection is closed.");
          System.Data.DataRelation dr;
          System.Data.DataColumn dc1;
          System.Data.DataColumn dc2;
          // Get the parent and child columns of the two tables.
          dc1 = ds.Tables["Suppliers"].Columns["SupplierID"];
          dc2 = ds.Tables["Products"].Columns["SupplierID"];
          dr = new System.Data.DataRelation("suppliers2products", dc1, dc2);
          ds.Relations.Add(dr);
       }
    }

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.