Visualizzazione dei risultati da 1 a 10 su 10
  1. #1

    [VB.NET] combobox in datagrid

    E' già stato trattato questo argomento ma non ho trovato la soluzione. Come faccio ad inserire una combobox in un datagrid? Una soluzione potrebbe essere quella di far apparire una combobox alla quale do le coordinate della cella selezionata?
    In tal caso come posso ricavare le coordinate della cella selezionata?
    (Domanda che non c'entra molto con il post) Il codice generato automaticamente per il datagrid è:
    codice:
    Me.DataGrid1.AlternatingBackColor = System.Drawing.Color.Azure
            Me.DataGrid1.BackColor = System.Drawing.Color.Ivory
            Me.DataGrid1.DataMember = ""
            Me.DataGrid1.DataSource = Me.DataSet1.temp1
            Me.DataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText
            Me.DataGrid1.Location = New System.Drawing.Point(32, 56)
            Me.DataGrid1.Name = "DataGrid1"
            Me.DataGrid1.PreferredColumnWidth = 120
            Me.DataGrid1.PreferredRowHeight = 20
            Me.DataGrid1.RowHeaderWidth = 17
            Me.DataGrid1.Size = New System.Drawing.Size(632, 280)
            Me.DataGrid1.TabIndex = 25
    Come faccio a modificare le impostazioni di ogni singola colonna come la grandezza per esempio?
    Ciao

  2. #2

  3. #3
    Nessuno sa dire come si può inserire una combo in un Datagrid???

  4. #4
    Utente di HTML.it L'avatar di cassano
    Registrato dal
    Aug 2004
    Messaggi
    3,002
    allora ti serve per un datagrid winfoem o webform ????

  5. #5
    Grazie per l'intervento. Mi serve inserire una combo nel Datagrid per una WinForm.
    Il problema di formattare a mio piacere il datagrid l'ho risolto con:

    codice:
         
    OleDbDataAdapter1.Fill(DataSetTemp1, "temp")
          
    DataGrid1.DataSource = DataSetRicetteTemp1.Tables("temp")
    
                With DataGrid1
                .BackColor = Color.GhostWhite
                .BackgroundColor = Color.Lavender
                .BorderStyle = BorderStyle.None
                .CaptionVisible = False
                .Font = New Font("Tahoma", 10.0!)
                '.ParentRowsBackColor =  Color.Lavender
                '.ParentRowsForeColor = Color.MidnightBlue
                .PreferredRowHeight = 20
            End With
    
            Dim grdTableStyle1 As New DataGridTableStyle
            With grdTableStyle1
    
                .AlternatingBackColor = Color.Azure
                .BackColor = Color.Ivory
                .ForeColor = Color.MidnightBlue
                .GridLineColor = Color.RoyalBlue
                .HeaderBackColor = Color.BlanchedAlmond
                .HeaderFont = New Font("Tahoma", 8.0!, FontStyle.Bold)
                .HeaderForeColor = Color.Black
                .RowHeadersVisible = False
                .MappingName = "temp"
                .PreferredColumnWidth = 125
                .PreferredRowHeight = 15
            End With
    
            Dim grdColStyle1 As New DataGridTextBoxColumn
            With grdColStyle1
                .HeaderText = "col1"
                .MappingName = "col1"
                .Width = 25
                .NullText = ""
            End With
    
    
            Dim grdColStyle2 As New DataGridTextBoxColumn
            With grdColStyle2
                .HeaderText = "col2"
                .MappingName = "col2"
                .Width = 247
                .NullText = ""
                .Alignment = HorizontalAlignment.Center
            End With
    
            Dim grdColStyle3 As New DataGridTextBoxColumn
            With grdColStyle3
                .HeaderText = "col3"
                .MappingName = "col3"
                .Width = 28
                '.TextBox.BackColor = Color.Red
                .NullText = ""
                .Alignment = HorizontalAlignment.Center
            End With
    
            Dim grdColStyle4 As New DataGridTextBoxColumn
            With grdColStyle4
                .HeaderText = "col4"
                .MappingName = "col4"
                .Width = 75
                .Alignment = HorizontalAlignment.Center
                .NullText = "0"
            End With
    
            Dim grdColStyle5 As New DataGridTextBoxColumn
            With grdColStyle5
                .HeaderText = "ingrediente"
                .MappingName = "ingrediente"
                .Width = 240
                .Alignment = HorizontalAlignment.Center
                .NullText = ""
            End With
    
    
           grdTableStyle1.GridColumnStyles.AddRange(New DataGridColumnStyle() {grdColStyle1, grdColStyle2, grdColStyle3, grdColStyle4, grdColStyle5})
    
            DataGrid1.TableStyles.Add(grdTableStyle1)

  6. #6
    Questa è la classe che sono riuscito a trovare e che dovrebbe fare al caso mio, come posso farla funzionare?
    codice:
    using System;
    using System.Drawing;
    using System.Collections;
    using System.ComponentModel;
    using System.Windows.Forms;
    using System.Data;
    
    namespace RamiSaad
    {
    	/// <summary>
    	/// Summary description for DataGridComboBoxColumn.
    	/// This class builds the combobox column user control for datagrids for windows application. 
    	/// It inherits DataGridTextBoxColumn, since this is already an available control in the .Net Framework.
    	/// What needs to be modified, depending on the application, is the Leave event. 
    	/// To use this class, simply add it to the project, construct an instance, and use the properties of its ComboBox
    	/// this is a sample usage:
    	/// 
    	/// DataGridComboBoxColumn col2=new DataGridComboBoxColumn();
    	///	col2.ComboBox.DataSource=dataSet.Client;
    	///	col2.ComboBox.DisplayMember="Name";
    	///	col2.ComboBox.ValueMember="Id";
    	///	dataGrid1.TableStyles[0].GridColumnStyles.Add(col2);
    	///	
    	/// </summary>
    	public class DataGridComboBoxColumn : DataGridTextBoxColumn
    	{
    		// Hosted combobox control
    		private ComboBox comboBox;
    		private CurrencyManager cmanager;
    		private int iCurrentRow;
        
    		// Constructor - create combobox, 
    		// register selection change event handler,
    		// register lose focus event handler
    		public DataGridComboBoxColumn()
    		{
    			this.cmanager = null;
    
    			// Create combobox and force DropDownList style
    			this.comboBox = new ComboBox();
    			this.comboBox.DropDownStyle = ComboBoxStyle.DropDownList;
            
    			// Add event handler for notification when combobox loses focus
    			this.comboBox.Leave += new EventHandler(comboBox_Leave);
    		}
        
    		// Property to provide access to combobox 
    		public ComboBox ComboBox
    		{
    			get { return comboBox; }
    		}       
                
    		// On edit, add scroll event handler, and display combobox
    		protected override void Edit(System.Windows.Forms.CurrencyManager 
    			source, int rowNum, System.Drawing.Rectangle bounds, bool readOnly, 
    			string instantText, bool cellIsVisible)
    		{
    			base.Edit(source, rowNum, bounds, readOnly, instantText, 
    				cellIsVisible);
    
    			if (!readOnly && cellIsVisible)
    			{
    				// Save current row in the DataGrid and currency manager 
    				// associated with the data source for the DataGrid
    				this.iCurrentRow = rowNum;
    				this.cmanager = source;
        
    				// Add event handler for DataGrid scroll notification
    				this.DataGridTableStyle.DataGrid.Scroll 
    					+= new EventHandler(DataGrid_Scroll);
    
    				// Site the combobox control within the current cell
    				this.comboBox.Parent = this.TextBox.Parent;
    				Rectangle rect = 
    					this.DataGridTableStyle.DataGrid.GetCurrentCellBounds();
    				this.comboBox.Location = rect.Location;
    				this.comboBox.Size = 
    					new Size(this.TextBox.Size.Width, 
    					this.comboBox.Size.Height);
    
    				// Set combobox selection to given text
    				this.comboBox.SelectedIndex =
    					this.comboBox.FindStringExact(this.TextBox.Text);
    
    				// Make the combobox visible and place on top textbox control
    				this.comboBox.Show();
    				this.comboBox.BringToFront();
    				this.comboBox.Focus();
    			}
    		}
    
    		// Given a row, get the value member associated with a row.  Use the
    		// value member to find the associated display member by iterating 
    		// over bound data source
    		protected override object 
    			GetColumnValueAtRow(System.Windows.Forms.CurrencyManager source, 
    			int rowNum)
    		{
    			// Given a row number in the DataGrid, get the display member
    			object obj =  base.GetColumnValueAtRow(source, rowNum);
            
    			// Iterate through the data source bound to the ColumnComboBox
    			CurrencyManager cmanager = (CurrencyManager) 
    				(this.DataGridTableStyle.DataGrid.BindingContext[this.comboBox.DataSource]);
    			// Assumes the associated DataGrid is bound to a DataView or 
    			// DataTable 
    			DataView dataview = ((DataView)cmanager.List);
                                
    			int i;
    
    			for (i = 0; i < dataview.Count; i++)
    			{
    				if (obj.Equals(dataview[i][this.comboBox.ValueMember]))
    					break;
    			}
            
    			if (i < dataview.Count)
    				return dataview[i][this.comboBox.DisplayMember];
            
    			return DBNull.Value;
    		}
    
    		// Given a row and a display member, iterate over bound data source to 
    		// find the associated value member.  Set this value member.
    		protected override void 
    			SetColumnValueAtRow(System.Windows.Forms.CurrencyManager source,
    			int rowNum, object value)
    		{
    			object s = value;
    
    			// Iterate through the data source bound to the ColumnComboBox
    			CurrencyManager cmanager = (CurrencyManager) 
    				(this.DataGridTableStyle.DataGrid.BindingContext[this.comboBox.DataSource]);
    			// Assumes the associated DataGrid is bound to a DataView or 
    			// DataTable 
    			DataView dataview = ((DataView)cmanager.List);
    			int i;
    
    			for (i = 0; i < dataview.Count; i++)
    			{
    				if (s.Equals(dataview[i][this.comboBox.DisplayMember]))
    					break;
    			}
    
    			// If set item was found return corresponding value, 
    			// otherwise return DbNull.Value
    			if(i < dataview.Count)
    				s =  dataview[i][this.comboBox.ValueMember];
    			else
    				s = DBNull.Value;
            
    			base.SetColumnValueAtRow(source, rowNum, s);
    		}
    
    		// On DataGrid scroll, hide the combobox
    		private void DataGrid_Scroll(object sender, EventArgs e)
    		{
    			this.comboBox.Hide();
    		}
    
    		// On combobox losing focus, set the column value, hide the combobox,
    		// and unregister scroll event handler
    		private void comboBox_Leave(object sender, EventArgs e)
    		{
    			DataRowView rowView = (DataRowView) this.comboBox.SelectedItem;
    			string s=null;
    			//in case the selected value is null.
    			try
    			{
    				if(!rowView.Row[this.comboBox.DisplayMember].GetType().FullName.Equals("System.DBNull"))
    					s = (string) rowView.Row[this.comboBox.DisplayMember];
    				else
    					s="";
    			}
    			catch(Exception ex)
    			{
    				MessageBox.Show(ex.Message);
    				//s="";
    			}
    
    			SetColumnValueAtRow(this.cmanager, this.iCurrentRow, s);
    			Invalidate();
    
    			this.comboBox.Hide();
    			this.DataGridTableStyle.DataGrid.Scroll -= 
    				new EventHandler(DataGrid_Scroll);            
    		}
    	}
    
    }
    Ciao

  7. #7
    Ritorno a farmi vivo per lo stesso motivo! l'avevo abbandonato per un po' ma poi ci sono ritornato su.
    Gentilmente qualcuno saprebbe indicarmi come posso inserire una combo in un datagrid? Ho già un datagrid al quale gli ho dato uno stile a mio piacere, dovrei aggiungere tra gli stili anche quello della combo... ma come si fa? Sono certo che non è così difficile come sembra
    Grazie e ciao

  8. #8
    Nel mio piccolo ho sempre cercato di aiutare tutti! Vuoi vedere che nessuno sa come inserire sta maledetta combo nel datagrid???
    Sta diventando un'ossessione!!!
    Tutto il codice che ho trovato è scritto in C#. E' da poco che mi sono avvicinato a VB.NET... e faccio fatica, figuriamoci se mi metto con C#
    Por favor

  9. #9
    Utente di HTML.it L'avatar di cassano
    Registrato dal
    Aug 2004
    Messaggi
    3,002
    io lo fatto grazie al codice di balena,solo che non ti rispondevoperche non trovo il codice sul pc ,ho solo il cartaceo e dirimettermi a scrivere tutto non mi va proprio .Cmq se trovoilcodice te lo facciovedere,è un po lunghetto però.

  10. #10
    Basta il pensiero.
    Vedi se riesci a fare una scansione.

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.