Hola.. ke pena la intensidad con esto pero es ke en realidad lo necesito.
Como insertar ComboBox en toda una Columna de un DataGrid?
Gracias.
| |||
Lo logré!! Bueno.... gracias por sus respuestas ![]() Ya logré insertar el ComboBox en la grid, ya el problema es que no me kiere cargar datosss ![]() Voy a poner el código de como lo hice por si alguien lo necesita y por si alguien sabe donde tengo el error o que estoy haciendo mal que solo me trae los cobobox vacios. Bueno... esta es la clase que tengo:
Código:
using System; using System.Data; using System.Data.SqlClient; using System.Windows.Forms; using System.Drawing; public class DataGridComboBox : DataGridColumnStyle { private ComboBox myCombo= new ComboBox(); private bool isEditing; public DataGridComboBox() : base() { myCombo.Visible = false; SqlDataAdapter da = new SqlDataAdapter("select * from tblUsuarios","server=server;uid=sa;pwd=sa;database=dbseguridadspf"); DataSet ds = new DataSet(); da.Fill(ds); myCombo.DataSource=ds.Tables[0]; myCombo.DisplayMember=ds.Tables[0].Rows[0]["strnombrecorto"].ToString(); myCombo.ValueMember=ds.Tables[0].Rows[0]["strnombrecorto"].ToString(); } protected override void Abort(int rowNum) { isEditing = false; myCombo.ValueMemberChanged -= new EventHandler(ComboBoxValueMemberChanged); Invalidate(); } protected override bool Commit(CurrencyManager dataSource, int rowNum) { myCombo.ValueMemberChanged-= new EventHandler(ComboBoxValueMemberChanged); if (!isEditing)return true; isEditing = false; try { string value = myCombo.ValueMember; SetColumnValueAtRow(dataSource, rowNum, value); } catch (Exception) { Abort(rowNum); return false; } Invalidate(); return true; } protected override void Edit(CurrencyManager source, int rowNum,Rectangle bounds, bool readOnly,string instantText, bool cellIsVisible) { //DateTime value = (DateTime) GetColumnValueAtRow(source, rowNum); string value = myCombo.ValueMember; if (cellIsVisible) { myCombo.Bounds = new Rectangle(bounds.X + 2, bounds.Y + 2, bounds.Width - 4, bounds.Height - 4); myCombo.ValueMember = value; myCombo.Visible = true; myCombo.ValueMemberChanged += new EventHandler(ComboBoxValueMemberChanged); } else { myCombo.ValueMember = value; myCombo.Visible = false; } if (myCombo.Visible)DataGridTableStyle.DataGrid.Invalidate(bounds); } protected override Size GetPreferredSize(Graphics g, object value) { return new Size(100, myCombo.PreferredHeight + 4); } protected override int GetMinimumHeight() { return myCombo.PreferredHeight + 4; } protected override int GetPreferredHeight(Graphics g, object value) { return myCombo.PreferredHeight + 4; } protected override void Paint(Graphics g, Rectangle bounds, CurrencyManager source, int rowNum) { Paint(g, bounds, source, rowNum, false); } protected override void Paint(Graphics g, Rectangle bounds,CurrencyManager source, int rowNum,bool alignToRight) { Paint(g,bounds, source, rowNum, Brushes.Red, Brushes.Blue, alignToRight); } protected override void Paint(Graphics g, Rectangle bounds,CurrencyManager source, int rowNum,Brush backBrush, Brush foreBrush,bool alignToRight) { //DateTime date = (DateTime) GetColumnValueAtRow(source, rowNum); Rectangle rect = bounds; g.FillRectangle(backBrush,rect); rect.Offset(0, 2); rect.Height -= 2; //g.DrawString(date.ToString("d"), //this.DataGridTableStyle.DataGrid.Font, //foreBrush, rect); } protected override void SetDataGridInColumn(DataGrid value) { base.SetDataGridInColumn(value); if (myCombo.Parent != null) { myCombo.Parent.Controls.Remove(myCombo); } if (value != null) { value.Controls.Add(myCombo); } } private void ComboBoxValueMemberChanged(object sender, EventArgs e) { this.isEditing = true; base.ColumnStartedEditing(myCombo); } } Y en el formulario este es el código que tengo: Tengo estas dos funciones:
Código:
Los cuales llamo en el Load del formulario asiprivate void AddGridStyle() { DataGridTableStyle myGridStyle = new DataGridTableStyle(); myGridStyle.MappingName = "Table"; DataGridTextBoxColumn nameColumnStyle = new DataGridTextBoxColumn(); // nameColumnStyle.MappingName = "strNomUsuario"; // nameColumnStyle.HeaderText= "strNomUsuario"; myGridStyle.GridColumnStyles.Add(nameColumnStyle); DataGridComboBox ComboBoxColumnStyle = new DataGridComboBox(); ComboBoxColumnStyle.MappingName = "strNombreCorto"; ComboBoxColumnStyle.HeaderText = "strNombreCorto"; ComboBoxColumnStyle.Width = 100; myGridStyle.GridColumnStyles.Add(ComboBoxColumnStyle); dgTReporteI.TableStyles.Add(myGridStyle); } private void AddData() { dsCombos= new DataSet(); dsCombos=clsUtilidades.SelectSeguridad("strNomUsuario, strNombreCorto","tblUsuarios","intEstado= 1 and strTipoUsuario=1 or strTipoUsuario=3 or strTipoUsuario=6 or strTipoUsuario=8",ds); dgTReporteI.DataSource=dsCombos.Tables[0]; // foreach(DataRow dr in dsCombos.Tables[0].Rows) // { // DataRow dRow = namesDataTable.NewRow(); // dRow["strNomUsuario"] = dr["strNomUsuario"].ToString(); // namesDataTable.Rows.Add(dRow); // // DataRow dRow = namesDataTable.NewRow(); // dRow["strNombreCorto"] = dr["strNombreCorto"].ToString(); // namesDataTable.Rows.Add(dRow); // } // namesDataTable.AcceptChanges(); }
Código:
Ok gracias espero que esta ves si me den una pequeña ayudita #region Carga Combos en Grid namesDataTable= new DataTable("tblUsuarios"); namesDataTable.Columns.Add(new DataColumn("strNomUsuario")); DataColumn DatosColumna = new DataColumn("strNombreCorto"); namesDataTable.Columns.Add(DatosColumna); AddGridStyle(); AddData(); #endregion ![]() Chau ![]() |