Diegoctn
Diegoctn

Reputation: 55

C# System.Data.DataRowView

I am almost there trying to filter my datagridview from a combobox avoiding to using the databinding GUI. My problem is now that I see System.Data.DataRowView in my combobox inxtead of the normal values. The code:

    private void Form2_Load(object sender, EventArgs e)
    {
        SqlConnection conn = new SqlConnection(@"Data SourceXXXXX;Initial Catalog=Studio;Integrated Security=True;");
        conn.Open();
        SqlCommand sc = new SqlCommand("SELECT Nome FROM DClub order by Nome", conn);
        SqlDataReader reader;

        reader = sc.ExecuteReader();
        DataTable dt = new DataTable();
        dt.Columns.Add("Nome", typeof(string));
        dt.Load(reader);

        comboBox1.ValueMember = "Nome";
        comboBox1.DisplayMember = "Nome";
        comboBox1.DataSource = dt;

        conn.Close();


        var select = "SELECT *,bell+corp+fasc+app as Obi, bell+corp+fasc+vic+app as Tot,(bell+corp+fasc+vic+app)/5 as Med, (bell+corp+fasc+app)/4 as MedOb FROM DClub order by bell+corp+fasc+vic+app desc";
        var c = new SqlConnection("Data Source=DIEGOPC;Initial Catalog=Studio;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 = $"Nome = '{comboBox1.Text}'";
        }
    }





    }

}

Upvotes: 1

Views: 866

Answers (1)

JohnG
JohnG

Reputation: 9469

In your last comment

combo1text contains the values retrieved from the query

is fine however what I meant was what does the string itself look like. I assume it is a string that would match one of the values in the Nome column? Without seeing what this “query” strings values are in the combo box, it is difficult to give a good answer. Below, I added a DataGridView and a ComboBox to a form then populated the DataGridView and ComboBox with two DataTables using some sample data. This seems to work as you describe. I assume the row filter line: dataView.RowFilter = $"Nome = '{comboBox1.Text}'"; is simply filtering on matches in DataTables Nome column. I hope the code below helps.

DataTable dgvData;
DataTable cbData;
public Form1() {
  InitializeComponent();
  dgvData = GetDVGData();
  cbData = GetCBData(dgvData);
  dataGridView1.DataSource = dgvData;
  this.comboBox1.SelectedIndexChanged -= new System.EventHandler(this.comboBox1_SelectedIndexChanged);
  comboBox1.DisplayMember = "Filter";
  comboBox1.ValueMember = "Filter";
  comboBox1.DataSource = cbData;
  this.comboBox1.SelectedIndexChanged += new System.EventHandler(this.comboBox1_SelectedIndexChanged);
}

private DataTable GetCBData(DataTable inDT) {
  DataTable dt = new DataTable();
  dt.Columns.Add("Filter", typeof(string));
  dt.Rows.Add("Remove Filter");
  foreach (DataRow dr in inDT.Rows) {
    dt.Rows.Add(dr.ItemArray[1].ToString());
  }
  return dt;
}

private DataTable GetDVGData() {
  DataTable dt = new DataTable();
  dt.Columns.Add("Number");
  dt.Columns.Add("Name");
  dt.Columns.Add("Address");
  dt.Rows.Add("1", "John", "Texas");
  dt.Rows.Add("2", "Mary", "Florida");
  dt.Rows.Add("3", "Tom", "New York");
  dt.Rows.Add("4", "Marvin", "Moon");
  dt.Rows.Add("5", "Jason", "Holland");
  dt.Rows.Add("6", "Teddy", "New York");
  return dt;
}

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 = $"Name = '{comboBox1.Text}'";
  }
}

Upvotes: 0

Related Questions