I have an XML file that is displayed on a DataGridView driven by a dataset. I am having trouble setting up a filter where a user can enter information in a textBox then have the DataGridView filter to find results.

The XML File is displayed on the DataGridView using this method:

XmlDataDocument xmlDatadoc = new XmlDataDocument();
//Opens XML file from directory
xmlDatadoc.DataSet.ReadXml(txtDirectory.Text);

DataSet ds = new DataSet("TestXML DataSet");
ds = xmlDatadoc.DataSet;

dataGridView1.DataSource = ds.DefaultViewManager;
//XML Element displayed on DataGridView
dataGridView1.DataMember = "TestElement";

In the past I was successful in filtering through a DataGridView using the BindingSource.Filter method, for example:

BindingSource.Filter = string.Format("Name Like '" + txtName.Text) + "*'";

Since I do not have a binding source here, what other method can I use to accomplish filtering. Can I setup a binding source and use my previous method? If so, how?

Any help would be appreciated. Thanks!

I did an example for you. I didnt use a xml file, but I did my own code of filling dataTable - which is dataSource to the dataGridView - but dont be scared - my point in the code is to show how to look for the value in dgv. The code loops through all the rows and column. And if the value is found, it colors blue (cell becomes selected).

As said, dont get frustrated if you see the dgv bound to the dataSaource (of bindingSourc of dataTable). The looking code is seperated from it. It manually checkes dgv.

namespace Feb15GroupControls
{
    public partial class Form1 : Form
    {
        BindingSource bs;
        DataTable table;       
        public Form1()
        {
            InitializeComponent();
            bs = new BindingSource();
            bs.DataSource = CreatingTable();
            dataGridView1.DataSource = bs;

            dataGridView1.AutoGenerateColumns = true;
            dataGridView1.RowHeadersVisible = false;
            dataGridView1.AllowUserToAddRows = true;
        }

        private DataTable CreatingTable()
        {
            table = new DataTable("myTable");
            table.Columns.Add(new DataColumn("id", typeof(int)));
            table.Columns.Add(new DataColumn("name", typeof(string)));
            table.Columns.Add(new DataColumn("age", typeof(int)));

            Customer[] customers = new Customer[] { new Customer(1, "Mitja Bonca", 31), 
                                                    new Customer(2, "John Johnson", 22), 
                                                    new Customer(3, "Karmen Glenn", 22) };
            DataRow dr;
            for (int i = 0; i < customers.Length; i++)
            {               
                dr = table.NewRow();
                dr["id"] = customers[i].id;
                dr["name"] = customers[i].name;
                dr["age"] = customers[i].age;
                table.Rows.Add(dr);
            }
            return table;
        }

        private void FilteringData(string myValue)
        {
            foreach (DataGridViewRow row in dataGridView1.Rows)
            {
                foreach (DataGridViewCell cell in row.Cells)
                {
                    if (cell.FormattedValue.ToString() != String.Empty)
                    {
                        if (cell.Value.ToString() == myValue)
                            cell.Selected = true;
                        else
                            cell.Selected = false;
                    }
                    else
                        cell.Selected = false;
                }
            }             
        }


        private void button1_Click(object sender, EventArgs e)
        {
            //DataRow[] rows = table.Select(textBox1.Text);
            string item = textBox1.Text;
            FilteringData(item);
        }

        internal class Customer
        {
            public int id { get; set; }
            public string name { get; set; }
            public int age { get; set; }

            public Customer(int _id, string _name, int _age)
            {
                id = _id;
                name = _name;
                age = _age;
            }
        }
    }
}

Hope it helps, a bit,
Mitja

hi,

i need a datagrid using xml file read by javascript,in di sorting must be there.
plaese anybody send me the code.

thanks in advance

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.