Hi when I am trying to use the DataGridViewAutoFilter function in my .net 3.5 windows forms application.

I get the following message

"Operation can only be performed on cells that belong to a DataGridView control"

Can any one help me????

Thank you again in advance.

SOURCE CODE ***

dataGridViewDowntime.BindingContextChanged += new EventHandler(dataGridViewDowntime_BindingContextChanged); 

CalculatedDowntime.DefaultView.Sort = "SiteName ASC, Engine Number ASC"; 
dataGridViewDowntime.DataSource = CalculatedDowntime.DefaultView; 



private void dataGridViewDowntime_BindingContextChanged(object sender, EventArgs e) 
{ 
if (dataGridViewDowntime.DataSource == null) return; 

foreach (DataGridViewColumn col in dataGridViewDowntime.Columns) 
{ 
col.HeaderCell = new DataGridViewAutoFilter.DataGridViewAutoFilterColumnHeaderCell(col.HeaderCell); 
} 
dataGridViewDowntime.AutoResizeColumns(); 
}

END OF SOURCE CODE ***

I get the following message

"Operation can only be performed on cells that belong to a DataGridView control"

Compiling or at runtime? What line produces the error?

Compiling or at runtime? What line produces the error?

I have a button click event on and when i debug and click the button it hits this error in vs 2008.

Thank you!

Hi firstly thank you, secondly i have condensed the code so its easier for you.

Thanks

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Globalization;
using System.Collections;
using DataGridViewAutoFilter;
using System.Threading;


namespace RDTReporter
{

    public partial class frmMain : Form
    {
        
        
        //Set Culture Information and Public Constants

        public const string connstring = @"Data Source=.\SQLEXPRESS;Initial Catalog=RDTReporter;Integrated Security=True";

        public frmMain()
        {
            
            InitializeComponent();
        }


        private void btnDisplayData_Click(object sender, EventArgs e)
        {

            //SQL Adapters
            SqlDataAdapter sa = new SqlDataAdapter();
            sa.SelectCommand = query;

            SqlDataAdapter sa2 = new SqlDataAdapter();
            sa2.SelectCommand = query2;

            SqlDataAdapter sa3 = new SqlDataAdapter();
            sa3.SelectCommand = query3;

            //btnDisplayData_Click Event
            try
            {
		//CONDENSED CODE 

		
                //Fill Datatable from which to organise to perform downtime query
                int i = 1;
                foreach (var item in AQuery)
                {
                    DataRow dr = newTable.NewRow();
                    dr["SiteName"] = item.SiteName;
                    dr[1] = Convert.ToDateTime(item.Date);
                    dr[2] = item.Message;
                    dr[3] = item.EngineNumber;
                    dr[4] = i;
                    newTable.Rows.Add(dr);
                    i++;
                }
               
                DataTable CalculatedDowntime = new DataTable();

                CalculatedDowntime.Columns.Add("SiteName", typeof(string));
                CalculatedDowntime.Columns.Add("Engine Down", typeof(DateTime));
                CalculatedDowntime.Columns.Add("Engine Up", typeof(DateTime));
                CalculatedDowntime.Columns.Add("Total DownTime", typeof(string));
                CalculatedDowntime.Columns.Add("Engine Number", typeof(string));
                CalculatedDowntime.Columns.Add("Percentage Downtime", typeof(string));
                CalculatedDowntime.Columns.Add("Total Downtime 2", typeof(string));

                
dataGridViewDowntime.BindingContextChanged += new EventHandler(dataGridViewDowntime_BindingContextChanged);                
               
               
                
                CalculatedDowntime.DefaultView.Sort = "SiteName ASC, Engine Number ASC";
                dataGridViewDowntime.DataSource = CalculatedDowntime;


                }

   



            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }

            DataGridViewCellStyle RedCellStyle = null;
            RedCellStyle = new DataGridViewCellStyle();
            RedCellStyle.ForeColor = Color.Red;
            DataGridViewCellStyle OrangeCellStyle = null;
            OrangeCellStyle = new DataGridViewCellStyle();
            OrangeCellStyle.ForeColor = Color.Orange;

            foreach (DataGridViewRow row in dataGridViewDowntime.Rows)
            {
                if (Convert.ToInt32((row.Cells["Total DownTime"].Value)) >= 300)
                {

                    row.DefaultCellStyle = OrangeCellStyle;

                }
                if (Convert.ToInt32((row.Cells["Total DownTime"].Value)) >= 600)
                {
                    row.DefaultCellStyle = RedCellStyle;

                }
            }



        }

private void dataGridViewDowntime_BindingContextChanged(object sender, EventArgs e)
  {
      if (dataGridViewDowntime.DataSource == null) return;
      
      foreach (DataGridViewColumn col in dataGridViewDowntime.Columns)
      {
          col.HeaderCell = new
              DataGridViewAutoFilterColumnHeaderCell(col.HeaderCell);
      }
      dataGridViewDowntime.AutoResizeColumns();
  }

I'm not sure what is executing that when the error occurs, but I'm pretty sure that the error is telling you that the cell does not yet belong to the DataGridView. Is it possible that the operation is being performed before the column is added, or that the DataGridView itself is not yet created?

hi, I am beginer, and I want use datagridviewAutoFilter. I use vb.net. Can you help me? How I can set filter list size?, filter list checkboxes?

This article has been dead for over six months. Start a new discussion instead.