válido valor pasar net llenar example dentro datos datagridviewcomboboxcolumn c# winforms datagridview combobox

valor - C#Combobox y DataGridView



meter un combobox en un datagridview (3)

Lo resolví de esta manera (tal vez no sea el más elegante, pero funciona):

private void Form5_Load(object sender, EventArgs e) { SqlConnection conn = new SqlConnection(@"Data Source=xxxxx;Initial Catalog=xxxxx;Integrated Security=True;"); conn.Open(); SqlCommand sc = new SqlCommand(" SELECT id, customername+'' - ''+cast(inserted as varchar(19)) as targ FROM baf where customername>''a'' order by customername asc, inserted desc ", conn); SqlDataReader reader; reader = sc.ExecuteReader(); DataTable dt = new DataTable(); dt.Columns.Add("id", typeof(string)); dt.Columns.Add("targ", typeof(string)); dt.Load(reader); comboBox1.ValueMember = "id"; comboBox1.DisplayMember = "targ"; comboBox1.DataSource = dt; conn.Close(); var select = "SELECT [id],[CustomerName], [email],[CapActual] FROM baf order by id desc"; var c = new SqlConnection("Data Source=xxxx;Initial Catalog=xxxxx;Integrated Security=True;"); // Your Connection String here var dataAdapter = new SqlDataAdapter(select, c); var commandBuilder = new SqlCommandBuilder(dataAdapter); var ds = new DataSet(); var bf = new DataTable("BF"); ds.Tables.Add(bf); dataAdapter.Fill(bf); dataGridView1.ReadOnly = true; dataGridView1.DataSource = bf; } private void comboBox1_SelectedIndexChanged(object sender, EventArgs e) { var select = "SELECT [id],[CustomerName],[email],[CapActual] FROM baf where id="+comboBox1.SelectedValue+" order by id desc"; var c = new SqlConnection("Data Source=xxxxx;Initial Catalog=xxxxxx;Integrated Security=True;"); // Your Connection String here var dataAdapter = new SqlDataAdapter(select,c); var commandBuilder = new SqlCommandBuilder(dataAdapter); var ds = new DataSet(); var bf = new DataTable("BF"); ds.Tables.Add(bf); dataAdapter.Fill(bf); dataGridView1.ReadOnly = true; dataGridView1.DataSource = bf; } } }

Tengo un cuadro combinado que recupera datos de una selección y una vista de cuadrícula de datos que recuperan datos de otra consulta. Me gustaría filtrar la vista de tabla de datos usando el comboboxvalue. Lo estoy intentando todo pero nada funciona. ¿Podrias ayudarme por favor? Además, ¿por qué cuando declaro dataview = ((DataTable) datagridview.datasource.defaultview (primera fila en combobox_SelectedIndexChanged) ya no puedo ver ningún valor en el cuadro combinado, en su lugar veo Data Data DataRowView pero la primera pregunta es más importante para mí

private void Form5_Load(object sender, EventArgs e) { SqlConnection conn = new SqlConnection(@"Data Source=xxxxx;Initial Catalog=xxxxx;Integrated Security=True;"); conn.Open(); SqlCommand sc = new SqlCommand(" SELECT id, customername+'' - ''+cast(inserted as varchar(19)) as targ FROM bf where customername>''a'' order by customername asc, inserted desc ", conn); SqlDataReader reader; reader = sc.ExecuteReader(); DataTable dt = new DataTable(); dt.Columns.Add("id", typeof(string)); dt.Columns.Add("targ", typeof(string)); dt.Load(reader); comboBox1.ValueMember = "id"; comboBox1.DisplayMember = "targ"; comboBox1.DataSource = dt; conn.Close(); var select = "SELECT [id],[CustomerName[email],[Capital] FROM baf order by id desc"; var c = new SqlConnection("Data Source=xxxxxx;Initial Catalog=xxxxx;Integrated Security=True;"); // Your Connection String here var dataAdapter = new SqlDataAdapter(select, c); var commandBuilder = new SqlCommandBuilder(dataAdapter); var ds = new DataSet(); dataAdapter.Fill(ds); dataGridView1.ReadOnly = true; dataGridView1.DataSource = ds.Tables[0]; } private void comboBox1_SelectedIndexChanged(object sender, EventArgs e) { //var dataView = ((DataTable)dataGridView1.DataSource).DefaultView; // if (comboBox1.Text == "Remove filter") // { // dataView.RowFilter = string.Empty; // } // else // { // //dataView.RowFilter = "id = {comboBox1.Text}"; // } //} }


Necesita utilizar BindingSource correctamente para este fin. A continuación se muestra el ejemplo completo de trabajo. Para experimentar con eso, lo que necesitas es lo siguiente:

  1. Crear un formulario en blanco Q1
  2. Agregue un control DataGridView y un control ComboBox al formulario
  3. Copie y pegue el código debajo del archivo Q1.cs
  4. Correr y experimentar

Espero que esto te ayude a avanzar.

using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; namespace WinFormQ { public partial class Q1 : Form { public Q1() { InitializeComponent(); } BindingSource bs; private void Q1_Load(object sender, EventArgs e) { // SqlConnection conn = new SqlConnection(@"Data Source=xxxxx;Initial Catalog=xxxxx;Integrated Security=True;"); // conn.Open(); // SqlCommand sc = new SqlCommand(" SELECT id, customername+'' - ''+cast(inserted as varchar(19)) as targ FROM bf where customername>''a'' order by customername asc, inserted desc ", conn); // SqlDataReader reader; // // reader = sc.ExecuteReader(); DataTable dt = new DataTable(); dt.Columns.Add("id", typeof(string)); dt.Columns.Add("targ", typeof(string)); // dt.Load(reader); dt.Rows.Add("1", "Targ-1"); // example code - remove dt.Rows.Add("2", "Targ-2"); // example code - remove dt.Rows.Add("3", "Targ-3"); // example code - remove dt.Rows.Add("4", "Targ-4"); // example code - remove comboBox1.ValueMember = "id"; comboBox1.DisplayMember = "targ"; comboBox1.DataSource = dt; // var select = "SELECT [id],[CustomerName[email],[Capital] FROM baf order by id desc"; // var c = new SqlConnection("Data Source=xxxxxx;Initial Catalog=xxxxx;Integrated Security=True;"); Your Connection String here // var dataAdapter = new SqlDataAdapter(select, c); // var commandBuilder = new SqlCommandBuilder(dataAdapter); DataSet ds = new DataSet(); DataTable bf = new DataTable("BF"); bf.Columns.Add("id", typeof(string)); // example code - remove bf.Columns.Add("CustomerName", typeof(string)); // example code - remove bf.Columns.Add("Email", typeof(string)); // example code - remove bf.Columns.Add("Capital", typeof(string)); // example code - remove ds.Tables.Add(bf); bs = new BindingSource(ds, "BF"); // dataAdapter.Fill(bf); bf.Rows.Add("1", "Customer-1", "Email-1", "Capital-1"); // example code - remove bf.Rows.Add("1", "Customer-2", "Email-2", "Capital-1"); // example code - remove bf.Rows.Add("2", "Customer-3", "Email-3", "Capital-2"); // example code - remove bf.Rows.Add("3", "Customer-4", "Email-4", "Capital-3"); // example code - remove bf.Rows.Add("3", "Customer-5", "Email-5", "Capital-3"); // example code - remove bf.Rows.Add("3", "Customer-6", "Email-6", "Capital-3"); // example code - remove bf.Rows.Add("4", "Customer-7", "Email-7", "Capital-4"); // example code - remove dataGridView1.ReadOnly = true; dataGridView1.DataSource = bs; this.comboBox1.SelectedIndexChanged += new EventHandler(comboBox1_SelectedIndexChanged); } void comboBox1_SelectedIndexChanged(object sender, EventArgs e) { if (comboBox1.Text == "Remove filter") { bs.RemoveFilter(); } else if (comboBox1.SelectedValue == null) { bs.RemoveFilter(); } else { bs.Filter = "id = " + comboBox1.SelectedValue; } } } }


Para los espectadores que están interesados ​​en encontrar cómo hacer que el código en cuestión funcione correctamente, presento la solución a continuación. Para lograr la preparación para experimentar, los datos están codificados de forma rígida y deben modificarse según la situación real.

DataView dataView = null; // <<< Difference #1 private void Form1_Load(object sender, EventArgs e) { var dt = new DataTable(); dt.Columns.Add("id", typeof(string)); dt.Columns.Add("targ", typeof(string)); dt.Rows.Add("1", "Targ-1"); // example code - remove dt.Rows.Add("2", "Targ-2"); // example code - remove dt.Rows.Add("3", "Targ-3"); // example code - remove dt.Rows.Add("4", "Targ-4"); // example code - remove comboBox1.ValueMember = "id"; comboBox1.DisplayMember = "targ"; comboBox1.DataSource = dt; var bf = new DataTable("BF"); bf.Columns.Add("id", typeof(string)); // example code - remove bf.Columns.Add("CustomerName", typeof(string)); // example code - remove bf.Columns.Add("Email", typeof(string)); // example code - remove bf.Columns.Add("Capital", typeof(string)); // example code - remove bf.Rows.Add("1", "Customer-1", "Email-1", "Capital-1"); // example code - remove bf.Rows.Add("1", "Customer-2", "Email-2", "Capital-1"); // example code - remove bf.Rows.Add("2", "Customer-3", "Email-3", "Capital-2"); // example code - remove bf.Rows.Add("3", "Customer-4", "Email-4", "Capital-3"); // example code - remove bf.Rows.Add("3", "Customer-5", "Email-5", "Capital-3"); // example code - remove bf.Rows.Add("3", "Customer-6", "Email-6", "Capital-3"); // example code - remove bf.Rows.Add("4", "Customer-7", "Email-7", "Capital-4"); // example code - remove dataGridView1.ReadOnly = true; dataGridView1.DataSource = bf; dataView = bf.DefaultView; // <<< Difference #2 this.comboBox1.SelectedIndexChanged += new EventHandler(comboBox1_SelectedIndexChanged); // <<< Difference #3 } void comboBox1_SelectedIndexChanged(object sender, EventArgs e) { if (comboBox1.Text == "Remove filter") { dataView.RowFilter = string.Empty; } else { dataView.RowFilter = string.Format("id = ''{0}''", comboBox1.SelectedValue); // <<< Difference #4 } }