Mitja Bonca
Nearly a Posting Maven
2,485 posts since May 2009
Reputation Points: 641
Solved Threads: 474
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
Mitja Bonca
Nearly a Posting Maven
2,485 posts since May 2009
Reputation Points: 641
Solved Threads: 474